"#include 'reg52. H' / / header file containing MCU registers
#Include 'intrins. H' / / include_ nop_() Header file for
sbit RS=P2^0; // LCD read / write selection bit
sbit RW=P2^1; // LCD read / write selection bit
sbit E=P2^2; // LCD enable terminal
sbit BF=P0^7; // Busy signal
sbit SCLK=P1^0; // 1302 clock output
sbit DATA=P1^1; // 1302 data terminal
sbit RST=P1^2; // 1302 reset end
unsigned char code digit[]=“0123456789”;
void delay1ms(unsigned int n)
{
unsigned char i;
while(n--)
for(i=0; i《115; i++);
}
void Write1302(unsigned char dat)
{
unsigned char i;
SCLK=0; // Make sure SCLK is low before writing data
_ nop_();
_ nop_(); // Give hardware response time
for(i=0; i《8; i++){
DATA=dat&0x01; // Take the lowest bit of data, the lowest bit is in the front, and the highest bit is in the back
_ nop_();
_ nop_(); // Give hardware response time
SCLK=1; // Forward jump write data
_ nop_();
_ nop_(); // Give hardware response time
SCLK=0; // Lower SCLK again to form a pulse
dat》》=1; // The dat shifts to the right in preparation for writing lower data
}
}
void WriteSet1302(unsigned char cmd, unsigned char dat)
{
RST=0; // Data transmission is prohibited.!!! This is very important
SCLK=0; // Make sure SCLK is low before writing data
RST=1; // Turn on data transmission
_ nop_();
_ nop_();
Write1302(cmd);
Write1302(dat);
SCLK=1; // Set the clock level to the high-level state and be in the known state
RST=0;
}
unsigned char Read1302()
{
unsigned char i,dat=0;
for(i=0; i《8; i++){
dat》》=1;
If (data = = 1) / / if the read data is 1
dat|=0x80; // Take out 1 and write it in the highest bit of dat
SCLK=1; // Set SCLK to high level to read the falling edge
_ nop_();
_ nop_();
SCLK=0; // Pull down SCLK to form the falling edge of pulse
}
return dat;
}
unsigned char ReadSet1302(unsigned char cmd)
{
unsigned char dat;
RST=0; // This is very important
SCLK=0; // Ensure that the number of writes is the highest and SCLK is pulled down
RST=1; // Start data transfer
_ nop_();
_ nop_();
Write1302(cmd); // Write command word
dat=Read1302(); // Read data
SCLK=1; // Set the clock level to a known state
RST=0; // Prohibit data transfer
return dat; // Return the read data
}
void DS1302_ Int()
{
WriteSet1302(0x8E,0x00); // Write the unprotected instruction according to the write status register command word
WriteSet1302(0x80,((55/10)《《4|(55))); // Write the initial value of seconds according to the write second register command word
WriteSet1302(0x82,((59/10)《《4|(59))); // Write the initial value of minute according to the command word of write minute register
WriteSet1302(0x84,((23/10)《《4|(23))); // Write the initial value of the hour according to the write hour register command word
WriteSet1302(0x86,((18/10)《《4|(18))); // Write the initial value of the day according to the write day register command word
WriteSet1302(0x88,((6/10)《《4|(6))); // Write the initial value of the month according to the write month register command word
WriteSet1302(0x8c,((9/10)《《4|(9))); // Write the initial value of the year according to the write year register command word
WriteSet1302(0x90,0xa5); // Turn on the charging function and select the 2K resistance charging mode
WriteSet1302(0x8E,0x80); // Write the protection instruction according to the write status register command word
}
bit BusyTest()
{
bit result;
RS=0; // According to the regulations, RS is low level and RW is high level, which can be read
RW=1;
E=1;
_ nop_();
_ nop_();
result=BF; // Assign the busy flag level to result
E=0; // Restore e to low level
_ nop_();
_ nop_();
return result;
}
void WriteInstruction(unsigned char dictate)
{
while(BusyTest()==1); // Wait if you are busy
RS=0; // According to the regulations, RS and R / W are at low level at the same time, and instructions can be written
RW=0;
E=0; // E is set to low level (when writing instruction, e is high pulse,
//Is to make e jump from 0 to 1, so "0" should be set first
_ nop_();
_ nop_();
P0=dictate;
_ nop_();
_ nop_();
_ nop_();
_ nop_();
E=1; // E set high level
_ nop_();
_ nop_();
_ nop_();
_ nop_();
E=0; // When e jumps from high level to low level, the LCD module starts to execute the command
}
void WriteAddress(unsigned char x)
{
WriteInstrucTIon(x|0x80); // The method for determining the display position is specified as "80h + address code X"
}
void WriteData(unsigned char dat)
{
while(BusyTest()==1); // Wait if you are busy
RS=1; // RS is high level and RW is low level. Data can be written
RW=0;
E=0; // E is set to low level (when writing instruction, e is high pulse,
//Is to make e jump from 0 to 1, so "0" should be set first
_ nop_();
_ nop_();
P0=dat; // Send the data to port P0, that is, write the data into the LCD module
_ nop_();
_ nop_();
_ nop_();
_ nop_();
E=1; // E set high level
_ nop_();
_ nop_();
_ nop_();
_ nop_();
E=0; // When e jumps from high level to low level, the LCD module starts to execute the command
}
void LCD_ Int()
{
delay1ms(15); // The delay is 15ms, and the LCD shall be given a long response time when writing instructions for the first time
WriteInstrucTIon(0x38); // Display mode setting: 16 × 2 display, 5 × 7-dot matrix, 8-bit data interface
delay1ms(5);
WriteInstrucTIon(0x38);
delay1ms(5);
WriteInstrucTIon(0x38);
delay1ms(5); // Three times in succession to ensure successful initialization
WriteInstruction(0x0c); // Display mode setting: the display is on, there is no cursor, and the cursor does not flash
delay1ms(5);
WriteInstruction(0x06); // Display mode setting: the cursor moves to the right and the character does not move
delay1ms(5);
WriteInstruction(0x01); // Clear screen command to clear the previous display
delay1ms(5);
}
void DisplaySecond(unsigned char x)
{
unsigned char i,j; // i. J stores ten bits and one bit respectively
i=x/10;
j=x;
WriteAddress(0x49);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayMinute(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x46);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayHour(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x43);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayDay(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x0c);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayMonth(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x09);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void DisplayYear(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x;
WriteAddress(0x06);
WriteData(digit[i]);
WriteData(digit[j]);
delay1ms(15);
}
void main()
{
unsigned char second,minute,hour,day,month,year; // Store seedlings, minutes, hours, days, months and years respectively
unsigned char ReadValue; // Store data read from 1302
LCD_ Int(); // Initialize the LCD
WriteAddress(0x01); // Write the display address of date, which will be displayed from line 1 to column 2
WriteData(‘D’);
WriteData(‘a’);
WriteData(‘t’);
WriteData(‘e’);
WriteData(‘:’);
WriteAddress(0x08);
WriteData(‘-’);
WriteAddress(0x0b);
WriteData(‘-’);
WriteAddress(0x45);
WriteData(‘:’);
WriteAddress(0x48);
WriteData(‘:’);
DS1302_ Int(); // Initialize 1302
while(1){
ReadValue = ReadSet1302(0x81); // Send from seconds
Our other product: