#include
Typedef unsigned char uint8;
Typedef unsigned int uint16;
Sbit SDA=P1 ^ 5;//data
Sbit SCK=P1 ^ 4;//the clock
Sbit RST P1=^ 3;//DS1302 reset (optional)
Uint8 time [7]=,0,0,0,0,0,0 {0};//second anniversary of the 10-08-15 23:59:50 time-sharing runs 7 representative Sunday
# define DS1302_W_ADDR 0 x80
# define DS1302_R_ADDR 0 x81
P1M0=0 x10. P1M1=0 x00;
Void delay (uint16 n)
{
While (n -);
}
Void ds1302_reset ()//to SCK=0; RST=1
{
RST=0;//stop data transfer
SCK=0;//the clock bus reset
Delay (10);
RST=1;//data operations
}
//write a byte
Void ds1302_write_byte uint8 (dat)
{
Uint8 I;
for(i=0; I<8; I++)
{
SDA=dat & amp; 0 x01;
SCK=1;
Delay (20);
SCK=0;
Dat>=1;
Delay (20);
}
}
//read a byte
//for data output: the start of the eight SCLK cycle, enter a command byte read, eight data bytes in the falling edge of the SCLK cycle output
//note that the first falling edge data bytes of the first, the last of the command word is written
//(Note that the first data bit to be transmitted occurs on the first falling edge after the last bit of the command byte is written.),
//and final one byte command byte is written after the falling edge makes the first data bits are read
Uint8 ds1302_read_byte ()
{
Uint8 I, dat=0;//dat stored data read
for(i=0; I<8; I++)
{
Dat>=1;
If (SDA==1)
Dat=dat | 0 x80;//because from low to start reading
SCK=1;
Delay (20);
SCK=0;//the above explanation: can know the first thing we should judge the if (SDA==1), and then to SCK=1, SCK=0
Delay (20);
}
Return dat.
}
//remove write protection
Void ds1302_clear_WP ()
{
Ds1302_reset ();
RST=1;
Ds1302_write_byte (0 x8e);//write address
Ds1302_write_byte (0 x00);//write data
After the//write
SDA=0;
RST=0;
}
//set the write protect
Void ds1302_set_WP ()
{
Ds1302_reset ();
RST=1;
Ds1302_write_byte (0 x8e);//write address
Ds1302_write_byte (0 x80);//write data
After the//write
SDA=0;
RST=0;
}
//write ds1302
Void ds1302_write (uint8 addr, uint8 dat)
{
Ds1302_reset ();
RST=1;
Ds1302_write_byte (addr);//write address
Ds1302_write_byte (dat);//write data
After the//write
RST=0;
SDA=0;
}
//the data read from the ds1302
Uint8 ds1302_read (uint8 addr)
{
Uint8 temp=0;//store the data read
Ds1302_reset ();
RST=1;
Ds1302_write_byte (addr);//write address
Temp=ds1302_read_byte ();//write data
After the//write
RST=0;
SDA=0;
Return temp.
}
/* *
* set the clock data
Involving binary and BCD conversion because ds1302 BCD, but machine only know binary
For example: in the ds1302 for 50, 0101, 0000 then we write is ds1302_write (80)
*/
Void set_time (uint8 * timedata)
{
Uint8 I;
Uint8 temp=0;
for(i=0; I<7. I++)
{
Temp=timedata [I]/10;
Timedata [I]=timedata [I] % 10;
Timedata [I]=timedata [I] + temp * 16;
}
Ds1302_clear_WP ();//the ds1302 operation before the wp bit reset
Temp=DS1302_W_ADDR;//write starting position
for(i=0; I<7. I++)
{
Ds1302_write (temp, timedata [I]);
Temp +=2;
}
Ds1302_set_WP ();
}
/* *
* read clock data format (BCD)
*/
Void read_time (uint8 * timedata)
{
Uint8 I, temp.
Temp=DS1302_R_ADDR;//read starting position
for(i=0; I<7. I++)
{
Timedata [I]=ds1302_read (temp);
Temp +=2;
}
}
/* UART initialization
* baud rate: 9600
*/
Void uart_init (void)
{
SCON=0x50;
AUXR |=0 x40;
AUXR & amp;=0 xfe;
TMOD & amp;=0 x0f;
TMOD |=0 x20;
TL1=0 XDC;
TH1=0 XDC;
ET1=0;
TR1=1;
}
/* *
* UART send a byte
*/
Void UART_Send_Byte (uint8 dat)
{
SBUF=dat;
While (TI==0);
TI=0;
}
/* *
* to convert data to ASC code and through the UART send
*/
Void UART_Send_Dat uint8 (dat)//attention is about more than 16
{
UART_Send_Byte (dat/16 + '0');
UART_Send_Byte (dat 16 + % '0');
}
Void main ()
{
Uart_init ();
Set_time (& amp; Time);//set the time value
While (1)
{
Read_time (& amp; Time);//second anniversary of the time-sharing runs
UART_Send_Dat (time [6]);
UART_Send_Byte (' - ');
UART_Send_Dat (time [4]);
UART_Send_Byte (' - ');
UART_Send_Dat (time [3]);
UART_Send_Byte (");
UART_Send_Dat (time [2]);
UART_Send_Byte (', ');
UART_Send_Dat (time [1]);
UART_Send_Byte (', ');
UART_Send_Dat (time [0]);
UART_Send_Byte (' \ r ');
UART_Send_Byte (' \ n ');
Delay (100000);
}
}
SCM is STC15W4K56A4, card day also could not find the reason, bosses look thanks
CodePudding user response:
Reference: https://blog.csdn.net/qq_29246181/article/details/105474102