单片机C语言定时器程序
时间:10-02
整理:3721RD
点击:
http://bbs.elecfans.com/forum.php?mod=attachment&aid=MTI5NTk1fGE3NGJmZTEwNzJhMjQwZDg2OTZmYzI3Njc0MDU2NTg5fDE1MDk2NTU3NzE%3D&request=yes&_f=.pdf
这是定时器的电路图 本人已经写了一些程序 下面不知道该怎么写了 求各位指点一下 要求实现的如下:1、实现两种模式设定:常开、常关2、实现多种“星期”选择:全周、工作日、周末、一周中任意一天 3、实现具体时间设定,精确到分4、实现每天多组功能设置5、具有外部复位功能
#include<reg51.h>#define uchar unsigned char#define uint unsigned int#define uint unsigned int
sbit rs=P2^0; //寄存器选择位sbit rw=P2^1; //读写选择位sbit e=P2^2; //使能信号位
sbit s1=P1^0;sbit s2=P1^1;sbit s3=P1^2;sbit s4=P1^3;sbit s5=P1^4;sbit s6=P1^5;sbit s7=P1^6;
uchar code table[]={" week: "}; uchar code table1[]={" 00:00:00"};
uchar count,s1num,slnum1,slnum2; char shi,fen,miao,shi1,fen1,miao1,shi2,fen2,miao2;/*延时函数*/
void delay(uint z)
{ uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--);}
void write_com(uchar com){ rs=0; rw=0; e=0; delay(5); P0=com; delay(5); e=1; delay(5); e=0;}
void write_date(uchar date){ rs=1; rw=0;e=0; P0=date; delay(5); e=1; delay(5); e=0;}
void init(){ uchar num; e=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); write_com(0x80); for(num=0;num<15;num++){ write_date(table[num]); delay(5); }
write_com(0x80+0x40); for(num=0;num<12;num++) { write_date(table1[num]); delay(5); } TMOD=0x01; TH0=(65536-46083)/256; TL0=(65536-46083)%256; EA=1; ET0=1; TR0=1;}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void keyscan()
{
if(s1==0)
{
delay(5);
if(s1==0) //设定当前时间
{ s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
if(s4==0)
{
delay(5);
if(s4==0)
{ write_com(0x86);
slnum1++;
while(!s4);
if(slnum1==1)
{
write_date('1');
}
if(slnum1==2)
{
write_date('2');
}
if(slnum1==3)
{
write_date('3');
}
if(slnum1==4)
{
write_date('4');
}
if(slnum1==5)
{
write_date('5');
}
if(slnum1==6)
{
write_date('6');
}
if(slnum1==7)
{
write_date('7');
}
if(slnum1==8)
{
write_date('1');
write_date('2');
write_date('3');
write_date('4');
write_date('5');
}
if(slnum1==9)
{
write_date('6');
write_date('7');
write_date(' ');
write_date(' ');
write_date(' ');
}
if(slnum1==10)
{
write_date('1');
write_date('3');
write_date('5');
write_date(' ');
write_date(' ');
}
if(slnum1==11)
{
write_date('2');
write_date('4');
write_date('6');
write_date(' ');
write_date(' ');
}
}
}
if(s5==0)
{ delay(5);
if(s5==0)
{
slnum2++;
while(!s5);
if(slnum2==1)
{
write_com(0x8B);
write_date('o');
write_date('n');
}
if(slnum2==2)
{ write_com(0x8B);
write_date('f');
write_date('f');
}
delay(5);
write_com(0x0c);
TR0=1;
}
}
}
void keyscan1()
{
if(s6==0)
{ uchar num1;
delay(5);
if(s6==0)//设定定时时间
{ write_com(0x01);
s1num++;
while(!s6);
if(s1num==1)
{ write_com(0x80);
for(num1=0;num1<15;num1++)
{write_date(table[num1]);
delay(5);
}
write_com(0x80+0x40);
for(num1=0;num1<12;num1++)
{write_date(table1[num1]);
delay(5);
}
write_com(0x80+0x40+10);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao1++;
if(miao1==60)
miao1=0;
write_sfm(10,miao1);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen1++;
if(fen1==60)
fen1=0;
write_sfm(7,fen1);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi1++;
if(shi1==24)
shi1=0;
write_sfm(4,shi1);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao1--;
if(miao1==-1)
miao1=59;
write_sfm(10,miao1);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen1--;
if(fen1==-1)
fen=59;
write_sfm(7,fen1);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi1--;
if(shi1==-1)
shi1=23;
write_sfm(4,shi1);
write_com(0x80+0x40+4);
}
}
}
}
if(s4==0)
{
delay(5);
if(s4==0)
{ write_com(0x86);
slnum1++;
while(!s4);
if(slnum1==1)
{
write_date('1');
}
if(slnum1==2)
{
write_date('2');
}
if(slnum1==3)
{
write_date('3');
}
if(slnum1==4)
{
write_date('4');
}
if(slnum1==5)
{
write_date('5');
}
if(slnum1==6)
{
write_date('6');
}
if(slnum1==7)
{
write_date('7');
}
if(slnum1==8)
{
write_date('1');
write_date('2');
write_date('3');
write_date('4');
write_date('5');
}
if(slnum1==9)
{
write_date('6');
write_date('7');
write_date(' ');
write_date(' ');
write_date(' ');
}
if(slnum1==10)
{
write_date('1');
write_date('3');
write_date('5');
write_date(' ');
write_date(' ');
}
if(slnum1==11)
{
write_date('2');
write_date('4');
write_date('6');
write_date(' ');
write_date(' ');
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
keyscan1();
}
}
void time0() interrupt 1
{
TH0=(65536-46083)/256;
TL0=(65536-46083)%256;
count++;
if(count==20)
{
count=0;
miao2++;
if(miao2==60)
{
miao2=0;
fen2++;
if(fen2==60)
{
fen2=0;
shi2++;
if(shi2==24)
{
shi2=0;
}
write_sfm(4,shi2);
}
write_sfm(7,fen2);
}
write_sfm(10,miao2);
}
}