当前位置:网站首页>51 MCU project design: Based on 51 MCU clock perpetual calendar
51 MCU project design: Based on 51 MCU clock perpetual calendar
2022-06-26 08:22:00 【Turn into dust】
List of articles
Bleep bleep video link : https://www.bilibili.com/video/BV1EF411z7im/
The data link : https://pan.baidu.com/s/1PkHsZd0ICn4OJIsMDl8BKA
Extraction code :p49y
One 、 Project functions
1、 The current time can be displayed 、 week 、 date
2、 You can modify the current time 、 week 、 date
3、 The ambient temperature can be obtained , And display to LCD
4、 You can set the alarm clock , When the time comes, the buzzer will sound 、 Press the key to turn off the beep
Two 、 Material selection
( One ) Master selection :STC89C52RC
STC89C52 It is a low power consumption 、 High performance CMOS8 Bit microcontroller , have 8K In system programmable Flash Memory . On a single chip , Have dexterous 8 position CPU And in system programmability Flash, bring STC89C52 It provides high flexibility for many embedded control application systems 、 Super effective solution .

( Two ) Display selection :LCD1602
LCD1602 LCD is a widely used character LCD module . It is composed of character LCD (LCD)、 Control drive main circuit HD44780 And its extended driving circuit HD44100, And a small amount of resistance 、 Capacitor elements and structural members are assembled in PCB On the board . Made by different manufacturers LCD1602 The chip may be different , But the usage is the same . To reduce costs , Most manufacturers directly put the bare chip on the board .

( 3、 ... and ) Clock chip selection :DS1302
DS1302 It's the United States DALLAS The company has launched a small current charging capacity of low-power real-time clock chip . It can be used for years 、 month 、 Japan 、 Zhou 、 when 、 branch 、 Seconds to time , And it has many functions such as leap year compensation .

( Four ) Temperature sensor selection :DS18B20
DS18B20 It is a commonly used digital temperature sensor , Its output is a digital signal , Small size , Low hardware overhead , Strong anti-interference ability , Features of high precision . [1] DS18B20 The digital temperature sensor is easy to connect , The package can be used in many occasions , Such as pipe type , Screw type , Magnet adsorption type , Stainless steel package , There are many models , Yes LTM8877,LTM8874 wait .

( 5、 ... and ) Peripheral devices : Key 、 Buzzer 、 switch 、 The button battery 、 Potentiometer 、 Triode, etc
3、 ... and 、 Schematic design

( One ) Minimum system
Power supply 、 Crystal oscillator 、 Reset circuit 
( Two ) Display circuit
The data cable is connected to P0, Pay attention to connect 1K Pull up resistance 
( 3、 ... and ) Clock chip circuit
3 Pins are connected to the single-chip microcomputer IO Mouth control 
( Four ) Key circuit 
( 5、 ... and ) Temperature sensor 
( 6、 ... and ) Power circuit and power indication 
Four 、PCB Design

