rx8025t

rx8025trx8025t_drive_function.h#ifndef__RX8025T_DRIVE_FUNCTION_H#define__RX8025T_DRIVE_FUNCTION_H#include"main.h"#defineRX8025T_Update0x00

大家好,欢迎来到IT知识分享网。

rx8025t_drive_function.h

#ifndef __RX8025T_DRIVE_FUNCTION_H
#define __RX8025T_DRIVE_FUNCTION_H

#include "main.h"


#define RX8025T_Update   0x00   //0x00:不更新数据    0x01:更新数据

/******************************************************************************
                                时间寄存器地址
******************************************************************************/
// 设备读写地址
#define RX8025_Address_Read_Enabled            0x65
#define RX8025_Address_Write_Enabled        0x64


// 设备寄存器地址
#define RX8025_Address_Second                0x00
#define RX8025_Address_Minute                0x01
#define RX8025_Address_Hour                    0x02
#define RX8025_Address_Week                    0x03
#define RX8025_Address_Date                    0x04
#define RX8025_Address_Month                0x05
#define RX8025_Address_Year                  0x06


#define RX8025_Address_Timer_Counter_0      0x0B
#define RX8025_Address_Timer_Counter_1      0x0C

#define RX8025_Address_Extension_Register   0x0D
#define RX8025_Address_Flag_Register        0x0E
#define RX8025_Address_Control_Register     0x0F


// 设备操作模式
#define RX8025_WRITE_MODE                    0xF0
#define RX8025_READ_MODE                        0xF0
#define RX8025_SIMP_READ_MODE                0x04


/******************************************************************************
                                 定义时间结构
******************************************************************************/
typedef struct RX8025T_Time_Typedef
{
    uint8_t  year;        //
    uint8_t  month;        //
    uint8_t  date;        //
    uint8_t  week;        //星期
    uint8_t     hour;        //小时
    uint8_t  minute;    //分钟
    uint8_t  second;    //秒钟 
}RX8025T_Time_Typedef;


#define RX8025T_I2C_Start            I2C_1_Start
#define RX8025T_I2C_Stop            I2C_1_Stop

#define RX8025T_I2C_SendData        I2C_1_SendData
#define RX8025T_I2C_ReceiveData        I2C_1_ReceiveData

#define RX8025T_I2C_Wait_Ack        I2C_1_Wait_Ack
#define RX8025T_I2C_Ack                I2C_1_Ack
#define RX8025T_I2C_NAck            I2C_1_NAck



void RX8025T_Time_Init(void);
void RX8025T_Write_Time(RX8025T_Time_Typedef *Time);
void RX8025T_Read_Time(RX8025T_Time_Typedef* Time);
void RX8025T_Time_Revise(RX8025T_Time_Typedef* Time);


extern RX8025T_Time_Typedef RX8025T_Time_Value;


#endif

 

rx8025t_drive_function.c

#include "rx8025t_drive_function.h"


RX8025T_Time_Typedef RX8025T_Time_Value;
uint8_t RX8025T_Time_Revise_Flag = 0;


/*******************************************************************************
* 函数名: BCD2HEX
* 描述  : BCD转为HEX 
* 参数  : BCD码
* 返回值: HEX码
*******************************************************************************/
uint8_t BCD2_HEX(uint8_t bcd_data)  
{   
    uint8_t temp;   
    temp=(bcd_data>>4)*10 + (bcd_data&0x0f);
    return temp;
}



/*******************************************************************************
* 函数名: HEX2BCD
* 描述  : HEX转为BCD  
* 参数  : -hex_data:十六进制数
* 返回值: BCD码
*******************************************************************************/
uint8_t HEX2_BCD(uint8_t hex_data)  
{   
    uint8_t temp; 
    temp=((hex_data/10)<<4) + (hex_data%10);
    return temp;     
}   



/**
  * @brief  向RX8025T某地址写入一个数据
  * @param  Write_Addr:要操作的地址    Write_Data:要写入的数据
  * @retval 无
  */
static uint8_t RX8025T_Write_Byte(uint8_t Write_Addr, uint8_t Write_Data)
{
    RX8025T_I2C_Start();
    
    RX8025T_I2C_SendData(RX8025_Address_Write_Enabled); //要操作的从机地址和写操作指令
    if(RX8025T_I2C_Wait_Ack())   //检测ACK信号
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_SendData(Write_Addr); //写寄存器存储地址
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }    

    RX8025T_I2C_SendData(Write_Data);
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }        

    RX8025T_I2C_Stop();
    return 0;
}



/**
  * @brief  向RX8025T某地址写入多个数据
  * @param  Write_Addr:要操作的地址  Write_Num:要写入的数据个数  p_Write_Buffer:要写入的数据
  * @retval 无
  */
static uint8_t RX8025T_Write_Bytes(uint8_t Write_Addr, uint32_t Write_Num, uint8_t *p_Write_Buffer)
{
    RX8025T_I2C_Start();
    
    RX8025T_I2C_SendData(RX8025_Address_Write_Enabled); //要操作的从机地址和写操作指令
    if(RX8025T_I2C_Wait_Ack())   //检测ACK信号
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_SendData(Write_Addr); //写寄存器存储地址
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }    
    for(int i=0; i<Write_Num; i++) //连续写
    {
        RX8025T_I2C_SendData(p_Write_Buffer[i]);
        if(RX8025T_I2C_Wait_Ack()) 
        {
            RX8025T_I2C_Stop();
            return 1;    
        }        
    }
    
    RX8025T_I2C_Stop();
    return 0;
}



/**
  * @brief  从RX8025T某地址读一个数据
  * @param  Read_Addr:要操作的地址
  * @retval 读到的数据
  */
