程序可以了,但是有中断会触发很多次甚至是十几次,请问要怎么用定时器消抖动呢,不用软件延时,我写的定时器好像不起作用。程序如下:
- #include<reg52.h>
- //#include <STC8G.h>
- #include <intrins.h>
- #define u8 unsigned char
- #define u16 unsigned int
- unsigned char flagkey =0;
- sbit DIO = P1^1;
- sbit CLK = P1^2;
- sbit p00 = P0^0;
- //u8 num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6F};
- u8 num[10]={0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFf,0xEF};
- u16 ms,second,min,hour;
- u16 dingshinum;
- void Delay2us() //@12.01MHz
- {
- unsigned char i;
- i = 6;
- while (--i);
- }
- void Delay3us() //@12MHz
- {
- unsigned char i;
- i = 10;
- while (--i);
- }
- void Delay5us() //@12MHz
- {
- unsigned char i;
- i = 18;
- while (--i);
- }
- void I2CStart()
- {
- CLK = 1;
- DIO = 1;
- Delay2us();
- DIO = 0;
- }
- void I2CAsk()
- {
- CLK = 0;
- Delay5us();
- while(DIO);
- CLK = 1;
- Delay2us();
- CLK = 0;
- }
- void I2CStop()
- {
- CLK = 0;
- Delay2us();
- DIO = 0;
- Delay2us();
- CLK = 1;
- Delay2us();
- DIO = 1;
- }
- void I2CWriteByte(u8 dat)
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- CLK = 0;
- if(dat&0x01)
- {
- DIO = 1;
- }
- else
- {
- DIO = 0;
- }
- Delay3us();
- dat>>=1;
- CLK = 1;
- Delay3us();
- }
- }
- void Display(u8 m,u8 s)
- {
- I2CStart(); //1637 开始
- I2CWriteByte(0x40);
- I2CAsk();
- I2CStop();
- I2CStart();
- I2CWriteByte(0xC0); //设置显示首地址即第一个LED
- I2CAsk();
- I2CWriteByte(num[m/10]);
- I2CAsk();
- I2CWriteByte(num[m%10]);
- I2CAsk();
- I2CWriteByte(num[s/10]);
- I2CAsk();
- I2CWriteByte(num[s%10]);
- I2CAsk();
- I2CWriteByte(0xFF);
- I2CAsk();
- I2CWriteByte(0xFF);
- I2CAsk();
- I2CStop();
- I2CStart();
- I2CWriteByte(0x8F);
- I2CAsk();
- I2CStop();
- }
- void main()
- {
- p00 = 1;
- P1=0x55; //P1口初始值
- EA=1; //全局中断开
- EX0=1; //外部中断0开
- IT0=1; //边沿触发
-
-
- while(1)
- {
- Display(min,second);
- }
- }
- /*------------定时器----------------*/
- void Timer0Init(void) //5毫秒@12.01MHz
- {
- TMOD = 0x01; // 设置定时器0为模式1
- TH0 = 0x0EC; // 设置定时器0的初始值,用于产生10ms的定时中断
- TL0 = 0x78;
- EA = 1; // 允许中断
- ET0 = 1; // 允许定时器0中断
- TR0 = 1; // 启动定时器0
- }
- /*------------外部中断程序----------------*/
- void ISR_Key(void) interrupt 0 using 1 {
- if(flagkey==1){
- Timer0Init();
- p00 = ~p00;
- second++;
- if(second==60){
- second=0;
- min++;
- if(min==60)
- {
- min=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
-
- }
- }
-
- }
- /*------------定时器中断子程序----------------*/
- void Timer0_isr(void) interrupt 1 using 1
- {
- TH0=0x0EC; //重新赋值
- TL0=0x78;
- dingshinum++;
- if(dingshinum<100){
- flagkey = 0;
- }else{
- flagkey = 0;
- dingshinum = 1;
- }
- }
复制代码 |