5、 ... and 、 Programming
/************************************************************************************** * project :51 Single chip clock calendar design * author : Turn into dust * edition :V1.1 * mailbox :[email protected] * Time :2020 year 12 month 1 Japan 16:43:51 * Bili Bili video address :https://www.bilibili.com/video/BV1VQ4y1M79K * matters needing attention : The alarm clock is designed according to the actual object , Can't simulate , It uses an internal eeprom ***************************************************************************************/
#include "reg52.h" // This file defines some special function registers of MCU
#include "ds1302.h"
#include "temp.h"
#include "lcd.h"
#include "eeprom.h"
sbit k1 = P1^0; // Key
sbit k2 = P1^1;
sbit k3 = P1^2;
sbit k4 = P1^3;
sbit lcdled = P2^4; //lcd Backlight
sbit beep = P1^4; // Buzzer
unsigned int ti=0,alarm=0; // Modify the time parameter 、 Modify the alarm clock parameters
unsigned char alarm_hour=0x12,alarm_min=0x00; // Alarm clock hour 、 Sub parameters
enum Mode // Define enumeration 、 Three models
{
DISPLAYDATA,MODIFYDATA,SETALARMCLOCK,NONE,ALARMCLOCK
}mode;
enum Alarmswitch // Define the alarm switch
{
OFF,ON
}alarmswitch;
/********* The time delay function ***********/
void delay(unsigned int t) // Short delay
{
while(t--);
}
void delay_ms(unsigned int t) // Millisecond delay
{
unsigned int a,b;
for(a=0;a<t;a++)
for(b=0;b<120;b++);
}
/******** Show date 、 Time 、 week ***********/
void display_data(void)
{
LcdWriteCom(0x80);
LcdWritestr("20");
LcdWriteData(TIME[6]/16+0x30); // year
LcdWriteData(TIME[6]%16+0x30);
LcdWriteData('-');
LcdWriteData(TIME[4]/16+0x30); // month
LcdWriteData(TIME[4]%16+0x30);
LcdWriteData('-');
LcdWriteData(TIME[3]/16+0x30); // Japan
LcdWriteData(TIME[3]%16+0x30);
LcdWritestr(" ");
switch(TIME[5]) // Show week
{
case 0:LcdWritestr("Mon"); break;
case 1:LcdWritestr("Tue"); break;
case 2:LcdWritestr("Wed"); break;
case 3:LcdWritestr("Thu"); break;
case 4:LcdWritestr("Fri"); break;
case 5:LcdWritestr("Sat"); break;
case 6:LcdWritestr("Sun"); break;
}
if(alarmswitch==ON)LcdWriteData('.');
else LcdWriteData(' ');
LcdWriteCom(0xC0);
LcdWriteData(' ');
LcdWriteData(TIME[2]/16+0x30); // when
LcdWriteData(TIME[2]%16+0x30);
LcdWriteData(':');
LcdWriteData(TIME[1]/16+0x30); // branch
LcdWriteData(TIME[1]%16+0x30);
LcdWriteData(':');
LcdWriteData(TIME[0]/16+0x30); // second
LcdWriteData(TIME[0]%16+0x30);
LcdWritestr(" ");
}
/********* Show the temperature ***********/
void displaytemp(int temp) // Show the temperature
{
float tp;
static char flag = 1;
if(temp< 0)
{
LcdWriteCom(0xca);
LcdWriteData('-');
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
}
else
{
LcdWriteCom(0xca);
LcdWriteData('+');
tp=temp;
temp=tp*0.0625*100+0.5;
}
if(flag)
{
flag =0;
temp = 2600;
}
if(temp==8500)
return ;
LcdWriteData(temp % 10000 / 1000 + 0x30);
LcdWriteData(temp % 1000 / 100 + 0x30);
LcdWriteData('.');
LcdWriteData(temp % 100 / 10 + 0x30);
LcdWriteData(temp % 10 + 0x30);
}
/******************************************************************************* * Letter Count name : keypros * The functionality : Key handling functions , Judge button K1 Whether to press *******************************************************************************/
void keypros() // Initial page key detection
{
if(k1 == 0) // Switch mode
{
delay(1000); // Eliminate jitter It's about 10ms
if(k1==0) // Judge whether the key is pressed again
{
mode+= 1;if(mode == 3)mode = DISPLAYDATA;
}
while(k1 == 0);
}
else if(k2 == 0) // Buzzer test
{
delay(1000); // Eliminate jitter It's about 10ms
if(k2==0) // Judge whether the key is pressed again
{
beep = !beep;
}
while(k2 == 0);
}
else if(k3 == 0) // Backlight test
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3==0) // Judge whether the key is pressed again
{
lcdled = !lcdled;
}
while(k3 == 0);
}
else if(k4 == 0) // Backlight test
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4==0) // Judge whether the key is pressed again
{
alarmswitch=!alarmswitch;
}
while(k4 == 0);
}
}
/************* Modification time ************/
void modify(void)
{
static int time=0;
time++;
if(k1 == 0) // Switch mode
{
delay(1000); // Eliminate jitter It's about 10ms
if(k1==0) // Judge whether the key is pressed again
{
mode+= 1;if(mode == 3)mode = DISPLAYDATA;
}
while(k1 == 0);
}
else if(k2 == 0) // Select Modify parameter
{
delay(1000); // Eliminate jitter It's about 10ms
if(k2==0) // Judge whether the key is pressed again
{
ti++;
if(ti == 8)ti=0;
}
while(k2 == 0);
}
else if(k3 == 0 ||k4 == 0)
switch(ti) // Select enter to modify parameters
{
case 0:
if(k4==0 | k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4==0 | k3 ==0) // Judge whether the key is pressed again
{
TIME[0]=0;
}
while(k4 == 0 | k3==0);
}
break; //?
case 1:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[1]++;
if(TIME[1]%16 == 0x0a)
{
TIME[1] += 16;
TIME[1] &= 0xf0;
}if(TIME[1]==0x60)TIME[1]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[1]--;
if(TIME[1]%16==0x0f && TIME[1]!=0xff)
{
TIME[1] &= 0xf9;
}
if(TIME[1]==0xff)TIME[1]=0x59;
}
while(k4==0);
}
break; //?
case 2:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[2]++;
if(TIME[2]%16 == 0x0a)
{
TIME[2] += 16;
TIME[2] &= 0xf0;
}if(TIME[2]==0x24)TIME[2]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[2]--;
if(TIME[2]%16==0x0f && TIME[2]!=0xff)
{
TIME[2] &= 0xf9;
}
if(TIME[2]==0xff)TIME[2]=0x23;
}
while(k4==0);
}
break; //?
case 3:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[3]++;
if(TIME[3]%16 == 0x0a)
{
TIME[3] += 16;
TIME[3] &= 0xf0;
}if(TIME[3]==0x32)TIME[3]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[3]--;
if(TIME[3]%16==0x0f && TIME[3]!=0xff)
{
TIME[3] &= 0xf9;
}
if(TIME[3]==0xff)TIME[3]=0x31;
}
while(k4==0);
}
break; // Japan
case 4:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[4]++;
if(TIME[4]%16 == 0x0a)
{
TIME[4] += 16;
TIME[4] &= 0xf0;
}if(TIME[4]==0x13)TIME[4]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[4]--;
if(TIME[4]%16==0x0f && TIME[4]!=0xff)
{
TIME[4] &= 0xf9;
}
if(TIME[4]==0xff)TIME[4]=0x12;
}
while(k4==0);
}
break; // month
case 5:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[5]++;if(TIME[5]==7)TIME[5]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[5]--;if(TIME[5]==0xff)TIME[5]=6;
}
while(k4==0);
}
break; // Zhou
case 6:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
TIME[6]++;
if(TIME[6]%16 == 0x0a)
{
TIME[6] += 16;
TIME[6] &= 0xf0;
}if(TIME[6]==0xa0)TIME[6]=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
TIME[6]--;
if(TIME[6]%16==0x0f && TIME[6]!=0xff)
{
TIME[6] &= 0xf9;
}
if(TIME[6]==0xff)TIME[6]=0x99;
}
while(k4==0);
}
break; // year
case 7:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
mode=DISPLAYDATA;
ti=0;
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
Ds1302Init(); // Clock initialization
mode = DISPLAYDATA; // Return date
ti = 0; // Restore initial changes
}
while(k4==0);
}
break; // year
}
if(time == 200)
{
display_data();
if(ti == 7){
LcdWriteCom(0xca);
LcdWritestr(" <- OK");
}
}
else if(time == 400)
switch(ti) // Select enter to modify parameters
{
case 0:
LcdWriteCom(0xc7);
LcdWritestr(" ");
break;
case 1:
LcdWriteCom(0xc4);
LcdWritestr(" ");
break;
case 2:
LcdWriteCom(0xc1);
LcdWritestr(" ");
break;
case 3:
LcdWriteCom(0x88);
LcdWritestr(" ");
break;
case 4:
LcdWriteCom(0x85);
LcdWritestr(" ");
break;
case 5:
LcdWriteCom(0x8c);
LcdWritestr(" ");
break;
case 6:
LcdWriteCom(0x80);
LcdWritestr(" ");
break;
case 7:
LcdWriteCom(0xca);
LcdWritestr(" ");
break;
}else if(time>400) time=0;
delay_ms(1);
}
void setalarmclock(void) // Set the alarm mode
{
static int time=0;
time++;
if(k1 == 0) // Switch mode
{
delay(1000); // Eliminate jitter It's about 10ms
if(k1==0) // Judge whether the key is pressed again
{
mode+= 1;if(mode == 3)mode = DISPLAYDATA;
}
while(k1 == 0);
}
if(k2 == 0) // Select alarm clock to modify parameters
{
delay(1000); // Eliminate jitter It's about 10ms
if(k2==0) // Judge whether the key is pressed again
{
alarm++;
if(alarm == 3)alarm=0;
}
while(k2 == 0);
}
switch(alarm) // Select enter to modify parameters
{
case 0:
if(k3 == 0) // Control the alarm clock on
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3==0) // Judge whether the key is pressed again
{
alarmswitch = ON;
SectorErase(0x2401);
byte_write(0x2401,alarmswitch);
}
while(k3 == 0);
}
if(k4 == 0) // Control the alarm clock to turn off
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4==0) // Judge whether the key is pressed again
{
alarmswitch = OFF;
SectorErase(0x2401);
byte_write(0x2401,alarmswitch);
}
while(k4 == 0);
}
break;
case 1:
if(k3==0) // Control the alarm clock to increase
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
alarm_hour++;
if(alarm_hour%16 == 0x0a)
{
alarm_hour += 16;
alarm_hour &= 0xf0;
}if(alarm_hour==0x24)alarm_hour=0;
SectorErase(0x2601);
byte_write(0x2601,alarm_hour);
}
while(k3==0);
}
if(k4==0) // Control the alarm clock to decrease
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
alarm_hour--;
if(alarm_hour%16==0x0f && alarm_hour!=0xff)
{
alarm_hour &= 0xf9;
}
if(alarm_hour==0xff)alarm_hour=0x23;
SectorErase(0x2601);
byte_write(0x2601,alarm_hour);
}
while(k4==0);
}
break;
case 2:
if(k3==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k3 ==0) // Judge whether the key is pressed again
{
alarm_min++;
if(alarm_min%16 == 0x0a)
{
alarm_min += 16;
alarm_min &= 0xf0;
}if(alarm_min==0x60)alarm_min=0;
SectorErase(0x2201);
byte_write(0x2201,alarm_min);
}
while(k3==0);
}
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
alarm_min--;
if(alarm_min%16==0x0f && alarm_min!=0xff)
{
alarm_min &= 0xf9;
}
if(alarm_min==0xff)alarm_min=0x59;
SectorErase(0x2201);
byte_write(0x2201,alarm_min);
}
while(k4==0);
}
break;
}
if(time == 200)
{
alarm_hour=byte_read(0x2601);
alarm_min=byte_read(0x2201);
alarmswitch=byte_read(0x2401);
LcdWriteCom(0x80); // Show
LcdWritestr("alarm clock: ");
LcdWriteCom(0xc0);
if(alarmswitch == OFF)LcdWritestr(" OFF ");
else LcdWritestr(" ON ");
LcdWriteCom(0xc9);
LcdWriteData(alarm_hour/16+0x30);
LcdWriteData(alarm_hour%16+0x30);
LcdWriteData(':');
LcdWriteData(alarm_min/16+0x30);
LcdWriteData(alarm_min%16+0x30);
LcdWritestr(" ");
} else if(time == 400)
switch(alarm) // Select enter to modify parameters
{
case 0:
LcdWriteCom(0xc0);
LcdWritestr(" ");
break;
case 1:
LcdWriteCom(0xc9);
LcdWritestr(" ");
break;
case 2:
LcdWriteCom(0xcc);
LcdWritestr(" ");
break;
}else if(time>400) time=0;
delay_ms(1);
}
/************ Alarm mode *****************/
void alarmclock(void)
{
if(alarmswitch==ON && alarm_hour==TIME[2] && alarm_min==TIME[1]) // alarm clock
{
beep=1;
delay_ms(100);
beep=0;
delay_ms(100);
beep=1;
delay_ms(100);
beep=0;
LcdWriteCom(0x80);
LcdWritestr(" time out! ");
LcdWriteCom(0xc0);
LcdWritestr("now time: ");
LcdWriteData(alarm_hour/16+0x30);
LcdWriteData(alarm_hour%16+0x30);
LcdWriteData(':');
LcdWriteData(alarm_min/16+0x30);
LcdWriteData(alarm_min%16+0x30);
LcdWritestr(" ");
delay_ms(500);
LcdClean();
}
else mode=DISPLAYDATA;
if(k4==0)
{
delay(1000); // Eliminate jitter It's about 10ms
if(k4 ==0) // Judge whether the key is pressed again
{
alarmswitch=OFF;
}
while(k4==0);
}
}
/******************************************************************************* * Letter Count name : main * The functionality : The main function * transport Enter into : nothing * transport Out : nothing *******************************************************************************/
void main(void)
{
int ucount=0;
unsigned char lastSec;
beep= 0;
LcdInit(); //lcd initialization
//Ds1302Init(); // Clock initialization
Ds18b20Init(); // Temperature sensor initialization
SectorErase(0x2001);
// byte_write(0x2001,0x08); // Perform one initialization
// byte_write(0x2201,0x00);
// byte_write(0x2401,0x00);
alarm_hour=byte_read(0x2601);
alarm_min=byte_read(0x2201);
alarmswitch=byte_read(0x2401);
while(1)
{
switch(mode) // Mode selection
{
case DISPLAYDATA: // Time display mode
Ds1302ReadTime(); // Update time
if(TIME[0] != lastSec)
{
lastSec = TIME[0];
display_data(); // Display time Second minute hour Sun Moon anniversary
displaytemp(Ds18b20ReadTemp());// Show the temperature
if(alarmswitch==ON && alarm_hour==TIME[2] && alarm_min==TIME[1]) // alarm clock
{
mode = ALARMCLOCK;
}
}
keypros(); // Key detection
break;
case MODIFYDATA: // Time modification mode
modify();
break;
case SETALARMCLOCK: // Set the alarm mode
setalarmclock();
break;
case ALARMCLOCK: // Alarm mode
alarmclock();
break;
}
}
}
边栏推荐
- 加密的JS代码,变量名能破解还原吗?
- Chapter VIII (classes and objects)
- 批量修改文件名
- [postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc
- Mapping '/var/mobile/Library/Caches/com. apple. keyboards/images/tmp. gcyBAl37' failed: 'Invalid argume
- Chapter VI (pointer)
- Oracle 19C download installation steps
- Uniapp scrolling load (one page, multiple lists)
- xxl-job配置告警邮件通知
- Idea automatically sets author information and date
猜你喜欢

Project practice: parameters of pycharm configuration for credit card digital recognition and how to use opencv in Anaconda
![[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc](/img/c2/d1432ab6021ee9da310103cc42beb3.jpg)
[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc

Oracle 19C download installation steps

Chapter VI (pointer)

(5) Matrix key

Go语言浅拷贝与深拷贝

Double linked list -- tail interpolation construction (C language)

Example of offset voltage of operational amplifier

X-VLM多模态模型解读

RF filter
随机推荐
SOC的多核启动流程详解
Application of wireless charging receiving chip xs016 coffee mixing cup
2022 ranking of bank financial products
Flume learning notes
Use of jupyter notebook
Learning signal integrity from scratch (SIPI) -- 3 challenges faced by Si and Si based design methods
Microcontroller from entry to advanced
swift 代码实现方法调用
The difference between push-pull circuit drive and totem pole drive
optee中的timer代码导读
Chapter VI (pointer)
Batch modify file name
js文件报无效字符错误
Idea uses regular expressions for global substitution
Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
Interview JS and browser
Timer code guide in optee
[postgraduate entrance examination] group planning exercises: memory
2: String insert
JWT in go