static uint8_t RX8025T_Read_Byte(uint8_t Read_Addr)
{
    uint8_t Read_Data;
        
    RX8025T_I2C_Start();
    
    RX8025T_I2C_SendData(RX8025_Address_Write_Enabled);  //要操作的从机地址和写操作指令
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_SendData(Read_Addr);  //发送要读取的寄存器地址
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_Start();         //重新产生起始信号
    
    RX8025T_I2C_SendData(RX8025_Address_Read_Enabled);  //读操作指令
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    

    Read_Data = RX8025T_I2C_ReceiveData(1);  //接收完发送一个非应答信号
    
    RX8025T_I2C_Stop();
    return Read_Data;
}




/**
  * @brief  从RX8025T某地址读多个数据
  * @param   Read_Addr:要操作的地址   Read_Num:要读取的数量   p_Read_Data:缓存读到的数据
  * @retval 读到的数据
  */
static uint8_t RX8025T_Read_Bytes(uint8_t Read_Addr, uint32_t Read_Num, uint8_t *p_Read_Data)
{
    RX8025T_I2C_Start();
    
    RX8025T_I2C_SendData(RX8025_Address_Write_Enabled);  //要操作的从机地址和写操作指令
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_SendData(Read_Addr);  //发送要读取的寄存器地址
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    RX8025T_I2C_Start();          //重新产生起始信号
    
    RX8025T_I2C_SendData(RX8025_Address_Read_Enabled);  //读操作指令
    if(RX8025T_I2C_Wait_Ack()) 
    {
        RX8025T_I2C_Stop();
        return 1;    
    }
    
    for(int i=0; i<Read_Num; i++) //连续读
    {
        if(i >= Read_Num-1)
        {
            p_Read_Data[i] = RX8025T_I2C_ReceiveData(1);  //接收完产生非应答信号
        }
        else
        {
            p_Read_Data[i] = RX8025T_I2C_ReceiveData(0);  //没接收完产生应答信号
        }
    }

    RX8025T_I2C_Stop();
    return 0;
}




/**
  * @brief  RX8025T初始化
  * @param  无
  * @retval 无
  */
void RX8025T_Time_Init(void)
{
    RX8025T_Time_Typedef Time={0x20,0x06,0x05,0x05,0x10,0x00,0x20};
    
    
    RX8025T_Write_Byte(RX8025_Address_Extension_Register,0x00); 
    if((RX8025T_Read_Byte(RX8025_Address_Flag_Register) & 0x02) == 0x02)
    {
        RX8025T_Write_Byte(RX8025_Address_Flag_Register,0x00); 
    }    
    RX8025T_Write_Byte(RX8025_Address_Control_Register,0x40); 

    
    if(RX8025T_Update == 0x01)
    {    
        RX8025T_Write_Byte(RX8025_Address_Second,Time.second);
        RX8025T_Write_Byte(RX8025_Address_Minute,Time.minute);
        RX8025T_Write_Byte(RX8025_Address_Hour,Time.hour);
        RX8025T_Write_Byte(RX8025_Address_Week,Time.week);
        RX8025T_Write_Byte(RX8025_Address_Date,Time.date);
        RX8025T_Write_Byte(RX8025_Address_Month,Time.month);
        RX8025T_Write_Byte(RX8025_Address_Year,Time.year);
    }
}



/**
  * @brief  RX8025T写时间
  * @param  *Time: 时间结构体指针
  * @retval 无
  */
void RX8025T_Write_Time(RX8025T_Time_Typedef *Time)
{        
    RX8025T_Write_Byte(RX8025_Address_Second,Time->second);
    RX8025T_Write_Byte(RX8025_Address_Minute,Time->minute);
    RX8025T_Write_Byte(RX8025_Address_Hour,Time->hour);
    RX8025T_Write_Byte(RX8025_Address_Week,Time->week);
    RX8025T_Write_Byte(RX8025_Address_Date,Time->date);
    RX8025T_Write_Byte(RX8025_Address_Month,Time->month);
    RX8025T_Write_Byte(RX8025_Address_Year,Time->year);
}    





/**
  * @brief  RX8025T读时间
  * @param  无
  * @retval 无
  */
void RX8025T_Read_Time(RX8025T_Time_Typedef* Time)
{
    uint8_t time_buffer[10];
    
    RX8025T_Read_Bytes(RX8025_Address_Second,7,time_buffer);
    
    Time->second = (time_buffer[0]>>4) * 10 + (time_buffer[0]&0x0f);
    Time->minute = (time_buffer[1]>>4) * 10 + (time_buffer[1]&0x0f);
    Time->hour = (time_buffer[2]>>4) * 10 + (time_buffer[2]&0x0f);
    Time->week = (time_buffer[3]&0x07);
    Time->date = (time_buffer[4]>>4) * 10 + (time_buffer[4]&0x0f);
    Time->month = (time_buffer[5]>>4) * 10 + (time_buffer[5]&0x0f);
    Time->year = (time_buffer[6]>>4) * 10 + (time_buffer[6]&0x0f);
}




/**
  * @brief  RX8025T_时间校正
  * @param  *Time: 时间结构体指针
  * @retval 无
  */
void RX8025T_Time_Revise(RX8025T_Time_Typedef* Time)
{
    if(Time->week == 1 && Time->hour == 11 && Time->minute == 59 && Time->second <= 30)
    {
        RX8025T_Time_Revise_Flag = 1;
    }

    if(Time->week == 1 && Time->hour == 12 && Time->minute == 0 &&  Time->second == 7)
    {
        if(RX8025T_Time_Revise_Flag == 1) //校正秒
        {
            RX8025T_Write_Byte(RX8025_Address_Second,0);      

            RX8025T_Time_Revise_Flag = 0;
        }    
    }        
}

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/29291.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信