|
发表于 2020-6-25 16:26:59
|
显示全部楼层
延时时,如果状态改变,则重新计时
- #include "STC_NEW_8051.H"
- #include "intrins.h"
- typedef unsigned char uint8;
- typedef unsigned int uint;
- sbit Input = P3^2; //电平检测脚
- sbit Out1 = P3^0; //输出1
- sbit Out2 = P3^1; //输出2
- bit KeyStaNow,KeyStaPre,KeyStaChg,timerOut;
- uint ms5,needTime;
- void int0_init(void)
- {
- IT0 = 0; //上升沿和下降沿
- EX0 = 1;
- EA = 1;
- }
- void Timer2Init(void) //5毫秒@11.0592MHz
- {
- AUXR |= 0x04; //定时器时钟1T模式
- T2L = 0x00; //设置定时初值
- T2H = 0x28; //设置定时初值
- AUXR |= 0x10; //定时器2开始计时
- }
- void int0_isr() interrupt 0
- {
- KeyStaPre = KeyStaNow;
- KeyStaChg = 1;
- KeyStaNow = Input;
- }
- void t2_isr() interrupt 12 using 1
- {
- ms5++;
- if(needTime<=ms5)
- {
- timerOut = 1;
- }
- }
- void needTime5ms(uint i){
- ms5=0;
- needTime = i;
- }
- main()
- {
- int0_init();
- Timer2Init(); IE2 |= 0x04;
- while (1){
- if(KeyStaChg){ //按键发生改变
- KeyStaChg=0;
- if(KeyStaNow){
- needTime5ms(2*1000/5);
- }
- else
- {
- needTime5ms(5*1000/5); //
- }
- }
- Out2 = KeyStaNow;
- if(timerOut)
- {
- Out1 = KeyStaNow;
- timerOut=0;
- }
- }
- }
复制代码
|
|