本帖最后由 123按时到岗 于 2021-7-14 12:10 编辑
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include "myiic.h"
#include "RX8010SJ.h"
#include "usart.h"
int main(void)
{
u8 t;
delay_init(); //延时函数初始化
NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600);
LED_Init(); //初始化与LED连接的硬件接口
TIM3_Int_Init(4999,7199);//10Khz的计数频率,计数到5000为500ms
RX8010SJ_Init();
Set_RX8010SJ_Time(21,7,1,10,00,00,4); //首次设置时间,设置完成后注释掉,重新下载一次程序即可掉电保存
//年,月,日,时,分,秒
RX8010SJ_Fixed();
while(1)
{
Get_RX8010SJ_Time();
if(t!=calendar.sec)
{
t=calendar.sec;
printf("年月日:");
printf("%ld,%d,%d\r\n",calendar.w_year+2000,calendar.w_month,calendar.w_date);
printf("时分秒:");
printf("%d,%d,%d\r\n",calendar.hour,calendar.min,calendar.sec);
printf("%d,%d\r\n",calendar.temper_H,calendar.temper_L);
}
delay_ms(100);
}
}
//RX8010SJ.c文件
#include "RX8010SJ.h"
#include "myiic.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
Calendar_OBJ calendar;
//所有的通信操作都是以[START开始条件]+[从设备地址+(R/W读写选择)]开始的 即0X65和0X64
#define RX8010SJ_WriteAddress 0x64
#define RX8010SJ_ReadAddress 0x65
u8 BCD2HEX(u8 val)
{
u8 i;
i= val&0x0f;
val >>= 4;
val &= 0x0f;
val *= 10;
i += val;
return i;
}
u16 B_BCD(u8 val)
{
u8 i,j,k;
i=val/10;
j=val%10;
k=j+(i<<4);
return k;
}
void RX8010SJ_WR_Byte(u8 addr,u8 bytedata)
{
I2C_Start();
I2C_SendByte(RX8010SJ_WriteAddress);
I2C_WaitAck();
I2C_SendByte(addr);
I2C_WaitAck();
I2C_SendByte(bytedata);
I2C_WaitAck();
I2C_Stop();
}
u8 RX8010SJ_RD_Byte(u8 addr)
{
u8 Dat=0;
I2C_Start();
I2C_SendByte(RX8010SJ_WriteAddress);
I2C_WaitAck();
I2C_SendByte(addr);
I2C_WaitAck();
I2C_Start();
I2C_SendByte(RX8010SJ_ReadAddress);
I2C_WaitAck();
Dat=I2C_ReceiveByte();
I2C_Stop();
return Dat;
}
void RX8010SJ_Init(void)
{
I2C_GPIO_Config();
RX8010SJ_WR_Byte(0x1D,0);
delay_ms(2);
RX8010SJ_WR_Byte(0x1E,0);
delay_ms(2);
RX8010SJ_WR_Byte(0x1F,0);
delay_ms(2);
}
void Set_RX8010SJ_Time(u8 yea,u8 mon,u8 da,u8 hou,u8 min,u8 sec,u8 week)
{
u8 temp=0;
temp=B_BCD(yea);
RX8010SJ_WR_Byte(0x16,temp);
temp=B_BCD(mon);
RX8010SJ_WR_Byte(0x15,temp);
temp=B_BCD(da);
RX8010SJ_WR_Byte(0x14,temp);
temp=B_BCD(hou);
RX8010SJ_WR_Byte(0x12,temp);
temp=B_BCD(min);
RX8010SJ_WR_Byte(0x11,temp);
temp=B_BCD(sec);
RX8010SJ_WR_Byte(0x10,temp);
temp=B_BCD(week);
RX8010SJ_WR_Byte(0x13,temp);
}
void Get_RX8010SJ_Time(void)
{
calendar.w_year=RX8010SJ_RD_Byte(0x16);
calendar.w_year=BCD2HEX(calendar.w_year);
calendar.w_month=RX8010SJ_RD_Byte(0x15);
calendar.w_month=BCD2HEX(calendar.w_month);
calendar.w_date=RX8010SJ_RD_Byte(0x14);
calendar.w_date=BCD2HEX(calendar.w_date);
calendar.hour=RX8010SJ_RD_Byte(0x12);
calendar.hour&=0x3f;
calendar.hour=BCD2HEX(calendar.hour);
calendar.min=RX8010SJ_RD_Byte(0x11);
calendar.min=BCD2HEX(calendar.min);
calendar.sec=RX8010SJ_RD_Byte(0x10);
calendar.sec=BCD2HEX(calendar.sec);
calendar.week=RX8010SJ_RD_Byte(0x13);
calendar.week=BCD2HEX(calendar.week);
// RX8010SJ_WR_Byte(0x0e,0x20);
// calendar.temper_H=RX8010SJ_RD_Byte(0x11);
// calendar.temper_L=(RX8010SJ_RD_Byte(0x12)>>6)*25;
}
//固定频率设定报警中断功能
void RX8010SJ_Fixed(void)
{
RX8010SJ_WR_Byte(0x1F,0x00);//清除AIE标志位
delay_ms(2);
RX8010SJ_WR_Byte(0x18,0x80);//设置报警数据 当所有的AE标志位设置位1时,报警中断会每分钟产生一次
delay_ms(2);
RX8010SJ_WR_Byte(0x19,0x80);
delay_ms(2);
RX8010SJ_WR_Byte(0x1A,0x80);
delay_ms(2);
RX8010SJ_WR_Byte(0x1D,0x00);//设置WADA标志位
delay_ms(2);
RX8010SJ_WR_Byte(0x1E,0x00);//清除AF标志位
delay_ms(2);
RX8010SJ_WR_Byte(0x1F,0x08);//设置AIE标志位位1
delay_ms(2);
}
//定时器中断功能
void RX8010SJ_Tim(void)
{
RX8010SJ_WR_Byte(0x1D,0x02);
delay_ms(2);
RX8010SJ_WR_Byte(0x1E,0x00);
delay_ms(2);
RX8010SJ_WR_Byte(0x1F,0x10);
delay_ms(2);
RX8010SJ_WR_Byte(0x32,0x05);
delay_ms(2);
RX8010SJ_WR_Byte(0x1B,0x04);
delay_ms(2);
RX8010SJ_WR_Byte(0x1D,0x12);
delay_ms(2);
}
//RX8010SJ.h文件
#ifndef __RX8010SJ_H
#define __RX8010SJ_H
#include "sys.h"
typedef struct
{
u8 hour;
u8 min;
u8 sec;
u32 w_year;
u8 w_month;
u8 w_date;
u8 week;
u8 temper_H;
u8 temper_L;
}Calendar_OBJ;
extern Calendar_OBJ calendar; //日历结构体
extern u8 const mon_table[12]; //月份日期数据表
void RX8010SJ_WR_Byte(u8 addr,u8 bytedata);
u8 RX8010SJ_RD_Byte(u8 addr);
void RX8010SJ_Init(void);
void Get_RX8010SJ_Time(void);
void Set_RX8010SJ_Time(u8 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec,u8 week);//设置时间
void RX8010SJ_Fixed(void);
#endif
|