Comprehensive example: 51 Single-Chip Microcomputer Perpetual calendar (temperature alarm clock year month day hour minute second week)

Source: Internet
Author: User

Function Description:

Lcd1602 LCD display year, month, day, hour, minute, second, week, temperature, three buttons control time and date adjustment and alarm settings.

 

Three buttons:

1. "school Hour" key, adjust the second, minute, hour, week, day, month, year, minute, hour, hour, and out of school when you press the button;

2. "Add/enable the alarm function", Press this key in school mode to add a value, press this key in non-school mode to display the alarm time and enable the alarm function (the screen displays the letter );

3. Press the "Reduce/Close alarm function" key to reduce the value by one, press this key in non-school mode to disable the alarm function (the screen letter A disappears );

 

 

# Include <reg52.h> typedef unsigned char uchar; typedef unsigned int uint; # define rs_h P3 | = 0x01 // The microcontroller outputs a high level to the LCD1602 LCD RS pin # define rs_l P3 & = 0xfe # define rw_h P3 | = 0x02 # define rw_l P3 & = 0xfd # define en_h P3 | = 0x04 # define en_l P3 & = 0xfb # define temp_h P3 | = 0x40 // Single Chip Microcomputer outputs high level to DS18B20 # define temp_l P3 & = 0xbf // single-chip microcomputer outputs Low Level to DS18B20 # define rst_h P3 | = 0x08 // single-chip microcomputer outputs high level to the RST pin of DS1302 clock chip # define rst_l P3 & = 0xf7 # define sck_h P3 | = 0x10 # define sck_l P3 & = 0xef # define io_h P3 | = 0x20 # define io_l P3 & = 0xdf # define SECADD 0X80 // second register address # define MINADD 0x82 # define HRADD 0x84 # define DATEADD 0x86 # define MONTHADD 0x88 # define DAYADD 0x8a # define YEARADD 0x8c # define CONTROLADD 0x8e # define PORT (P2) // P2 connects eight data pins of LCD1602 sbit time = P3 ^ 5; // DS1302 Input and Output pins sbit tmp = P3 ^ 6; // DS18B20 Input and Output pins void delayms (uint x); // delay function void LCD _com (uchar com); // LCD1602 LCD write command function void LCD _dat (uchar dat ); // LCD1602 Liquid Crystal Data Writing Function void LCD _write (uchar c, uchar r, uchar dat); // write the data function void LCD _init () to the specified row and column of the LCD1602 liquid crystal (); // LCD1602 initialize void Ds1302Init (); // DS1302 initialize void WriteByte (uchar dat); // write a uchar ReadByte () to DS1302 (); // read the ar BCDtoDex (uchar dat) byte from DS1302; // convert the BCD code to the decimal uchar DextoBCD (uchar dat); // convert the BCD code to void Ds1302Write (uchar add, uchar dat); // write data to the specified address of DS1302 uchar Ds1302Read (uchar add); // read the data void DS18B20 b20_reset () of the address specified by DS1302 (); // reset the temperature sensor void DS18B20 b20_write (uchar dat); // write a byte void showtem () to the DS18B20; // display the temperature of uchar DS18B20 b20_read (); // read a byte char keyscan () from DS18B20; // check the key. If a key is pressed, 1 is returned; otherwise, 0 uchar Timflag is returned; // 50 ms to uchar Temflag; // Let the "command DS18B20 conversion temperature" and "read temperature value" modules stagger the execution of uchar dat1, dat2; // read back temperature value, dat1 stores low bytes, dat2 saves the high byte uchar keystate = 2; // key status flag, initialized to 2, is to be able to enter the keystat1 branch when the program is running for the first time # define keystate0 1 // keystate0 in keyscan () in the function, it means to press the confirmation function # define keystate1 2 // keystate1 in the keyscan () function, it means to press uchar keynum without a key; // call the keyscan () function, if this function returns 1, it indicates that a key is pressed, and keynum saves the key value uchar keynum1 = 0; // indicates how many times uchar TimeShowEn = 1 is pressed by key 1; // set this variable to prevent the main function from refreshing the time to display uchar TimeChanged; // It indicates whether the user has modified the time sbit led1 = P0 ^ 0; // key 1 indicator light, press Key 1 This lamp changes state sbit led2 = P0 ^ 1; // key 2 indicator light, press Key 2 This lamp changes state sbit led3 = P0 ^ 2; // key 3 indicator light, press key 3 this indicator light changes the State sbit buzz = P1 ^ 0; // The Buzzer interface uchar AlarmEn, AlarmCome; // AlarmEn indicates whether the alarm function is enabled, alarmCome indicates whether the scheduled time has reached char AlarmMin and AlarmHr; // The uchar CountForAlarm parameter used to save the alarm minutes and the alarm hour; // this parameter is used to accumulate the time. When the time reaches, the buzzer interface level is flipped, determine the sound frequency of the buzzer. If the value of ToShowAlarm is 1, the alarm time module in the main function is allowed to execute uchar CountForShowAlarm; // CountForShowAlarm is used to accumulate the alarm display time .................. void Timer0 () interrupt 1 {TH0 = (65536-50000)/256; TL0 = (65536-50000) % 256; Timflag = 1; // if the value of Timflag is 1, the scheduled time for 50 ms is reached} void Ds1302Init () {rst_l; sck_l; Ds1302Write (CONTROLADD, 0); // disable write protection Ds1302Write (MINADD, dextoBCD (06); Ds1302Write (HRADD, DextoBCD (19); Ds1302Write (DATEADD, DextoBCD (20); Ds1302Write (MONTHADD, DextoBCD (5); Ds1302Write (DAYADD, dextoBCD (7); Ds1302Write (YEARADD, DextoBCD (12); Ds1302Write (SECADD, DextoBCD (30); // start the clock // Ds1302Write (CONTROLADD, 0x80); // write protection} void WriteByte (uchar dat) // output the rising edge {uchar I; for (I = 8; I> 0; I --) {sck_l; // if (dat & 0x01) io_h; else io_l; sck_h; dat >>=1 ;}}uchar BCDtoDex (uchar dat) // convert the BCD code to {uchar I; I = dat/16; dat % = 16; I = I * 10 + dat; return I;} uchar DextoBCD (uchar dat) // convert the BCD code to {uchar I; I = dat/10; dat % = 10; I = I * 16 + dat; return I;} uchar ReadByte () // dropped along the output {uchar I; uchar dat; for (I = 8; I> 0; I --) // consider the status after writing, the first time you enter this loop, there is a descent edge {dat >>=1; sck_l; if (time = 1) dat | = 0x80; sck_h ;}return dat ;} void Ds1302Write (uchar add, uchar dat) {rst_l; sck_l; rst_h; WriteByte (add); // a value of 0 indicates WriteByte (dat); rst_l ;} uchar Ds1302Read (uchar add) {uchar dat; rst_l; sck_l; rst_h; WriteByte (add + 1); // The lowest address position 1 indicates read dat = ReadByte (); rst_l; return dat;} void delayms (uint x) // delay 1 ms {uint y, z; for (y = x; y> 0; y --) for (z = 111; z> 0; z --);} void LCD _init () // LCD1602 initialization {LCD _com (0x38); LCD _com (0x0c); LCD _com (0x06 ); LCD _write (, 0xb0); // display-LCD _write (, 0xb0); // display-LCD _write (, 0x3a); // display: LCD _write (, 0x3a ); // display:} void LCD _com (uchar com) // write the command {rs_l; rw_l; PORT = com; delayms (1); en_h; delayms (1) To LCD1602 ); en_l;} void LCD _dat (uchar dat) // write data to LCD1602 {rs_h; rw_l; PORT = dat; delayms (1); en_h; delayms (1); en_l ;} void LCD _write (uchar c, uchar r, uchar dat) // specify rows, columns, and write data to LCD1602 {LCD _com (0x80 + 0x40 * c + r ); LCD _dat (dat );}..................

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.