51/ 52单片机时钟仿真图
程序源代码主函数
#include <REGX52.H>
#include "LCD1602.h"
#include "Timer0.h"
sbit K1 = P1^4;
sbit K2 = P1^5;
sbit K3 = P1^6;
sbit K4 = P1^7;
unsigned char Uctime[8];
unsigned int UcYear[]={2200};
unsigned char Sec=57,Min=58,Hour=23;
unsigned char Zhou=7,Day=28,Mon=2;
unsigned char KeyNum,moshi=0;
unsigned char SetTime=6,SetAlarm=0;
unsigned int Year=2200;
unsigned int tt;shan;
unsigned char Uclock[3]={23,59,00};
unsigned char Year1,Year2;
void delay()
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
void show()
{
if((moshi==0)||(moshi==1))
{
if(Uctime[3]==6)
{
LCD_ShowString(1,1,"Good ming! ");
if(SetTime==6)SetTime=3;
}
if(Uctime[3]!=6)
{
LCD_ShowString(1,5,"-");
LCD_ShowString(1,8,"-");
LCD_ShowString(1,11,"-");
LCD_ShowNum(1,1,Uctime[6],2);
LCD_ShowNum(1,3,Uctime[7],2);
LCD_ShowNum(1,6,Uctime[0],2);
LCD_ShowNum(1,9,Uctime[1],2);
LCD_ShowNum(1,12,Uctime[2],2);
}
LCD_ShowString(2,3,":");
LCD_ShowString(2,6,":");
LCD_ShowNum(2,1,Uctime[3],2);
LCD_ShowNum(2,4,Uctime[4],2);
LCD_ShowNum(2,7,Uctime[5],2);
}
if(moshi==1)
{
if((SetTime==6)&&(shan<500))
LCD_ShowString(1,1," ");
if((SetTime==7)&&(shan<500))
LCD_ShowString(1,3," ");
if((SetTime==0)&&(shan<500))
LCD_ShowString(1,6," ");
if((SetTime==1)&&(shan<500))
LCD_ShowString(1,9," ");
if((SetTime==2)&&(shan<500))
LCD_ShowString(1,12," ");
if((SetTime==3)&&(shan<500))
LCD_ShowString(2,1," ");
if((SetTime==4)&&(shan<500))
LCD_ShowString(2,4," ");
if((SetTime==5)&&(shan<500))
LCD_ShowString(2,7," ");
}
if(moshi==2)
{
LCD_ShowString(1,1,"Set the alarm");
LCD_ShowString(2,3,":");
LCD_ShowString(2,6,":");
LCD_ShowNum(2,1,Uclock[0],2);
LCD_ShowNum(2,4,Uclock[1],2);
LCD_ShowNum(2,7,Uclock[2],2);
if((SetAlarm==0)&&(shan<500))
LCD_ShowString(2,1," ");
if((SetAlarm==1)&&(shan<500))
LCD_ShowString(2,4," ");
if((SetAlarm==2)&&(shan<500))
LCD_ShowString(2,7," ");
}
if(moshi==3)
{
LCD_ShowString(1,1,"Set the alarm");
LCD_ShowString(2,3,":");
LCD_ShowString(2,6,":");
LCD_ShowNum(2,1,Uclock[0],2);
LCD_ShowNum(2,4,Uclock[1],2);
LCD_ShowNum(2,7,Uclock[2],2);
}
}
void sound()
{
if((Uctime[3]==Uclock[0])&&(Uctime[4]==Uclock[1])&&(Uctime[5]==Uclock[2]))P3_7=0;
}
void main()
{
LCD_Init();
Timer0Init();
Timer1_Init();
Year1=Year/100,Year2=Year%100;
while(1)
{
sound();
if(moshi==1)
{
Sec=Uctime[5];
Min=Uctime[4];
Hour=Uctime[3];
Zhou=Uctime[2];
Day=Uctime[1];
Mon=Uctime[0];
Year1=Uctime[6];
Year2=Uctime[7];
}
Uctime[5]=Sec;
Uctime[4]=Min;
Uctime[3]=Hour;
Uctime[2]=Zhou;
Uctime[1]=Day;
Uctime[0]=Mon;
Uctime[6]=Year1;
Uctime[7]=Year2;
show();
}
}
void Timer0_Routine() interrupt 1
{
TL0 = 0x18;
TH0 = 0xFC;
if(++tt==1000)
{
tt=0;
Sec++;
if(Sec==60)
{
Sec=0;
Min++;
if(Min==60)
{
Min=0;
Hour++;
if(Hour==24)
{
Hour=0;
Zhou++;
Day++;
if(Zhou==8)Zhou=1;
if((Mon==1)||(Mon==3)||(Mon==5)||(Mon==7)||(Mon==8)||(Mon==10)||(Mon==12))
{
if(Day==32)
{
Day=1;
Mon++;
if(Mon==13)
{
Mon=1;
Year++;
}
}
}
else
if((Mon==4)||(Mon==6)||(Mon==9)||(Mon==11)||(Mon==8)||(Mon==10))
{
if(Day==31)
{
Day=1;
Mon++;
}
}
if((Year%400==0)||(((Year%100)!=0)&&((Year%4)==0)))
{
if(Mon==2)
{
if(Day==30)
{
Day=1;
Mon++;
}
}
}
else
if(Mon==2)
{
if(Day>=29)
{
Day=1;
Mon++;
}
}
}
}
}
}
}
void anjian() interrupt 0
{
TR0 = 0;
if(K1 == 0)
{
delay();
if(K1 == 0)
{
if(++moshi==4)moshi=0;
while(!K1);
}
}
if(moshi==1)
{
if(K2 == 0)
{
delay();
if(K2 == 0)
{
SetTime++;
if(SetTime==8)SetTime=0;
while(!K2);
}
}
if(K3 == 0)
{
delay();
if(K3 == 0)
{
Uctime[SetTime]++;
if(Uctime[0]>12){Uctime[0]=1;}
if( Uctime[0]==1 || Uctime[0]==3 || Uctime[0]==5 || Uctime[0]==7 ||
Uctime[0]==8 || Uctime[0]==10 || Uctime[0]==12)
{
if(Uctime[1]>31)Uctime[1]=1;
}
else if(Uctime[0]==4 || Uctime[0]==6 || Uctime[0]==9 || Uctime[0]==11)
{
if(Uctime[1]>30)Uctime[1]=1;
}
else if(Uctime[0]==2)
{
if((UcYear[0]%400==0)||(UcYear[0]%100!=0)&&(UcYear[0]%4==0))
{
if(Uctime[1]>29)Uctime[1]=1;
}
else
{
if(Uctime[1]>28)Uctime[1]=1;
}
}
if(Uctime[2]>7){Uctime[2]=1;}
if(Uctime[3]==24){Uctime[3]=0;}
if(Uctime[4]==60){Uctime[4]=0;}
if(Uctime[5]==60){Uctime[5]=0;}
while(!K3);
}
}
if(K4 == 0)
{
delay();
if(K4 == 0)
{
Uctime[SetTime]--;
if(Uctime[0]<1){Uctime[0]=12;}
if( Uctime[0]==1 || Uctime[0]==3 || Uctime[0]==5 || Uctime[0]==7 ||
Uctime[0]==8 || Uctime[0]==10 || Uctime[0]==12)
{
if(Uctime[1]<1)Uctime[1]=31;
}
else if(Uctime[0]==4 || Uctime[0]==6 || Uctime[0]==9 || Uctime[0]==11)
{
if(Uctime[1]<1)Uctime[1]=30;
}
else if(Uctime[0]==2)
{
if((UcYear[0]%400==0)||(UcYear[0]%100!=0)&&(UcYear[0]%4==0))
{
if(Uctime[1]<1)Uctime[1]=29;
}
else
{
if(Uctime[1]<1)Uctime[1]=28;
}
}
if(Uctime[2]<1){Uctime[2]=7;}
if(Uctime[3]==255){Uctime[3]=23;}
if(Uctime[4]==255){Uctime[4]=59;}
if(Uctime[5]==255){Uctime[5]=59;}
while(!K4);
}
}
}
if(moshi==2)
{
if(K2 == 0)
{
delay();
if(K2 == 0)
{
SetAlarm++;
if(SetAlarm==3)SetAlarm=0;
while(!K2);
}
}
if(K3 == 0)
{
delay();
if(K3 == 0)
{
Uclock[SetAlarm]++;
if(Uclock[0]==24){Uclock[0]=0;}
if(Uclock[1]==60){Uclock[1]=0;}
if(Uclock[2]==60){Uclock[2]=0;}
}
}
if(K4 == 0)
{
delay();
if(K4 == 0)
{
Uclock[SetAlarm]--;
if(Uclock[0]==255){Uclock[0]=23;}
if(Uclock[1]==255){Uclock[1]=59;}
if(Uclock[2]==255){Uclock[2]=59;}
}
}
}
if(K4 == 0)
{
delay();
if(K4 == 0)
{
P3_7=1;
while(!K1);
}
}
if(moshi==0) TR0 = 1;
}
void jishi() interrupt 3
{
if(++shan==1000)shan=0;
TL1 = 0x18;
TH1 = 0xFC;
}
定时器配置
定时器.c文件
#include <REGX52.H>
void Timer0Init(void)
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
IT0=1;
EX0=1;
ET0=1;
EA=1;
}
void Timer1_Init(void) //1毫秒@12.000MHz
{
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x10; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xfc; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; //使能定时器1中断
EA=1;
EX1=1;
IT1=1;
}
定时器.H文件
#ifndef __TIMER0_H__
#define __TIMER0_H__
void Timer0Init(void);
void Timer1_Init(void);
#endif
液晶调用函数
.c文件
#include <REGX52.H>
sbit LCD_RS=P2^5;
sbit LCD_RW=P2^6;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1+0x40));
}
}
void LCD_Init()
{
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x0c);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x01);
}
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
int LCD_Pow(int X,int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
}
}
.H文件
#ifndef __LCD1602_H__
#define __LCD1602_H__
void LCD_Init();
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
#endif
按键功能
采用四个按键
按键1模式切换 按键2调位置 按键3时间加 按键4时间减