我想要建立一個5位加/減計數器。當我能夠使仿真工作時,我會更加高興地購買這些零件來構建它。我到目前爲止使用的是ATmega8,但坦率地說,只要組件相當便宜,任何解決方案都適用於我。ATmega8加/減計數器不正確計數
我在網上找到了一個簡單的加號計數器,並且改變了它以包含一個down函數。我從代碼中刪除了一些可怕的_delay_ms(1000)
聲明,這些聲明可能是爲了在輸入被保留時防止多次計數?它使櫃檯非常緩慢而且沒有反應。這是我到目前爲止:
我遇到的問題是,它不計數,當它倒數不顯示零。
代碼: 的#include 的#include 的#include 的#include
#define F_CPU 4000000
volatile uint16_t digits[5]={0,0,0,0,0}; //INITIALISE VARIABLE TO STORE INDIVIDUAL DIGITS OF THE NUMBER
void breakup(uint16_t num) { //FUNCTION TO FIND THE INDIVIDUAL DIGITS OF THE NUMBER
// AND STORE THEM IN A GLOBAL VARIABLE
DDRD=0xFF; // INITIALISE PORTD AS ALL OUTPUT
PORTD=0x00;
unsigned int i=0;
while (num!=0) {
digits[i]=num%10;
num=num/10;
i++;
}
for(i=0;i<5;i++) {
PORTD=(1<<i); // 'i'th PORTD GIVEN HIGH
display(digits[i]);
_delay_us(600);
PORTD=(0<<i); //'i'th PORTD GIVEN LOW
}
}
void display (uint16_t num) { // Bit patterns changed from the original to correct for my hardware layout.
DDRB=0b11111111;
PORTB=0xFF;
switch(num) {
case 0:
PORTB=0b00111111;
break;
case 1:
PORTB=0b00000110;
break;
case 2:
PORTB=0b01011011;
break;
case 3:
PORTB=0b01001111;
break;
case 4:
PORTB=0b01100110;
break;
case 5:
PORTB=0b01101101;
break;
case 6:
PORTB=0b01111101;
break;
case 7:
PORTB=0b00000111;
break;
case 8:
PORTB=0b01111111;
break;
case 9:
PORTB=0b01101111;
break;
default:
PORTB=0xFF;
}
}
int main(void) {
DDRB=0xFF; //Initialise PORTB as all outputs.
DDRC=0x00; //Initialise PORTC as all inputs.
PORTC=0b01000000; //Enable internal pullup for the reset pin.
char x;
char down_button_press = 0;
char up_button_press = 0;
uint16_t i, c=5;
while(1) {
x=PINC&0b00000001; //Check condition of PC0 DOWN button.
if (x==0 && down_button_press==0) {
c--;
breakup(c); //Pass c to the breakup routine and display.
down_button_press++;
}
if (x==1) {
down_button_press = 0;
}
x=PINC&0b00000010; //Check condition of PC1 UP button.
if (x==0 && up_button_press==0) {
c++;
breakup(c); //Pass c to the breakup routine and display.
up_button_press++;
}
if (x==1) {
up_button_press = 0;
}
else
breakup(c);
}
}
計數並在最後倒計時的代碼位基本上是相同的,所以我不知道爲什麼它倒計時,但不起來。在按下按鈕時試圖停止計數器進行多次計數,我已經包含了一種鎖存器&& up_button_press==0
。它不應該是debouncing代碼,它只是讓我每次按下按鈕時獲得單個增量和單個遞減。 我設法找出是不指望了,因爲當按鈕後變爲高電平向上鎖存未被清除 - 所以&& up_button_press==0
條件是不正確的:
if (x==1) {
up_button_press = 0;
}
如果有人能幫助我把事情做好,我會很感激。如果有幫助,我可以添加Proteus仿真文件?
對不起,但故事和圖片不相關,但使您的問題不太清楚。我建議刪除它。 (旁註:不知道哪些固態光盤對她有好處,但無論如何)。 SO不是諮詢或編碼服務。見[問]。 (並且在表達式中使用空格大大增強了可讀性)。 – Olaf
SSD =在此上下文中的七段顯示。 – Ctx
我不是問這些問題的專家。我已經刪除了混亂的位。 –