S7-200 and four LED digital tubes are used to design the circuit and control program for displaying hours and minutes.
# include & ltintrins.h & gt
sbit sck=p3^6; //clock
sbit sda=p3^4; //data
sbit rst = p3^5; // DS 1302 reset
sbit ls 138a=p2^2;
sbit ls 138b=p2^3;
sbit ls 138c=p2^4;
Bit ReadRTC _ Flag// defines the read DS 1302 flag.
Unsigned character l _ tmpdate [7]; //={0,0, 13, 15,5,3,8}; //Every minute of the sun and the moon commemorates 08-05- 15 12:00:00.
Unsigned character l _ tmpdisplay [8];
code unsigned char write _ RTC _ address[7]= { 0x 80,0x82,0x84,0x86,0x88,0x8a,0x8c }; //Minimum reading and writing bit of the month, year and year, in seconds.
code unsigned char read _ RTC _ address[7]= { 0x 8 1,0x83,0x85,0x87,0x89,0x8b,0x8d };
Code unsigned char table[]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40};
//* * * Digital tube 0-9 '-' off' table
/******************************************************************/
/* Function declaration */
/******************************************************************/
Void Write_Ds 1302_byte (unsigned chartemp);
Void Write_Ds 1302 (unsigned character address, unsigned character dat);
Unsigned character Read_Ds 1302 (unsigned character address);
void Read _ RTC(void); //Read RTC
void Set _ RTC(void); //Set RTC
void inittimer 0(void); //Initial Timer 0
/******************************************************************/
/* Main function */
/******************************************************************/
Invalid master (invalid)
{
init timer 0(); //Initialize Timer 0
set _ RTC(); //Write the clock value. If you use a spare battery, you don't need to write it every time you turn it on. This program should be blocked.
while( 1)
{
If (readrtc _ flag)//readrtc _ flag = =1execute the following statement.
{
read RTC _ Flag = 0;
read _ RTC();
l _ tmpdisplay[0]= l _ tmpdate[2]/ 16; //data conversion, because we use the display of digital tubes 0~9 to separate data.
l _ tmpdisplay[ 1]= l _ tmpdate[2]& amp; 0x0f
l _ tmpdisplay[2]= 10; //Add "-"
l _ tmpdisplay[3]= l _ tmpdate[ 1]/ 16;
l _ tmpdisplay[4]= l _ tmpdate[ 1]& amp; 0x0f
l _ tmpdisplay[5]= 10;
l _ tmpdisplay[6]= l _ tmpdate[0]/ 16;
l _ tmpdisplay[7]= l _ tmpdate[0]& amp; 0x0f
}
}
}
/******************************************************************/
/* Timer 0 initialization */
/******************************************************************/
void InitTIMER0(void)
{
TMOD | = 0x 0 1; //Timer setting 16 bits
TH0 = 0xef// initialization value
TL0 = 0xf0
ET0 = 1;
TR0 = 1;
EA = 1;
}
/******************************************************************/
/* Write a byte */
/******************************************************************/
Void Write_Ds 1302_Byte (unsigned character temperature)
{
Unsigned character I;
for(I = 0; I<8; I++)// Write data 8 times.
{
SCK = 0;
SDA = temp & amp0x 0 1; //Low number of bytes per transmission
temp & gt& gt= 1; //Move one position to the right
SCK = 1;
}
}
/******************************************************************/
/* write to DS 1302 */
/******************************************************************/
Void Write_Ds 1302 (unsigned character address, unsigned character data)
{
RST = 0;
_ nop _();
SCK = 0;
_ nop _();
RST = 1;
_ nop _(); //Start
Write_Ds 1302_Byte (address); //Send address
write _ ds 1302 _ Byte(dat); //Send data
RST = 0; //resume
}
/******************************************************************/
/* read data of DS 1302 */
/******************************************************************/
Unsigned character Read_Ds 1302 (unsigned character address)
{
Unsigned character I, temp = 0x00
RST = 0;
_ nop _();
_ nop _();
SCK = 0;
_ nop _();
_ nop _();
RST = 1;
_ nop _();
_ nop _();
Write_Ds 1302_Byte (address);
for(I = 0; I<8; I++)// Read data 8 times.
{
Intermediate frequency (SDA)
Temp | = 0x80// Number of low bytes per transmission
SCK = 0;
temp & gt& gt= 1; //Move one position to the right
_ nop _();
_ nop _();
_ nop _();
SCK = 1;
}
RST = 0;
_ nop _(); //The following is the setup time of DS 1302 reset.
_ nop _();
RST = 0;
SCK = 0;
_ nop _();
_ nop _();
_ nop _();
_ nop _();
SCK = 1;
_ nop _();
_ nop _();
SDA = 0;
_ nop _();
_ nop _();
SDA = 1;
_ nop _();
_ nop _();
Return (temporary); //Return
}
/******************************************************************/
/* Read clock data */
/******************************************************************/
Void Read _ RTC(void)// Read the calendar
{
Unsigned character I, * p;;
P = read _ rtc _ address; //address delivery
for(I = 0; I<7; I++)// Minute 7 Readings Seconds Minute Sun Moon Anniversary
{
l _ tmpdate[I]= Read _ ds 1302(* p); //Write the number of l_tmpdate[i] to the address Read_Ds 1302.
p++;
}
}
/******************************************************************/
/* Set clock data */
/******************************************************************/
Void Set _ RTC(void)// Set the calendar.
{
Unsigned char i, *p, tmp
for(I = 0; I<7; I++){ //BCD processing
tmp = l _ tmpdate[I]/ 10;
l _ tmpdate[I]= l _ tmpdate[I]% 10;
l _ tmpdate[I]= l _ tmpdate[I]+tmp * 16;
}
Write_Ds 1302(0x8E,0x 00);
P = write _ rtc _ address; //address
for(I = 0; I<7; I++)// write 7 times a second, the anniversary of the sun and the moon.
{
Write_Ds 1302(*p,l _ tmpdate[I]);
p++;
}
Write_Ds 1302(0x8E,0x 80);
}
/******************************************************************/
/* timer interrupt function */
/******************************************************************/
Void tim(void) interrupt 1 use 1
//Interrupt, used for digital tube scanning.
{
Static unsigned character I, num
TH0 = 0xf5
TL0 = 0xe0
P0 = table[l _ tmp display[I]]; //Look-up table method gets the number segment of the number to be displayed.
Switch (1)
{
Case 0: ls138a = 0; ls 138 b = 0; ls 138 c = 0; Break;
Case1:ls138a =1; ls 138 b = 0; ls 138 c = 0; Break;
Case 2: ls138 a = 0; ls 138 b = 1; ls 138 c = 0; Break;
Case 3: ls138 a =1; ls 138 b = 1; ls 138 c = 0; Break;
Case 4: ls138 a = 0; ls 138 b = 0; ls 138 c = 1; Break;
Case 5: ls138 a =1; ls 138 b = 0; ls 138 c = 1; Break;
Case 6: ls138 a = 0; ls 138 b = 1; ls 138 c = 1; Break;
Case 7: ls138a =1; ls 138 b = 1; ls 138 c = 1; Break;
}
i++;
If (i==8)
{
I = 0;
num++;
If( 10==num) // Read the data of 1302 periodically. The time interval can be adjusted
{
read RTC _ Flag = 1; //Use the flag bit for judgment.
num = 0;
}
}
}
This is the C program of DS 1302 which shows the time. Take a good look.