# include & ltpic.h & gt// contains pre-defined internal resources of single chip microcomputer.
_ _ CONFIG(0x 1832);
//Chip configuration word, watchdog off, power-on delay on, power-off detection off, low-voltage programming off, encryption, 4M crystal oscillator HS oscillation.
# Define i_o RB4 // Define the data port of DS 1302.
# Define sclk RB0 // Define the clock port of DS 1302.
# define rst RB5 // define the reset port of DS 1302.
//unsigned char time _ rx
Unsigned char time _ rx @ 0x30// defines the receive register.
Static volatile bit time _rx7 @ (unsigned) time _ rx * 8+7; //most significant bit of the receiving register.
//Static variable bit temp0 @ (unsigned) temp * 8+0;
void port _ init(); //Declare the pin initialization function
void ds 1302 _ init(); //Declare the DS 1302 initialization function.
void set _ time(); //Declare setting time function
void get _ time(); //Declare the read time function
Void display (); //Declare the display function
Void time_write_ 1 (unsigned chartime _ tx); //Declare to write a byte function
Unsigned chartime _ read _1(); //Declare a function to read a byte.
void delay(); //Declare the delay function
//Define the time to be set: seconds, minutes, hours, days, months, weeks, years and control words.
const char table[]={0x00,0x58,0x 12,0x8,0x3,0x06,0x06,0x 00 };
//Define the read time and date storage table.
char table 1[7];
//Define the display code of 0-9.
const char table2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x 90 };
// -
//main function
void main()
{
port _ init(); //Call the pin initialization function
ds 1302 _ init(); //Call DS 1302 initialization function.
set _ time(); //Call the set time function
while( 1)
{
get _ time(); //Call the time function
Display (); //Call the display function
}
}
// -
//DS 1302 initialization function
void ds 1302_init()
{
sclk = 0; //Pull-down clock signal
rst = 0; //Reset DS 1302
rst = 1; //Enable DS 1302
time _ write _ 1(0x8e); //Send control command
time _ write _ 1(0); //write DS 1302 is allowed.
rst = 0; //Reset
}
// -
//Set time function
void set_time()
{
int I; //Define loop variables
rst = 1; //Enable DS 1302
time _ write _ 1(0x be); //Clock multi-byte write command
for(I = 0; I<8; I++) // Write 8 bytes of data continuously.
{
Time_write_ 1 (table [I]); //Call the write a byte function.
}
rst = 0; //Reset
}
// -
//Read time function
void get_time()
{
int I; //Set loop variable
rst = 1; //Enable DS 1302
time _ write _ 1(0x BF); //Send multi-byte read command
for(I = 0; I<7; I++) // Read 7 bytes of data continuously.
{
table 1[I]= time _ read _ 1(); //Call the function that reads 1 byte data.
}
rst = 0; //Reset DS 1302
}
// -
//Write a byte data function
Void time_write_ 1 (unsigned character time_tx)
{
int j; //Set loop variable
for(j = 0; j & lt8; J++) // Write 8bit continuously.
{
I _ o = 0; //Set the data to 0 first.
sclk = 0; //The clock signal goes low
if(time _ tx & amp; 0x00 1)/Determines whether the data bit to be sent is 0 or1.
{
I _ o = 1; //The data bit to be sent is 1.
}
time _ tx = time _ tx & gt& gt 1; //The data to be sent is shifted to the right by 1 bit.
sclk = 1; //Pull the clock signal high
}
sclk = 0; //Write a byte to pull down the clock signal.
}
// -
//Read a byte function
Unsigned character time_read_ 1 ()
{
int j; //Set loop variable
tris B4 = 1; //Set the data port direction to input.
for(j = 0; j & lt8; J++) // Read 8bit continuously.
{
sclk = 0; //Pull-down clock signal
Time reception = time reception & gt& gt 1; //The receive register is shifted to the right by 1 bit.
time _ rx7 = I _ o; //Put the received data into the highest bit of the receiving register.
sclk = 1; //Pull the clock signal high
}
tris B4 = 0; //Resume data port directional output.
sclk = 0; //Pull-down clock signal
return(time _ rx); //Returns the read data.
}
// -
//Pin definition function
void port_init()
{
TRISA = 0x00// Sets the full output of port A..
TRISD = 0X00// Set the D port full output.
adcon 1 = 0x 06; //Set the A port as a normal I/O port.
TRISB = 0X02// Set RB 1 as input and others as output.
Option = 0X00// Turn on the weak pull-up of port B..
PORTA = 0XFF
PORTD = 0XFF// Turn off all monitors first.
}
// -
//Display function
Empty display ()
{
int I; //Define lookup variables
If(RB 1==0) // Determines whether RB 1 is pressed. If it is pressed, the date will be displayed instead of the time.
{
table 1[0]= table 1[3];
table 1[ 1]= table 1[4];
table 1[2]= table 1[6];
}
I = table 1[0]& amp; 0x0f// Find the unit of seconds.
PORTD = table 2[I]; //Send to port D for display.
PORTA = 0x 1f; //unit of illumination seconds
delay(); //Extend a period of time to ensure the brightness.
I = table 1[0]& amp; 0xf0// Find dozens of seconds
i = i & gt& gt4; //Move 4 bits to the right
PORTD = table 2[I]; //Send to port D for display.
PORTA = 0x2f// Decimal number of lighting seconds.
delay(); //Extend a period of time to ensure the brightness.
I = table1[1]&; 0x0f// unit of fraction
PORTD = table 2[I]& amp; 0x7f// sent to port D for display, showing decimal point.
PORTA = 0x37// unit of lighting minutes.
delay(); //Delay for a certain time to ensure the brightness.
I = table1[1]&; 0xf0// ten points
i = i & gt& gt4;
PORTD = table 2[I]; //Send to port D for display.
PORTA = 0x3b// Ten digits of the lighting point.
delay(); //Extend a period of time to ensure the brightness.
I = table 1[2]& amp; 0x0f// unit of search time
PORTD = table 2[I]& amp; 0x7f// Send it to port D for display, and add decimal point.
PORTA = 0x3d// unit when lit.
delay(); //Delay for a certain time to ensure the brightness.
I = table 1[2]& amp; 0xf0// Timing of ten digits
i = i & gt& gt4;
PORTD = table 2[I]; //Send to port D for display.
PORTA = 0x3e// Ten digits when lit
delay(); //Extend a period of time to ensure the brightness.
}
// -
//Delay program
Void delay() // delay program
{
int I; //Define an integer variable
for(I = 0x 64; I-; ); //delay
}