Pic microcontroller control clock chip DS1302 program

Single chip microcomputer STM32L151CCU6
1206RGB (single)
>

#include / / Contains the internal resources of the microcontroller predefined
__CONFIG(0x1832);
/ / Chip configuration word, watchdog off, power-on delay on, power-down detection off, low-voltage programming off, encryption, 4M crystal HS oscillation

#definei_oRB4//Define the data port of DS1302
#definesclkRB0//Define the clock port of the DS1302
#definerstRB5//Define the reset port of DS1302

//unsignedchartime_rx;
Unsignedchartime_rx@0x30; / / define the receiving register
Staticvolatilebittime_rx7@(unsigned)&time_rx*8+7;//The highest bit of the receive register
//staticvolatilebittemp0@(unsigned)&temp*8+0;

Voidport_init();//Declare pin initialization function
Voidds1302_init();//Declare DS1302 initialization function
Voidset_time();// declare the set time function
Voidget_time();//Declare the read time function
Voiddisplay();//Declare the display function
Voidtime_write_1(unsignedchartime_tx); / / declare a byte function
Unsignedchartime_read_1();//Declare to read a byte function
Voiddelay();//Declare the delay function
/ / Define the time to be set: seconds, minutes, hours, days, months, weeks, years, control words
Constchartable[]={0x00,0x58,0x12,0x8,0x3,0x06,0x06,0x00};
/ / Define the read time and date storage table
Chartable1[7];
/ / Define the display code of 0-9
Constchartable2[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

//----------------------------------------------
/ / Main function
Voidmain()
{
Port_init();//call pin initialization function
Ds1302_init();//Invoke DS1302 initialization function
Set_time();//call set time function
While(1)
{
Get_time();//call take time function
Display();//call display function
}
}

//---------------------------------------------
/ / DS1302 initialization function
Voidds1302_init()
{
Sclk=0; / / pull down the clock signal
Rst=0;//Reset DS1302
Rst=1;//Enable DS1302
Time_write_1 (0x8e); / / send control commands
Time_write_1(0);//Allow write DS1302
Rst=0;//reset
}

//---------------------------------------------
/ / Set the time function
Voidset_time()
{
Inti; / / define the loop variable
Rst=1;//Enable DS1302
Time_write_1(0xbe);//clock multibyte write command
For(i=0;i<8;i++)//Write 8 bytes of data continuously
{
Time_write_1(table[i]);//call write a byte function
}
Rst=0;//reset
}

//---------------------------------------------
/ / Read the time function
Voidget_time()
{
Inti; / / set the loop variable
Rst=1;//Enable DS1302
Time_write_1 (0xbf); / / send multi-byte read command
For(i=0;i<7;i++)// continuously reads 7 bytes of data
{
Table1[i]=time_read_1();//call a function that reads 1 byte of data
}
Rst=0;//Reset DS1302
}

//--------------------------------------------
/ / Write a byte data function
Voidtime_write_1(unsignedchartime_tx)
{
Intj; / / set the loop variable
For(j=0;j<8;j++)//Continuous write 8bit
{
I_o=0; / / first set the data to 0
Sclk=0;//clock signal is pulled low
If(time_tx&0x01)//determine that the data bit to be transmitted is 0 or 1
{
I_o=1;//The data bit to be sent is 1
}
Time_tx=time_tx>>1;//The data to be sent is shifted to the right by 1 bit
Sclk=1; / / pull high clock signal
}
Sclk=0; / / write a byte, pull down the clock signal
}

//---------------------------------------------
/ / Read a byte function
Unsignedchartime_read_1()
{
Intj; / / set the loop variable
TRISB4=1; / / set the data port direction as input
For(j=0;j<8;j++)//Continuous reading of 8bit
{
Sclk=0; / / pull down the clock signal
Time_rx=time_rx>>1; / / receive register right shift 1 bit
Time_rx7=i_o;//put the received data to the highest bit of the receive register
Sclk=1; / / pull high clock signal
}
TRISB4=0; / / restore the data port direction to output
Sclk=0; / / pull down the clock signal
Return(time_rx);//return the read data
}

//--------------------------------------------
//pin definition function
Voidport_init()
{
TRISA=0x00; / / set A port full output
TRISD=0X00; / / set D port full output
ADCON1=0X06; / / set A port is a normal I / O port
TRISB=0X02; / / set RB1 as input, the other is output
OPTION=0X00;//Open B port weak pull-up
PORTA=0XFF;
PORTD=0XFF; / / first off all displays
}

//-------------------------------------------
/ / Display function
Voiddisplay()
{
Inti; / / define table lookup variables
If(RB1==0)//Determine whether RB1 is pressed, if it is pressed, the date is displayed, and the display time is not pressed.
{
Table1[0]=table1[3];
Table1[1]=table1[4];
Table1[2]=table1[6];
}
i=table1[0]&0x0f;//seconds of the second
PORTD=table2[i];//Send D port display
PORTA=0x1f;//one bit of the second
Delay();//Extension for a while to ensure brightness
i=table1[0]&0xf0;//Get the tenth of the second
i=i>>4;//shift 4 bits to the right
PORTD=table2[i];//Send D port display
PORTA=0x2f; / / light the tenth of the second
Delay();//Extension for a while to ensure brightness

i=table1[1]&0x0f;//The number of bits to be divided
PORTD=table2[i]&0x7f;//Send D port display and display decimal point
PORTA=0x37; / / lit ones
Delay();//delay for a certain period of time to ensure brightness
i=table1[1]&0xf0;//10 points for the score
i=i>>4;
PORTD=table2[i];//Send D port display
PORTA=0x3b; / / light ten points
Delay();//Extension for a while to ensure brightness

i=table1[2]&0x0f;//one bit of the time
PORTD=table2[i]&0x7f;//Send D port display, plus decimal point
PORTA=0x3d;//one digit when lit
Delay();//delay for a certain period of time to ensure brightness
i=table1[2]&0xf0;//10th in the time
i=i>>4;
PORTD=table2[i];//Send D port display
PORTA=0x3e;//10 digits when lit
Delay();//Extension for a while to ensure brightness
}

//------------------------------------------------ ------------------
/ / delay program
Voiddelay()//delay program
{
Inti; / / define the shaping variable
For(i=0x64;i--;);//delay
}



/ / Set the time and date first: seconds (00), minutes (58), time (12), day (08), month (03), year (06)
//The six-digit digital view shows the time and date. The default is the time. The decimal point is used to distinguish between minutes and minutes.

ADC EV Charger

ADC EV charger

Shenzhen Hongjiali New Energy Co., Ltd. , https://www.hjlcharger.com