这是一个50秒计时器,想要利用外部中断形成倒计时
#include < reg51.h >
#define GPIO_DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
code unsigned char sag[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//0123456789
sbit key_again=P3^3; //重新开始
sbit key_back=P3^2;// 倒计时
sbit key_start=P3^1; //开始
sbit key_pause=P3^0; //暂停
unsigned int count,count1;
bit k_b=1;
void delay(unsigned int n);
unsigned int count,count1;
void displayb(unsigned char a);
void display(unsigned char d);
void main(void)
{
bit k_s,k_a,k_p;
TMOD=0x01;//设置T0、T1为方式1(16位定时器)
TH0=(65536-50000)/256 ; //50ms延时常数(t=(65536-TH)/fosc/12)
TL0=(65536-50000)%256 ;
IT0=0; EX0=1; //外部中断0电平触发
IE=0x82; //开T0中断(总中断和外部中断)
TR0=0; //T0定时禁止
// EX0=0;
count=0;
count1=20;
while(1)
{ display(count);
k_s=key_start;
k_a=key_again;
k_p=key_pause;
if(k_s==0||k_p==0||k_a==0||k_b==0)
{if(k_s==0)
{TR0=1;}
do
{ display(count);
}
while(key_start==0);//判断按键是否释放
if(k_p==0)
{TR0=0;}
do
{ display(count);
}
while(key_pause==0);//判断按键是否释放
if(k_a==0)
{ TR0=1;
count=0;}
do
{ display(count);
}
while(key_again==0);//判断按键是否释放 */
if(k_b==0)
// {IT0=0; EX0=1; }
do
{displayb(count);}
while(key_back==0);//断按键是否释放 */是否释放
if(count1==0)
{count1=20;count+=1;}
if(count>50)
count=0; }}}
void timer0() interrupt 1 using 2// 定时器0中断服务程序.
{ TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count1-=1;}
void display(unsigned char d )
{ unsigned int outdata[2];
unsigned char b;
b=d;
outdata[0]=b/10; //取出高位(整形会将b/10的小数位丢掉)
outdata[1]=b%10; //取出低位
LSA=0;
LSB=0;
LSC=0;
switch(outdata[1])
{case 0:GPIO_DIG=sag[0];break;
case 1:GPIO_DIG=sag[1];break;
case 2:GPIO_DIG=sag[2];break;
case 3:GPIO_DIG=sag[3];break;
case 4:GPIO_DIG=sag[4];break;
case 5:GPIO_DIG=sag[5];break;
case 6:GPIO_DIG=sag[6];break;
case 7:GPIO_DIG=sag[7];break;
case 8:GPIO_DIG=sag[8];break;
case 9:GPIO_DIG=sag[9];break;
}
delay(50);
GPIO_DIG=0x00;
LSA=1;
LSB=0;
LSC=0;
switch(outdata[0])
{case 0:GPIO_DIG=sag[0];break;
case 1:GPIO_DIG=sag[1];break;
case 2:GPIO_DIG=sag[2];break;
case 3:GPIO_DIG=sag[3];break;
case 4:GPIO_DIG=sag[4];break;
case 5:GPIO_DIG=sag[5];break;
case 6:GPIO_DIG=sag[6];break;
case 7:GPIO_DIG=sag[7];break;
case 8:GPIO_DIG=sag[8];break;
case 9:GPIO_DIG=sag[9];break;
}
delay(50);
GPIO_DIG=0x00;
}
void displayb(unsigned char a)
{ unsigned int outdatab[2];
unsigned char m;
while(a>0)
{m=a--;
outdatab[0]=m/10; //取出高位(整形会将b/10的小数位丢掉)
outdatab[1]=m%10; //取出低位
LSA=0;
LSB=1;
LSC=1;
switch(outdatab[1])
{case 0:GPIO_DIG=sag[0];break;
case 1:GPIO_DIG=sag[1];break;
case 2:GPIO_DIG=sag[2];break;
case 3:GPIO_DIG=sag[3];break;
case 4:GPIO_DIG=sag[4];break;
case 5:GPIO_DIG=sag[5];break;
case 6:GPIO_DIG=sag[6];break;
case 7:GPIO_DIG=sag[7];break;
case 8:GPIO_DIG=sag[8];break;
case 9:GPIO_DIG=sag[9];break;
}
delay(50);
GPIO_DIG=0x00;
LSA=1;
LSB=1;
LSC=1;
switch(outdatab[0])
{case 0:GPIO_DIG=sag[0];break;
case 1:GPIO_DIG=sag[1];break;
case 2:GPIO_DIG=sag[2];break;
case 3:GPIO_DIG=sag[3];break;
case 4:GPIO_DIG=sag[4];break;
case 5:GPIO_DIG=sag[5];break;
case 6:GPIO_DIG=sag[6];break;
case 7:GPIO_DIG=sag[7];break;
case 8:GPIO_DIG=sag[8];break;
case 9:GPIO_DIG=sag[9];break;
}
delay(50);
GPIO_DIG=0x00;
delay(250);
}
EX0=0;
}
void delay(unsigned int n) // 延时子程序
{ while(n--) ;
}
void Int0()interrupt 0
{ delay(50);
if(key_back==0)
k_b=0;
}
此帖出自
小平头技术问答
一周热门 更多>