51 MCU digital clock simulation (LCD1602 liquid crystal display) + source program + circuit schematic diagram

Publisher:开国古泉Latest update time:2020-08-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following is the 51 single-chip digital clock program:

#include

#define DSbus P0

#define LCDbus P1


//Define the control lines of DS12C887 and LCD

sbit DS_CS = P2^7; //Pin 13, chip select signal input, low level is valid.

sbit DS_AS = P2^4; //Pin 14, address select input.

sbit DS_RW = P2^5; //Pin 15, read/write input.

sbit DS_DS = P2^6; //Pin 17, data select or read input.

sbit LCD_RS=P2^0;

sbit LCD_EN=P2^2;

//Time variable definition

unsigned char Counter;

unsigned char Hour,Min,Sec,Year,Month,Date,Week;


/*DS12CR887 driver--------------------------------------------------------------------------*/

//Function to write data to DS12CR887

void DS12887write(unsigned char add,unsigned char Date)         

{

        DS_CS=0;

  DS_DS=1;

        DS_RW=1;

  DS_AS=1;

        DSbus=add;

        DS_AS=0;

  DS_RW=0;

        DSbus=Date;

        DS_RW=1;

  DS_AS=1;

        DS_CS=1;

}

//Read DS12CR887 data function

unsigned char DS12887read(unsigned char add)

{

        unsigned char z;

        DS_CS=0;

  DS_RW=1;

        DS_DS=1;

  DS_AS=1;

        DSbus=add;

        DS_AS=0;

  DS_DS=0;

  DSbus=0xff;

        z=DSbus;

        DS_DS=1;

  DS_AS=1;

        DS_CS=1;

        return z;

}

//DS12CR887 initialization function

void DS12887LCDinit()

{

        DS_AS=0; DS_DS=0; DS_RW=0;

        DS12887write(0x0a,0x20); //DS12CR887 register A function setting, turn on the clock oscillator

        DS12887write(0x0b,0x06); //Register B function setting, alarm interrupt enable is not enabled, data mode is binary, 24-hour mode.

        //DS12887write(4,0x8);DS12887write(2,0x00);DS12887write(0,0x00); //Assign the hours, minutes, and seconds to DS12CR887, and it will display 8:00:00 after booting

}

void Delay(unsigned int z)

{

        unsigned int x,y;

   for(x=z;x>0;x--)

         for(y=110;y>0;y--);

}

void LCDwritecom(unsigned char com)

{

        LCD_RS=0;

        LCDbus=com;

        Delay(5);

        LCD_EN=1;

        Delay(5);

        LCD_EN=0;

}

void LCDwritecomdata(unsigned char dat)

{

        LCD_RS=1;

        LCDbus=dat;

        Delay(5);

        LCD_EN=1;

        Delay(5);

        LCD_EN=0;

}

void LCDinit()

{

        LCD_EN=0;

        LCDwritecom(0x38);

        LCDwritecom(0x0c);

        LCDwritecom(0x06);

        LCDwritecom(0x01);

        LCDwritecom(0x80);

}

void Timedisplay(void)

{

        //LCDwritecom(1);

        LCDwritecom(0x80);

//Fill in the "hour" data on the LCD screen-----------------------------------------------

        Hour=DS12887read(4); //Read the hour data of DS12CR887

        if((Hour/10)==0)LCDwritecomdata(0);

                else LCDwritecomdata(Hour/10+0x30); //Tens of hours

        LCDwritecomdata(Hour%10+0x30); //Hour digit

        LCDwritecomdata(':'); //clock separator:"

//Fill in the "minute" data on the LCD screen-----------------------------------------------

        Min=DS12887read(2); //Read the sub-data of DS12CR887

        LCDwritecomdata(Min/10+0x30);

        LCDwritecomdata(Min%10+0x30);

        LCDwritecomdata(':'); //clock separator:"                               

//Fill in the "seconds" data on the LCD screen-------------------------------------------------

        Sec=DS12887read(0); //Read the second data of DS12CR887

        LCDwritecomdata(Sec/10+0x30);

        LCDwritecomdata(Sec%10+0x30);

        Delay(100);                               

}

void Datedisplay(void)

{

        //LCDwritecom(1);

        LCDwritecom(0xc0);

//Fill in the "year" data on the LCD screen-----------------------------------------------

        LCDwritecomdata('2');

        LCDwritecomdata('0');

        Year=DS12887read(9); //Read the year data of DS12CR887

       

        LCDwritecomdata(Year/10+0x30); //Year 10 digits

        LCDwritecomdata(Year%10+0x30); //Year digit

        LCDwritecomdata('/'); //clock separator:"

//Fill in the "month" data on the LCD screen-----------------------------------------------

        Month=DS12887read(8); //Read the monthly data of DS12CR887

        LCDwritecomdata(Month/10+0x30);

        LCDwritecomdata(Month%10+0x30);

        LCDwritecomdata('/'); //clock separator:"                               

//Fill in the "day" data on the LCD screen-------------------------------------------------

        Date=DS12887read(7); //Read the daily data of DS12CR887

        LCDwritecomdata(Date/10+0x30);

        LCDwritecomdata(Date%10+0x30);

//Fill in the "week" data on the LCD screen-------------------------------------------------

        Week=DS12887read(6); //Read daily data from DS12CR887

        LCDwritecomdata(0);

        LCDwritecomdata(Week-1+0x30);

        Delay(100);                               

}

void main()

{

//        unsigned char i;

        LCDinit();

        DS12887LCDinit();

  DS12887write(0x0a,0x00); //Start timing, DS12CR887 turns off the clock oscillator

        DS12887write(0,55); //seconds

        DS12887write(2,59);

        DS12887write(4,23);

  DS12887write(6,5); //week

        DS12887write(7,22);  //日

        DS12887write(8,9);  //

        DS12887write(9,12);  //

        //display_Date();

        while(1)

        {

                Timedisplay();

          Datedisplay();

                Delay(100);

        }

}


Reference address:51 MCU digital clock simulation (LCD1602 liquid crystal display) + source program + circuit schematic diagram

Previous article:51 MCU software SPI initialization ILI9488-DPI mode program
Next article:51 single chip electronic calendar (adjustable with temperature) DS1302 clock + 12864 LCD display

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号