|
單片機(jī)定時(shí)器掃描按鍵,實(shí)現(xiàn)按鍵時(shí)間不同功能不同
單片機(jī)源程序如下:
- #include<stc15f2k60s2.h>
- sbit KEY1 = P2^0; //定義KEY1為P2.0腳
- sbit LED1 = P5^0; //定義LED1為P5.0腳
- sbit LED2 = P5^1; //定義LED2為P5.1腳
- unsigned char key_flag;
- unsigned int count;
- void timer0init()
- {
- AUXR |= 0x80; //定時(shí)器時(shí)鐘1T模式
- TMOD &= 0xF0; //設(shè)置定時(shí)器模式
- TL0 = 0X00; //設(shè)定定時(shí)器初值
- TH0 = 0X28; //設(shè)定定時(shí)器初值
- EA = 1; //總中斷打開
- ET0 = 1; //定時(shí)器0中斷打開
- TF0 = 0; //清楚TF0標(biāo)志
- }
- void main()
- {
- P0M0=0X00;
- P0M1=0X00;
- P2M0=0X00;
- P2M1=0X00;
- P4M0=0X00;
- P4M1=0X00;
- P5M0=0X00;
- P5M1=0X00;
- timer0init();
- while(1)
- {
- if(KEY1 == 0)
- {
- key_flag=1;
- TR0 = 1;
- }
- else
- {
- TR1 = 1;
- key_flag=0;
- count=0;
- }
- if(key_flag)
- {
- if((10<=count)&&(count<400))
- {
- LED1 = 0;
- }
- else if((400<=count)&&(count<800))
- {
- LED1 = 1;
- LED2 = 0;
- }
- else if((800<=count)&&(count<1200))
- {
- LED1 = 1;LED2 = 1;
- }
- else
- {
- LED1 = 1;
- LED2 = 1;
- LED3 = 1;
- }
-
- }
- }
- }
- void Timer0_isr(void) interrupt 1 using 1
- {
- TL0 = 0X00; //設(shè)定定時(shí)器初值
- TH0 = 0X28; //設(shè)定定時(shí)器初值
- count++;
- }
復(fù)制代碼
所有資料51hei提供下載:
key.docx
(13.71 KB, 下載次數(shù): 17)
2018-9-23 10:05 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|