AVR 數碼管顯示設計 按鍵加減數
//******************************ICCAVR 編譯*****************************//
本文引用地址:http://www.104case.com/article/201611/316778.htm#include
#define uchar unsigned char
#define uint unsigned int
//數碼管字型表,對應0,1,2,3,4,5,6,7,8,9//
uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Da
uint CNT=0; //初始計數值:0
uchar Key_Up; //加計數按鍵標志
uchar Key_Down; //減計數按鍵標志
void DelayMs(uint i) //Ms級延時,參數i為延時時間
{uint j;
for(;i!=0;i--)
{for(j=8000;j!=0;j--) {;}}
}
void Display(uchar *p) //動態顯示函數,參數p為待顯示的數組名
{uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTC=sel; //選通最右邊的數碼管
PORTA=Table[p[i]]; //送字型碼
DelayMs(2); //顯示延時
sel=sel<<1; //移位以顯示前一位
}
}
//******************************計數值處理函數******************************//
//功能:此函數用于將計數值拆分為BCD碼的千,百,十,一數據,用于查表顯示//
//參數說明:參數i:計數值;參數p:處理數據存放的數組名//
void Process(uint i,uchar *p)
{p[0]=i/1000;
i=i%1000;
p[1]=i/100;
i=i%100;
p[2]=i/10;
i=i%10;
p[3]=i;
}
void Init_IO(void) //初始化I/O口
{DDRA=0xff; //設置A口為推挽1輸出
PORTA=0xff;
DDRB=0x00; //設置B口為不帶上拉電阻輸入
PORTB=0x00;
DDRC=0xff; //設置C口為推挽1輸出;
PORTC=0xff;
}
void Get_Key(void) //按鍵掃描函數
{while((PINB&0x01)==0) //若S1按下,置位加計數標志;
{Key_Up=1;
Display(Da
while((PINB&0x02)==0) //若S2按下,置位減計數標志;
{Key_Down=1;
Display(Da
}
void main(void)
{uchar I;
Init_IO(); //初始化I/O口
PORTA=0xff; //點亮測試所有的數碼管
PORTC=0x00;
DelayMs(3000); //延時
PORTC=0xff; //數碼管熄滅,進入計數狀態
while(1)
{Get_Key(); //按鍵掃描
if(Key_Up==1) //加計數處理
{if(CNT!=9999) //條件判斷是否計數到9999
{CNT=CNT+1; //計數值加1
Key_Up=0;} //加計數標志復位
}
if(Key_Down==1) //減計數處理
{if(CNT!=0) //條件判斷是否計數到0000
{CNT=CNT-1; //計數值減一
Key_Down=0;} //減計數標志復位
}
Process(CNT,Da
Display(Da
}
}
評論