18.2.14 初始化子函数

初始化子函数用于对实时时钟芯片DS1302的时钟寄存器进行初始化操作。在程序中,分别输入需要设置的年、月、星期、日、小时、分钟和秒的数值,然后调用写允许子函数允许对寄存器操作,调用充电控制子函数允许涓流充电,最后以多字节突发方式写入多个时钟数据。初始化程序代码示例如下。


void InitDS1302()//初始化子函数

{

uchar year,month,date,day,hour,minute,second;

printf(“\nEnter the Clock information:”);

printf(“\nPlease Enter the year(0-99):”);//输入年

scanf(“%bx”,&year);

printf(“\n Please Enter the month(1-12):”);//输入月

scanf(“%bx”,&month);

printf(“\n Please Enter the date(1-31):”);//输入日

scanf(“%bx”,&date);

printf(“\n Please Enter the day(1-7):”);//输入星期

scanf(“%bx”,&day);

printf(“\n Please Enter the hour(1-24):”);//输入小时

scanf(“%bx”,&hour);

hour=hour&0x3f;//设置时钟为24小时方式

printf(“\n Please Enter the minute(0-59):”);//输入分钟

scanf(“%bx”,&minute);

printf(“\n Please Enter the second(0-59):”);//输入秒

scanf(“%bx”,&second);

EnableWrite();//写允许子函数

Charge();//允许充电

ResetDS1302();//复位DS1302

WriteByteDS1302(0xbe);//以多字节突发方式写入8个字节时钟数据

WriteByteDS1302(second);//写入秒

WriteByteDS1302(minute);//写入分钟

WriteByteDS1302(hour);//写入小时

WriteByteDS1302(date);//写入日

WriteByteDS1302(month);//写入月

WriteByteDS1302(day);//写入星期

WriteByteDS1302(year);//写入年

WriteByteDS1302(0);//对写保护控制寄存器写入0

ResetDS1302();//复位DS1302

}