|
分享一个按键计数器(带仿真图和编程代码)
单片机程序:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code jishu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88};//显示数字0-9
- uchar Count=0;
- void main()
- { P1=0x00;
- P2=0x00;
- TMOD=0x06; //计数器T0方式2
- TH0=TL0=256-1; //计数值为1
- ET0=1; //允许T0中断
- EX0=1; //允许INT0中断
- EA=1; //允许CPU中断
- IP=0x02; //设置优先级,T0高于INT0
- IT0=1; //INT0中断触发方式为下降沿触发
- TR0=1; //启动T0
- while(1)
- { P1=jishu[Count/10];
- P2=jishu[Count%10];
- }
- }
- void Key_Counter() interrupt 1
- {
- Count=(Count+1)%100; //10以内
- }
- //INT0中断函数//
- void Clear_Counter() interrupt 0
- {
- Count=0;
- }
- //INT0中断函数,每次按下计数键时触发INT0中断,中断程序累加计数,
- //方法2
- //#include<reg51.h>
- //int count;
- //main()
- //{EA=1;EX0=1;IT0=0;
- //while(1){
- //display(count);//显示计数值
- //}}
- // void EXTI0(void) interrupt 0//外部中断
- //{count++;}//计数
复制代码
|
评分
-
查看全部评分
|