找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4598|回復(fù): 0
收起左側(cè)

RX8010SJ驅(qū)動的關(guān)鍵代碼,自己編寫資源,與君共享

[復(fù)制鏈接]
ID:81196 發(fā)表于 2021-4-30 11:51 | 顯示全部樓層 |閱讀模式
本帖最后由 shumivan 于 2021-4-30 13:37 編輯

RX8010SJ.c的關(guān)鍵代碼如下:

CLK_Time calendar;

/**************時鐘芯片初始化************/
void RX8010_Init(void)
{        
    CLK_IIC_Init();
}
/**************向時鐘寫入時間************/
void RX8010_Write_Time(CLK_Time SetRtc)
{
        u8 buf[7];
        buf[0] = SetRtc.second;
        RX8010_Write_Bytes(0x10,buf,1);
        buf[0] = SetRtc.minute;
        RX8010_Write_Bytes(0x11,buf,1);
        buf[0] = SetRtc.hour;
        RX8010_Write_Bytes(0x12,buf,1);
        buf[0] = RX8010_Week_Translate(WriteWeek ,CLK_Get_Week(2000 + BCD_To_Dec(SetRtc.year) ,BCD_To_Dec(SetRtc.month) ,BCD_To_Dec(SetRtc.day)));
        
        printf("W:%d\r\n",RX8010_Week_Translate(WriteWeek ,CLK_Get_Week(2000 + BCD_To_Dec(SetRtc.year) ,BCD_To_Dec(SetRtc.month) ,BCD_To_Dec(SetRtc.day))));
        RX8010_Write_Bytes(0x13,buf,1);
        buf[0] = SetRtc.day;
        RX8010_Write_Bytes(0x14,buf,1);
        buf[0] = SetRtc.month;
        RX8010_Write_Bytes(0x15,buf,1);
        buf[0] = SetRtc.year;
        RX8010_Write_Bytes(0x16,buf,1);
        
        buf[0] = 0x00;
        RX8010_Write_Bytes(0x1F,buf,1);
}
/**************從時鐘讀出時間************/
void RX8010_Read_Time (CLK_Time *GetRtc)
{
  u8 i;
        u8 buf[7];
        RX8010_Read_Bytes(0x10,buf,7);//讀出溫度值(器件地址,子地址,數(shù)據(jù)儲存器,字節(jié)數(shù))
        GetRtc->second = buf[0]&0x7f; //秒,低七位有效
        GetRtc->minute = buf[1]&0x7f; //分,低七位有效
        GetRtc->hour   = buf[2]&0x3f; //時,低六位有效
        GetRtc->week   = RX8010_Week_Translate(ReadWeek,buf[3]&0x7f); //周,低七位有效
        GetRtc->day    = buf[4]&0x3f; //日,低六位有效
        GetRtc->month  = buf[5]&0x1f; //月,低五位有效
        GetRtc->year   = buf[6]&0xff; //年,低八位有效
}

/**************向RAM指定地址寫入數(shù)據(jù)************/
//slv_addr  器件地址
//sub_addr  內(nèi)部寄存器地址
void RX8010_Read_Bytes(unsigned int sub_addr,unsigned char *s,unsigned char no)
{
        u8 i;
        
        CLK_IIC_Start(); //*發(fā)送啟動信號*/
  if(CLK_IIC_Send_Byte(RX8010_WRITE) == 1)//*發(fā)送SLA+W*///0x64
          {
                  CLK_IIC_Stop();
                        return;
                }
        if(CLK_IIC_Send_Byte(sub_addr) == 1)//*發(fā)送SLA+W*///0x64
          {
                  CLK_IIC_Stop();
                        return;
                }
               
        CLK_IIC_Start();
        if(CLK_IIC_Send_Byte(RX8010_READ) == 1)
          {
                  CLK_IIC_Stop();
                        return;
                }
        for(i=0;i<no-1;i++)
                {
                  *(s + i) = CLK_IIC_Read_Byte(1);
                }
        *(s + no-1) = CLK_IIC_Read_Byte(0);
        CLK_IIC_Stop();
}

/**************從RAM指定地址讀出數(shù)據(jù)************/
//slv_addr  器件地址
//sub_addr  內(nèi)部寄存器地址
void RX8010_Write_Bytes(unsigned int sub_addr,unsigned char *s,unsigned char no)
{
        u8 i;

        CLK_IIC_Start();
        if(CLK_IIC_Send_Byte(RX8010_WRITE) == 1)
          {
                  CLK_IIC_Stop();
                        return;
                }
               
  if(CLK_IIC_Send_Byte(sub_addr) == 1)
          {
                  CLK_IIC_Stop();
                  return;
                }
        for(i=0;i<no;i++)
    {
      if(CLK_IIC_Send_Byte(*(s + i)) == 1)
                          {
                                  CLK_IIC_Stop();
                                        return;
                                }
                        *s=CLK_IIC_Read_Byte(1);
      s++;
    }
}
/**************獲得現(xiàn)在是星期幾************/
//設(shè)置時鐘
//把輸入的時鐘轉(zhuǎn)換為秒鐘
//以1970年1月1日為基準(zhǔn)
//1970~2099年為合法年份
//返回值:0,成功;其他:錯誤代碼.
//月份數(shù)據(jù)表                                                                                         
u8 const Table_Week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正數(shù)據(jù)表        
//獲得現(xiàn)在是星期幾
//功能描述:輸入公歷日期得到星期(只允許1901-2099年)
//輸入?yún)?shù):公歷年月日
//返回值:星期號                                                                                                                                                                                 
u8 CLK_Get_Week(u16 year,u8 month,u8 day)
{        
        u16 temp2;
        u8 yearH,yearL;
        
        yearH=year/100;        yearL=year%100;
        // 如果為21世紀(jì),年份數(shù)加100  
        if (yearH>19)yearL+=100;
        // 所過閏年數(shù)只算1900年之后的  
        temp2=yearL+yearL/4;
        temp2=temp2%7;
        temp2=temp2+day+Table_Week[month-1];
        if (yearL%4==0&&month<3)temp2--;
        return(temp2%7);
}

RX8010SJ.h的關(guān)鍵代碼如下:


#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 )





評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表