#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
}
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();
}
}
Previous article:Serial port expansion parallel port drive data tube display
Next article:DS18B20+LCD1602 digital temperature sensor
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Bluetooth product FCC certification, RF testing
- [Popular Science] Do you know how to match signal source impedance and set various parameters?
- News: Qorvo, a well-known RF chip company, also makes power solutions
- Why "It's time to abandon the switch matrix for multi-port testing" is all in Keysight's white paper ~ Limited time gifts are waiting for you
- It’s the beginning of autumn. Check out the beginning of autumn postcards from all over the country.
- How computer memory is organized
- Usage of SoftI2C and SoftSPI
- [Raspberry Pi 4B Review] + System Installation and Environment Construction
- 4G low-power LTE wireless communication module data sharing suitable for camera development
- How does this thyristor voltage regulator circuit achieve short circuit protection?