PIC microcontroller (PIC16F877A) DS1302 chip Program

Source: Internet
Author: User
# Include <pic. h> typedef unsigned char uchar; typedef unsigned int uint; # define rs_h PORTC | = 0x01 # define rs_l PORTC & = 0xfe # define rw_h PORTC | = 0x02 # define rw_l PORTC & = 0xfd # define en_h PORTC | = 0x04 # define en_l PORTC & = 0xfb # define rst_h PORTC | = 0x08 # define rst_l PORTC & = 0xf7 # define sck_h PORTC | = 0x10 # define sck_l PORTC & = 0xef # define io_h PORTC | = 0x20 # define io_l PORTC & = 0xdf # define SECADD 0X80/S 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 (PORTB) void delayms (uint x); void LCD _com (uchar com); void LCD _dat (uchar dat); void LCD _write (uchar c, uchar r, uchar dat); void LCD _init (void ); void Ds1302Init (void); void WriteByte (uchar dat); uchar ReadByte (void); uchar BCDtoDex (uchar dat); uchar DextoBCD (uchar dat); uchar ReadByte (void ); void Ds1302Write (uchar add, uchar dat); uchar Ds1302Read (uchar add); void main (void) {uchar sec, min, hr, date, month, day, year; TRISB = 0x00; TRISC & = 0xc0; LCD _init (); Ds1302Init (); while (1) {sec = Ds1302Read (SECADD); min = Ds1302Read (MINADD ); hr = Ds1302Read (HRADD); date = Ds1302Read (DATEADD); month = Ds1302Read (MONTHADD); day = Ds1302Read (DAYADD); year = Ds1302Read (YEARADD ); sec = BCDtoDex (sec); min = BCDtoDex (min); hr = BCDtoDex (hr); date = BCDtoDex (date); month = BCDtoDex (month ); day = BCDtoDex (day); year = BCDtoDex (year); LCD _write (, 0x32); LCD _write (, 0x30); LCD _write, 0x30 + year/10); LCD _write (, 0x30 + year % 10); LCD _write (, 0x30 + month/10); LCD _write, 0x30 + month % 10); LCD _write (, 0x30 + date/10); LCD _write (, 0x30 + date % 10); LCD _write, 0x30 + day); LCD _write (, 0x30 + hr/10); LCD _write (, 0x30 + hr % 10); LCD _write, 0x30 + min/10); LCD _write (, 0x30 + min % 10); LCD _write (, 0x30 + sec/10); LCD _write (, 11, 0x30 + sec % 10); delayms (1005); // 1 ms} void Ds1302Init (void) {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 )); // write the second and start the clock at the same time} void WriteByte (uchar dat) // output the rising edge {uchar I; TRISC & = 0xdf; for (I = 8; I> 0; I --) {sck_l; // if (dat & 0x01) io_h; elseio_l; sck_h; dat >>=1 ;}} uchar BCDtoDex (uchar dat) // convert the BCD code to the decimal {uchar I; I = dat/16; dat % = 16; I = I * 10 + dat; return I ;} uchar DextoBCD (uchar dat) // convert the code to BCD in decimal format {uchar I; I = dat/10; dat % = 10; I = I * 16 + dat; return I ;} uchar ReadByte (void) // the output of the descent edge {uchar I; uchar dat; TRISC | = 0x20; 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 (PORTC & 0x20) dat | = 0x80; sck_h;} return dat;} void Ds1302Write (uchar add, uchar dat) {rst_l; sck_l; rst_h; WriteByte (add ); // The lowest position 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) // 1 ms {uint y, z; for (y = x; y> 0; y --) for (z = 220; z> 0; z --);} void LCD _init (void) // LCD1602 initialization {LCD _com (0x38); LCD _com (0x0c); LCD _com (0x06); LCD _write (, 0xb0); LCD _write (, 0xb0 ); LCD _write (, 0x3a); LCD _write (, 0x3a);} void LCD _com (uchar com) // write the command {rs_l; rw_l; PORT = com to LCD1602; delayms (1); en_h; delayms (1); 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); delayms (1 );}


 

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.