常见51单片机时钟用的是DS1302,也有很多视频教程。工作中碰见用到RX8010SJ时钟,查阅相关资料,结合网上案例,总结出以下下代码。
RX8010SJ.c- #include "RX8010.h"
- #include "I2C.h"
- #include "DELAY.H"
- Calendar_OBJ calendar;
- //unsigned char BCD2HEX(unsigned char val)
- //{
- // unsigned char i;
- // i= val&0x0f;
- // val >>= 4;
- // val &= 0x0f;
- // val *= 10;
- // i += val;
- // return i;
- //}
- unsigned short B_BCD(unsigned char val)
- {
- unsigned char i,j,k;
- i=val/10;
- j=val%10;
- k=j+(i<<4);
- return k;
- }
- /**************时钟芯片初始化************/
- void RX8010_Init(void)
- {
- // I2C_Init();
- RX8010_Write_Bytes(0x1D,0);
- Delay_ms(2);
- RX8010_Write_Bytes(0x1E,0);
- Delay_ms(2);
- RX8010_Write_Bytes(0x1F,0);
- Delay_ms(2);
- }
- /**************向RAM指定地址写入数据************/
- //slv_addr 器件地址
- //sub_addr 内部寄存器地址
- void RX8010_Write_Bytes(unsigned char sub_addr,unsigned char bytedata)
- {
- I2C_Start(); //*发送启动信号*/
- I2C_WriteAbyte(RX8010_WRITE);//*发送SLA+W*///0x64
- I2C_Check_ACK();
- I2C_WriteAbyte(sub_addr);
- I2C_Check_ACK();
- I2C_WriteAbyte(bytedata);
- I2C_Check_ACK();
- I2C_Stop();
- }
- /**************从RAM指定地址读出数据************/
- //slv_addr 器件地址
- //sub_addr 内部寄存器地址
- unsigned char RX8010_Read_Bytes(unsigned char sub_addr)
- {
- unsigned char Dat = 0;
- I2C_Start();
- I2C_WriteAbyte(RX8010_WRITE);
- I2C_Check_ACK();
- I2C_WriteAbyte(sub_addr);
- I2C_Check_ACK();
- I2C_Start();
- I2C_WriteAbyte(RX8010_READ);
- I2C_Check_ACK();
- Dat = I2C_ReadAbyte();
- I2C_Stop();
- return Dat;
- }
- /**************向时钟写入时间************/
- void Set_RX8010SJ_Time(unsigned char yea,unsigned char mon,unsigned char da,unsigned char hou,unsigned char min,unsigned char sec,unsigned char week)
- {
- unsigned char temp = 0;
- temp=B_BCD(yea);
- RX8010_Write_Bytes(0x16,temp);
- temp=B_BCD(mon);
- RX8010_Write_Bytes(0x15,temp);
- temp=B_BCD(da);
- RX8010_Write_Bytes(0x14,temp);
- temp=B_BCD(hou);
- RX8010_Write_Bytes(0x12,temp);
- temp=B_BCD(min);
- RX8010_Write_Bytes(0x11,temp);
- temp=B_BCD(sec);
- RX8010_Write_Bytes(0x10,temp);
- temp=B_BCD(week);
- RX8010_Write_Bytes(0x13,temp);
- }
- /**************从时钟读出时间************/
- void Get_RX8010SJ_Time(void)
- {
- calendar.w_year=RX8010_Read_Bytes(0x16);
- // calendar.w_year=BCD2HEX(calendar.w_year);
- calendar.w_month=RX8010_Read_Bytes(0x15);
- // calendar.w_month=BCD2HEX(calendar.w_month);
- calendar.w_date=RX8010_Read_Bytes(0x14);
- // calendar.w_date=BCD2HEX(calendar.w_date);
- calendar.hour=RX8010_Read_Bytes(0x12);
- // calendar.hour&=0x3f;
- // calendar.hour=BCD2HEX(calendar.hour);
- calendar.min=RX8010_Read_Bytes(0x11);
- // calendar.min=BCD2HEX(calendar.min);
- calendar.sec=RX8010_Read_Bytes(0x10);
- // calendar.sec=BCD2HEX(calendar.sec);
- calendar.week=RX8010_Read_Bytes(0x13);
- // calendar.week=BCD2HEX(calendar.week);
- }
复制代码- #ifndef _BSP_RTC_H
- #define _BSP_RTC_H
- #include <STC15F2K60S2.H>
- #define RX8010_READ 0x65
- #define RX8010_WRITE 0x64
- // register address , for RX-8010SJ
- #define RX8010_ADDR_SECOND ( 0x10 )
- #define RX8010_ADDR_MINUTE ( 0x11 )
- #define RX8010_ADDR_HOUR ( 0x12 )
- #define RX8010_ADDR_WEEK ( 0x13 )
- #define RX8010_ADDR_DATE ( 0x14 )
- #define RX8010_ADDR_MONTH ( 0x15 )
- #define RX8010_ADDR_YEAR ( 0x16 )
- #define RX8010_ADDR_RSV17 ( 0x17 )
- #define RX8010_ADDR_ALM_MINUTE ( 0x18 )
- #define RX8010_ADDR_ALM_HOUR ( 0x19 )
- #define RX8010_ADDR_ALM_WEEK ( 0x1A )
- #define RX8010_ADDR_ALM_DATE ( 0x1A )
- #define RX8010_ADDR_TMR_CNT0 ( 0x1B )
- #define RX8010_ADDR_TMR_CNT1 ( 0x1C )
- #define RX8010_ADDR_EXT_REG ( 0x1D )
- #define RX8010_ADDR_FLAG_REG ( 0x1E )
- #define RX8010_ADDR_CTRL_REG ( 0x1F )
- typedef struct
- {
- unsigned char hour;
- unsigned char min;
- unsigned char sec;
- unsigned int w_year;
- unsigned char w_month;
- unsigned char w_date;
- unsigned char week;
- unsigned char temper_H;
- unsigned char temper_L;
- }Calendar_OBJ;
- extern Calendar_OBJ calendar; //日历结构体
- extern unsigned char const mon_table[12]; //月份日期数据表
- void RX8010_Write_Bytes(unsigned char sub_addr,unsigned char bytedata);
- unsigned char RX8010_Read_Bytes(unsigned char sub_addr);
- void RX8010SJ_Init(void);
- void Get_RX8010SJ_Time(void);
- void Set_RX8010SJ_Time(unsigned char syear,unsigned char smon,unsigned char sday,unsigned char hour,unsigned char min,unsigned char sec,unsigned char week);//设置时间
- #endif
复制代码 main.c- #include <STC15F2K60S2.H>
- #include "LCD1602.h"//??LCD1602???
- #include "RX8010.h"//??DS1302???
- bit flag200ms = 0; //200ms定时标志
- unsigned char T0RH = 0; //T0重载值的高字节
- unsigned char T0RL = 0; //T0重载值的低字节
- void ConfigTimer0(unsigned int ms);
- int main(void)
- {
- unsigned char t = 0xAA;
- unsigned char str[12]; //字符串转换缓冲区
- EA = 1; //开总中断
- ConfigTimer0(1); //T0定时1ms
- // RX8010SJ_Init(); //初始化实时时钟
- LCD_Init(); //初始化液晶
- Set_RX8010SJ_Time(21,7,1,10,00,00,4);
-
- while (1)
- {
- if(flag200ms)
- {
- flag200ms = 0;
- Get_RX8010SJ_Time();
- if(t != calendar.sec)
- {
- str[0] = '2';
- str[1] = '0';
- str[2] = (calendar.w_year >> 4) + '0';
- str[3] = (calendar.w_year & 0x0F) + '0';
- str[4] = '-';
- str[5] = (calendar.w_month >> 4) + '0';
- str[6] = (calendar.w_month & 0x0F) + '0';
- str[7] = '-';
- str[8] = (calendar.w_date >> 4) + '0';
- str[9] = (calendar.w_date & 0x0F) + '0';
- str[10] = '\0';
- LCD_ShowString(0, 0, str);
- str[0] = ( calendar.week & 0x0F) + '0';
- str[1] = '\0';
- LCD_ShowString(11, 0, "week");
- LCD_ShowString(15, 0, str);
- str[0] = (calendar.hour >> 4) + '0';
- str[1] = (calendar.hour & 0x0F) + '0';
- str[2] = ':';
- str[3] = (calendar.min >> 4) + '0';
- str[4] = (calendar.min & 0x0F) + '0';
- str[5] = ':';
- str[6] = (calendar.sec >> 4) + '0';
- str[7] = (calendar.sec & 0x0F) + '0';
- str[8] = '\0';
- LCD_ShowString(4, 1, str);
- t = calendar.sec;
- }
- }
-
- }
- }
- /* 配置并启动T0,ms-T0定时时间 */
- void ConfigTimer0(unsigned int ms)
- {
- unsigned long tmp; //临时变量
-
- tmp = 11059200 / 12; //定时器计数频率
- tmp = (tmp * ms) / 1000; //计算所需的计数值
- tmp = 65536 - tmp; //计算定时器重载值
- tmp = tmp + 12; //补偿中断响应延时造成的误差
- T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
- T0RL = (unsigned char)tmp;
- TMOD &= 0xF0; //清零T0的控制位
- TMOD |= 0x01; //配置T0为模式1
- TH0 = T0RH; //加载T0重载值
- TL0 = T0RL;
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- }
- /* T0中断服务函数,执行200ms定时 */
- void InterruptTimer0() interrupt 1
- {
- static unsigned char tmr200ms = 0;
-
- TH0 = T0RH; //重新加载重载值
- TL0 = T0RL;
- tmr200ms++;
- if (tmr200ms >= 200) //定时200ms
- {
- tmr200ms = 0;
- flag200ms = 1;
- }
- }
复制代码
|