标题: 基于单片机的定时器设计 [打印本页]
作者: COCo丶 时间: 2017-10-17 08:48
标题: 基于单片机的定时器设计
#include < reg51.h >
#include <intrins.h>
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
sbit d=P1^3;
sbit T = P2^0;
sbit K1 = P1^4;
sbit K2 = P1^5;
sbit K3 = P1^6;
sbit K4 = P1^7;
#define GPIO_DIG P0
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned intLedNumVal,g,m,DisplayData[4];
void Delay10ms(unsigned int c);
void DigDisplay1();
void beep();
void system_Ini()
{
TMOD = 0x01;
TH0 = 0xee;
TL0 = 0x00;
IE = 0x8A;
TR0 =1;
}
void main()
{
unsigned char i;
system_Ini() ;
LedNumVal=300;
g=0;
while(1)
{
i=0;
DisplayData[0] = ~ DIG_CODE[LedNumVal/60/10];
DisplayData[1] = ~ (DIG_CODE[LedNumVal/60%10]|0x80);
DisplayData[2] = ~ DIG_CODE[LedNumVal%60/10];
DisplayData[3] = ~ DIG_CODE[LedNumVal%10];
DigDisplay1();
if (K1==0)
{
Delay10ms(1);
if(K1==0)
{
TR0 = 0;
LedNumVal=(LedNumVal/60+1)*60+30;
if(LedNumVal>3601)
LedNumVal=30;
while(!K1);
TR0 = 1;
}
}
if(K2==0)
{
Delay10ms(1);
if(K2==0)
{
LedNumVal=300;
while(!K2);
}
}
if(K3==0)
{
Delay10ms(1);
if(K3==0)
{
LedNumVal=1200;
while(!K3);
}
}
if(K4==0)
{
Delay10ms(1);
if(K4==0)
{
LedNumVal=3600;
while(!K4);
}
}
}
}
void T0zd(void) interrupt 1
{
TH0 = 0xee;
TL0 = 0x00;
g++;
if(g==200)
{
g=0;
LedNumVal--;
if(LedNumVal==-1)
while(1)
beep();
}
}
void beep()
{
a=b=c=d=0;
P0=~ 0x40;
T=1;
Delay10ms(50);
T=0;
Delay10ms(50);
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for(b=38;b>0;b--)
{
for(a=130;a>0;a--);
}
}
}
void DigDisplay1()
{
unsigned int j;
a=0,b=1,c=1,d=1;
GPIO_DIG= DisplayData[0];
j= 100;
while(j--);
GPIO_DIG= 0xff;
a=1,b=0,c=1,d=1;
GPIO_DIG= DisplayData[1];
j= 100;
while(j--);
GPIO_DIG= 0xff;
a=1,b=1,c=0,d=1;
GPIO_DIG= DisplayData[2];
j= 100;
while(j--);
GPIO_DIG= 0xff;
a=1,b=1,c=1,d=0;
GPIO_DIG= DisplayData[3];
j= 100;
while(j--);
GPIO_DIG= 0xff;
}
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |