Simulation program:
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
//*******LCD*******
sbit LCDRS=P2^0; //LCD data/command selection terminal
sbit LCDRW=P2^1;
sbit LCDEN=P2^2; //LCD enables the new terminal
//******DS1302******
sbit IO=P1^0; //1302 data line
sbit SCLK=P1^1; //1302 clock line
sbit RST=P1^2; //Reset
uint num=0;
//uchar week[]={"SUN","***","MON","TUS","WEN","THU","FRI","SAT"};
uchar Date[]={"Date: 2000-00-00 "};
uchar Time[]={"Time: *00:00-00* "};
uchar date_time[7]; //Current date and time read from ds1302
//=======Delay=======
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//********Write data to 1302*********
void write_ds1302(uchar dat)
{
flying i;
for(i=0;i<8;i++)
{
IO=dat & 0x01; //Keep the last bit as 1, read status
SCLK=1;
delay(1);
SCLK=0;
that >>= 1;
}
}
//************Read data from 1302*********
uchar read_ds1302()
{
fly i,b=0x00;
for(i=0;i<8;i++)
{
b |= _crol_((fly)IO,i);
SCLK=1;
delay(1);
SCLK=0;
}
return b/16*10+b%16; //Convert to BCD code
}
//************Read data from the specified location********
fly read_data(fly addr)
{
flying dat;
RST=0;
SCLK=0;
RST=1;
write_ds1302(addr);
dat=read_ds1302();
SCLK=1;
RST=0;
return that;
}
//************Read the current date and time*********
void read_date_time()
{
uchar i,addr=0x81;
for(i=0;i<7;i++)
{
date_time[i]=read_data(addr);
addr+=2;
}
}
//----------LCD write instruction----------
void lcd_write_com(uchar com)
{
LCDRS=0; //When RS is 0, write instruction, when RS is 1, write data
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
//----------LCD write data----------
void lcd_write_data(uchar dat)
{
LCDRS=1;
P0=that;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
//-------LCD initialization-------
void lcd_init()
{
LCDEN=0;
lcd_write_com(0x38); //LCD display mode setting
lcd_write_com(0x0c); //LCD display on/off and cursor setting
lcd_write_com(0x06); //After writing a character, the address pointer increases by 1, and the cursor increases by 1
lcd_write_com(0x01); //Clear the screen
}
//---------Set LCD display position-----------
void set_lcd_pos(uchar p)
{
lcd_write_com(p | 0x80);
}
//---------LCD display program----------
void lcd_print(uchar p,uchar *s)
{
set_lcd_pos(p);
for(num=0;num<16;num++)
{
lcd_write_data(s[num]);
delay(10);
}
}
//----------Date time conversion-----------
void format_datetime(string d, string *p)
{
p[0]=d/10+'0';
p[1]=d%10+'0';
}
//---------Main program--------------
void main()
{
LCDRW=0;
lcd_init();
while(1)
{
read_date_time();
format_datetime(date_time[6],Date+8); // Date
format_datetime(date_time[4],Date+11);
format_datetime(date_time[3],Date+14);
// strcpy(Date+13,week[date_time[5]]);
format_datetime(date_time[2],Time+7); //Time conversion
format_datetime(date_time[1],Time+10);
format_datetime(date_time[0],Time+13);
lcd_print(0x00,Date);
lcd_print(0x40,Time);
}
}
Previous article:51 single chip microcomputer c language --- delay
Next article:12864 LCD simple display program (with font library)
- 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
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [NUCLEO-L552ZE Review] Unboxing and Onboard Resource Analysis
- [Xianji HPM6750 Review] PWM Control Buzzer Sound
- Interface ov5640_camera
- 51 MCU 16_16 dot matrix example
- [NXP Rapid IoT Review] + How to import the project downloaded from WEB IDE into MCUXpresso IDE and debug it?
- EEWORLD University Hall----Engineering is smarter, industrial design is more powerful-field transmitter and smart meter design solution
- Startup interface kernel code modification
- DC regulated power supply to charge the battery
- The most comprehensive Fudan Micro MCU chip selection information on the entire network
- AWR1843BOOST mmw demo operation guide (Part 1)