AT89C2051+LCD1602+DS1302 Real-time Clock Design (c51)

Publisher:WhisperingWaveLatest update time:2016-09-29 Source: eefocusKeywords:AT89C2051 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include
#define uchar unsigned char
sbit rs=P3^0;//LCD1602控制脚
sbit rw=P3^1;
sbit e=P3^2;

sbit io=P3^4; //DS1302 PCI bus pin
sbit rst=P3^5;
sbit sclk=P3^3;

uchar hour,minute,second,year,months,date,day; //Display time register
uchar whour,wminute,wsecond,wyear,wmonths,wdate,wday; //Set initial time register
uchar code table1[]="0123456789-:w";
uchar code table2[]="Date:20";
uchar code table3[]="Time:";

void delayms(uchar k)//1ms延时
{
 uchar j,i;
 for(i=0;i   for(j=0;j<120;j++); 
}

void delay2us() //2us delay
{
 _nop_();
 _nop_();
}

void write1602_data(uchar dat)//1602 write data subroutine
{
 rs=1;
 rw=0;
 P1=dat;
 delayms(2);
 e=0;
 e=1;
 e=0; 
}

void write1602_com(uchar com) //1602 write command subroutine
//
{
 rs=0;
 rw=0;
 P1=com;
 delayms(5);
 e=0;
 e=1;
 e=0;
}

void init1602() //1602 initialization subroutine
{
 write1602_com(0x38); //lcd1602 16*2 display, 5*7 dot matrix, 8bit data interface
 write1602_com(0x0f); //turn on the display, display the cursor, and the cursor flashes
 write1602_com(0x06); //When a character is written, the address pointer increases by one, and the cursor increases by one
 write1602_com(0x01); //clear the screen
}

void disptop() //1602 first line display subroutine
{
 uchar i;
 write1602_com(0x80);
 for(i=0;i<0x07;i++)
 {
  write1602_data(table2[i]);
  delayms(1);
 }
}

void dispbot() //1602 second line display subroutine
{
 uchar i;
 write1602_com(0xc0);
 for(i=0;i<0x05;i++)
 {
  write1602_data(table3[i]);
  delayms(1);
 }
}

/*******************************************************/

void write1302byte(uchar dat)//1302 writes a byte Subroutine
{
 uchar i;
 sclk=0;
 for(i=0;i<8;i++)
 {
  io=(bit)(dat&0x01);
  delay2us();
  sclk=1;
  delay2us();
  sclk=0;
  dat=dat>>1;
 } 
}

uchar read1302byte() //1302 reads a byte subroutine
{
 uchar i,dat;
 for(i=0;i<8;i++)
 {
  dat>>=1;
  if(io)
  dat|=0x80;
  delay2us();
  sclk=1;
  delay2us();
  sclk=0;
  delay2us();  
 }
 return dat;  
}

void writeset1302(uchar add,uchar dat)//1302 read child program
{
 rst=0;
 delay2us();
 rst=1;            
 write1302byte(add);
 write1302byte(dat);
 rst=0;
}

uchar readset1302(uchar add)//1302写子电影
{
 uchar dat;
 rst=0;
 delay2us();
 rst=1;
 write1302byte(add);
 dat=read1302byte();
 rst=0;
 return dat;
}

void init1302() //1302 initialization subroutine
{
 uchar flag;
 flag=readset1302(0x81); //Judge whether 1302 has a backup battery, if yes, FLAG is 0
 if(flag&0x80)
 {
  writeset1302(0x8e,0x00);
  writeset1302(0x80,((wsecond/10)<<4|(wsecond%10)));
  writeset1302(0x82,((wminute/10)<<4|(wminute%10)));
  writeset1302(0x84,((whour/10)<<4|(whour%10)));
  writeset1302(0x86,((wday/10)<<4|(wday%10)));
  writeset1302(0x88,((wmonths/10)<<4|(wmonths%10)));
  writeset1302(0x8a,((wdate/10)<<4|(wdate%10)));
  writeset1302(0x8c,((wyear/10)<<4|(wyear%10)));
  writeset1302(0x90,0xa5);
  1302(0x8e,0x80);
 } 
}

uchar readvalue(uchar value) //data conversion
{
 uchar a;
 a=((value&0x70)>>4)*10+(value&0x0f);
 return a; 
}

void read1302()    //读取时间
{
 uchar value;
 value=readset1302(0x81);
 second=readvalue(value);
 
 value=readset1302(0x83);
 minute=readvalue(value);
 
 value=readset1302(0x85);
 hour=readvalue(value);
 
 value=readset1302(0x87);
 day=readvalue(value);
 
 value=readset1302(0x89);
 months=readvalue(value);

 value=readset1302(0x8b);
 date=readvalue(value);

 value=readset1302(0x8d);
 year=((value&0xf0)>>4)*10+(value&0x0f);

}

void disptime()   //时间显示
{
 write1602_com(0x87);
 write1602_data(table1[year/10]);
 write1602_data(table1[year%10]);
 write1602_data(table1[10]);
 write1602_data(table1[months/10]);
 write1602_data(table1[months%10]);
 write1602_data(table1[10]);
 write1602_data(table1[day/10]);
 write1602_data(table1[day%10]);
 write1602_com(0xc5);
 write1602_data(table1[hour/10]);
 write1602_data(table1[hour%10]);
 write1602_data(table1[11]);
 write1602_data(table1[minute/10]);
 write1602_data(table1[minute%10]);
 write1602_data(table1[11]);
 write1602_data(table1[second/10]);
 write1602_data(table1[second%10]);
 write1602_com(0xce);
 write1602_data(table1[12]);
 write1602_data(table1[date]);
}

void inittime() //initial time setting
{
 whour=23;
 wminute=59;
 wsecond=20;
 wyear=10;
 wmonths=12;
 wdate=3;
 wday=1;
}

void main()
{
    inittime();
 init1602();
 disptop();
 dispbot();
 init1302();
 while(1)
 {
  read1302();
  disptime();
 }
}

Keywords:AT89C2051 Reference address:AT89C2051+LCD1602+DS1302 Real-time Clock Design (c51)

Previous article:Serial port expansion parallel port drive data tube display
Next article:DS18B20+LCD1602 digital temperature sensor

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号