上一次的文章并没有用到定时器,作为单片机里重要的一部分,当然也并不能漏掉,这一期是定时器60秒版!!!
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit led1 = P1^1;
uchar num,num1;
void init();
void key();
void time(uint t){
uint i,j;
for(i = t;i>0;i--)
for(j = 110;j>0;j--);
}
void kk(){
dula = 1;
P0 = table[num/10];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
time(1);
dula = 1;
P0 = table[num%10];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
time(1);
}
void main(){
init();
while(1){
key();
kk();
}
}
void time_one() interrupt 1
{
TH0 = (65536-45872)/256;
TL0 = (65536 - 45872)%256;
num1++;
if(num1 == 20){
num1 = 0;
num++;
if(num == 60){
num = 0;
}
}
}
void init(){
TMOD = 0x01;
TH0 = (65536 - 45872)/256;
TL0 = (65536 - 45872)%256;
EA = 1;
ET0 = 1;
}
void key(){
while(1){
kk();
if(key1 == 0){
time(10);
if(key1 == 0){
while(1){
num++;
kk();
time(100);
if(num == 60)
num = 0;
if(key2 == 0){break;}
if(key3 == 0){
time(10);
if(key3 == 0)
num = 0;
}
}
}
}
if(key2 == 0){
time(10);
if(key2 == 0){
while(1){
kk();
if(num == 0)
num = 60;
num--;
time(100);//
while(!key2);
if(key1 == 0){break;}
if(key3 == 0){
time(10);
if(key3 == 0)
num = 0;
}
}
}
}
}
}
还不知道怎么学习的单片机的看这里
花钱买的课程以及软件,统统免费拿走,私信拿走