采用定时器做时钟,可调整时间和设定闹钟
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- #include "LCD.h"
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit LCD_RS = P2^0;
- sbit LCD_RW = P2^1;
- sbit LCD_EN = P2^2;
- sbit Buzzer = P2^3;
- sbit Start = P3^0;
- sbit Channg = P3^1;
- sbit Up = P3^2;
- sbit Down = P3^3;
- uchar Cursor;
- uint Year=2018,Mon=8,Day=8,Hour = 8,Min = 8,Sec = 8;
- uint Clock_Year,Clock_Mon,Clock_Day,Clock_Hour,Clock_Min,Clock_Sec; //闹钟时间设定
- uint Count;
- bit Clock_Flag=0;
- bit Buzzer_Flag;
- bit Screen_Flag = 1;
- void Timer0Init(void) //10毫秒@12.000MHz
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0xF0; //设置定时初值
- TH0 = 0xD8; //设置定时初值
- TF0 = 0; //清除TF0标志
- ET0 = 1;
- EA = 1;
- TR0 = 1;
- }
- void SysInit()
- {
- LcdInit();
- Timer0Init();
- LcdWriteStr(1,3,"Current Time");
- LcdWriteStr(2,1,"2018-10-01-12-15");
- }
- void Display_Screen_1(uint Year_1,uint Mon_1,uint Day_1,uint Hour_1,uint Min_1)
- {
- LcdWriteStr(2,1,"0000-00-00-00-00");
- LcdWriteNum(2,1,Year_1);
- LcdWriteNum(2,6,Mon_1);
- LcdWriteNum(2,9,Day_1);
- LcdWriteNum(2,12,Hour_1);
- LcdWriteNum(2,15,Min_1);
- }
- bit Set_Time()
- {
- LcdClear();
- LcdWriteStr(1,3,"Set Time");
- LcdWriteStr(2,1,"2018-08-12-08-08");
- LcdWriteCom(0xc0);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0x0f); //光标闪烁
- Cursor = 1;
- while(1)
- {
- if(Up == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Up == 0)
- {
- while(Up ==0);
- if(Cursor == 1)
- {
- Year++;
- LcdWriteNum(2,1,Year);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+0);
- }
- if(Cursor == 2)
- {
- Mon++;
- if(Mon == 13)Mon=12;
- LcdWriteNum(2,6,Mon);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+5);
- }
- if(Cursor == 3)
- {
- Day++;
- if(Day == 32)Day=31;
- LcdWriteNum(2,9,Day);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+8);
- }
- if(Cursor == 4)
- {
- Hour++;
- if(Hour == 25)Hour=24;
- LcdWriteNum(2,12,Hour);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+11);
- }
- if(Cursor == 5)
- {
- Min++;
- if(Min == 60)Min=59;
- LcdWriteNum(2,15,Min);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc+14);
- }
- }
- }
- if(Down == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Down == 0)
- {
- while(Down ==0);
- if(Cursor == 1)
- {
- Year--;
- LcdWriteNum(2,1,Year);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+0);
- }
- if(Cursor == 2)
- {
- Mon--;
- if(Mon == 0)Mon=1;
- LcdWriteNum(2,6,Mon);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+5);
- }
- if(Cursor == 3)
- {
- Day--;
- if(Day == 0)Day=1;
- LcdWriteNum(2,9,Day);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+8);
- }
- if(Cursor == 4)
- {
-
- if(Hour>0)Hour--;
- else Hour = 0;
- LcdWriteNum(2,12,Hour);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+11);
- }
- if(Cursor == 5)
- {
- if(Min>0)Min--;
- else Min = 0;
- LcdWriteNum(2,15,Min);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc+14);
- }
- }
- }
- if(Channg == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Channg == 0)
- {
- while(Channg == 0);
- Cursor++;
- if(Cursor == 6)Cursor = 1;
- if(Cursor == 1)LcdWriteCom(0xc0+0);
- if(Cursor == 2)LcdWriteCom(0xc0+5);
- if(Cursor == 3)LcdWriteCom(0xc0+8);
- if(Cursor == 4)LcdWriteCom(0xc0+11);
- if(Cursor == 5)LcdWriteCom(0xc0+14);
- Lcd1602_Delay1ms(10);
- }
- }
- if(Start == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Start == 0)
- {
- while(Start == 0);
- LcdWriteCom(0x0C); //关闭光标闪烁
- return 1;
- }
-
- }
-
- }
- }
- bit Set_Clock()
- {
- LcdClear();
- LcdWriteStr(1,3,"Set Clock ON");
- LcdWriteStr(2,1,"2018-08-12-08-08");
- Display_Screen_1(Clock_Year,Clock_Mon,Clock_Day,Clock_Hour,Clock_Min);
- LcdWriteCom(0xc0);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0x0f); //光标闪烁
- Cursor = 1;
- while(1)
- {
- if(Up == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Up == 0)
- {
- while(Up ==0);
- if(Cursor == 1)
- {
- Clock_Year++;
- LcdWriteNum(2,1,Clock_Year);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+0);
- }
- if(Cursor == 2)
- {
- Clock_Mon++;
- if(Mon == 13)Clock_Mon=12;
- LcdWriteNum(2,6,Clock_Mon);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+5);
- }
- if(Cursor == 3)
- {
- Clock_Day++;
- if(Clock_Day == 32)Clock_Day=31;
- LcdWriteNum(2,9,Clock_Day);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+8);
- }
- if(Cursor == 4)
- {
- Clock_Hour++;
- if(Clock_Hour == 25)Clock_Hour=24;
- LcdWriteNum(2,12,Clock_Hour);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+11);
- }
- if(Cursor == 5)
- {
- Clock_Min++;
- if(Clock_Min == 60)Clock_Min=59;
- LcdWriteNum(2,15,Clock_Min);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc+14);
- }
- if(Cursor == 6)
- {
- Clock_Flag = 1;
- LcdWriteStr(1,14,"ON ");
- LcdWriteCom(0x80+13);
- }
- }
- }
- if(Down == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Down == 0)
- {
- while(Down ==0);
- if(Cursor == 1)
- {
- Clock_Year--;
- LcdWriteNum(2,1,Clock_Year);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+0);
- }
- if(Cursor == 2)
- {
- Clock_Mon--;
- if(Clock_Mon == 0)Clock_Mon=1;
- LcdWriteNum(2,6,Clock_Mon);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+5);
- }
- if(Cursor == 3)
- {
- Clock_Day--;
- if(Clock_Day == 0)Clock_Day=1;
- LcdWriteNum(2,9,Clock_Day);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+8);
- }
- if(Cursor == 4)
- {
-
- if(Clock_Hour>0)Clock_Hour--;
- else Clock_Hour = 0;
- LcdWriteNum(2,12,Clock_Hour);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc0+11);
- }
- if(Cursor == 5)
- {
- if(Clock_Min>0)Clock_Min--;
- else Clock_Min = 0;
- LcdWriteNum(2,15,Clock_Min);
- Lcd1602_Delay1ms(10);
- LcdWriteCom(0xc+14);
- }
- if(Cursor == 6)
- {
- Clock_Flag = 0;
- LcdWriteStr(1,14,"OFF");
- LcdWriteCom(0x80+13);
- }
- }
- }
- if(Channg == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Channg == 0)
- {
- while(Channg == 0);
- Cursor++;
- if(Cursor == 7)Cursor = 1;
- if(Cursor == 1)LcdWriteCom(0xc0+0);
- if(Cursor == 2)LcdWriteCom(0xc0+5);
- if(Cursor == 3)LcdWriteCom(0xc0+8);
- if(Cursor == 4)LcdWriteCom(0xc0+11);
- if(Cursor == 5)LcdWriteCom(0xc0+14);
- if(Cursor == 6)LcdWriteCom(0x80+13); //闹钟开关显示
- Lcd1602_Delay1ms(10);
- }
- }
- if(Start == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Start == 0)
- {
- while(Start == 0);
- return 1;
- }
- }
- }
- }
- void Scan()
- {
- if(Start == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Start == 0)
- {
- Screen_Flag = 0; //关闭定时器显示时间
- Cursor = 1; //设置光标
- while(Start ==0);
- LcdClear();
- LcdWriteStr(1,1,">");
- LcdWriteStr(1,3,"Set Time");
- LcdWriteStr(2,3,"Set Clock");
- while(1)
- {
- if(Channg == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Channg == 0)
- {
- while(Channg == 0);
- Cursor++;
- if(Cursor==3)Cursor=1;
- if(Cursor == 1)
- {
- LcdWriteStr(1,1,">");
- LcdWriteStr(2,1," ");
- }
- if(Cursor == 2)
- {
- LcdWriteStr(1,1," ");
- LcdWriteStr(2,1,">");
- }
-
- }
- }
- if(Start == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Start == 0)
- {
- while(Start == 0);
- if(Cursor == 1)
- {
- if(Set_Time())
- {
- LcdWriteCom(0x0C); //关闭光标闪烁
- Screen_Flag = 1; //打开定时器时间显示
- LcdClear();
- LcdWriteStr(1,3,"Current Time");
- Display_Screen_1(Year,Mon,Day,Hour,Min);
- return;
- }
-
- }
- else
- {
- if(Set_Clock())
- {
- LcdWriteCom(0x0C); //关闭光标闪烁
- Screen_Flag = 1; //打开定时器时间显示
- LcdClear();
- LcdWriteStr(1,3,"Current Time");
- Display_Screen_1(Year,Mon,Day,Hour,Min);
- return;
- }
-
- }
- }
-
- }
-
- }
-
- }
-
- }
- if(Channg == 0)
- {
- Lcd1602_Delay1ms(10);
- if(Channg = 0)
- {
- while(Channg ==0);
- Buzzer_Flag = 0;
-
- }
- }
- }
- void main()
- {
- SysInit();
- while(1)
- {
- Scan();
- }
- }
- void Time0() interrupt 1 //10毫秒@11.0592MHz
- {
- TL0 = 0xF0; //设置定时初值
- TH0 = 0xD8; //设置定时初值
- Count++;
- if(Count == 100)
- {
- Count = 0;
- Sec++;
- if(Sec == 60)
- {
- Sec = 0;
- Min++;
- if(Min == 60)
- {
- Min = 0;
- Hour++;
- if(Hour == 25)
- {
- Hour = 0;
- Day++;
- if(Day == 32)
- {
- Day = 1;
- Mon++;
- if(Mon == 32)
- {
- Mon = 1;
- Year++;
- }
- }
- }
- }
- if(Screen_Flag)Display_Screen_1(Year,Mon,Day,Hour,Min);
- }
- }
- if(Clock_Flag)
- {
- if(Clock_Min == Min)
- {
- if(Clock_Hour == Hour)
- {
- if(Buzzer_Flag == 1)
- {
- Buzzer = 0;
- }
- else
- Buzzer = 1;
- }
- }
- else
- Buzzer_Flag = 1;
- }
- }
复制代码
所有资料51hei提供下载:
1602时间闹钟显示.zip
(101.22 KB, 下载次数: 93)
|