欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
基于51單片機RX8010SJ時鐘
[打印本頁]
作者:
文明觀球
時間:
2024-5-30 17:16
標題:
基于51單片機RX8010SJ時鐘
常見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;
}
}
復制代碼
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1