7045|6

19

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

lm3s读取rx8025代码i2c程序轮询方式 [复制链接]

#define I2C_RX8025_CALENDAR_MOUDLE
#include   
#include             


#define DELAY_2N     0



#define I2C_Speed                   100000
#define RX8025_SLAVE_ADDRESS                         0x32         




static u8 bcdtobin(u8 bcd);
static u8 bintobcd(u8 bcd);
//static void delay (int cnt);


#if 0
static void delay (int cnt)
{
  cnt <<= DELAY_2N;

  while (cnt--);
}
  #endif

static u8 bcdtobin(u8 bcd)
{
        return ((bcd/16)*10 + (bcd&0x0f));
}


static u8 bintobcd(u8 bin)
{                       
        return ((bin/10)*16+(bin%10));
}

/*******************************************************************************
* Function Name  : I2C1_CAL_Init
* Description    : Initializes peripherals used by the I2C Calendar Rx8025 driver.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C1_RX8025CAL_Init(void)
{
        SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
       
        GPIOPinConfigure(GPIO_PA6_I2C1SCL);
    GPIOPinConfigure(GPIO_PA7_I2C1SDA);
       
        GPIOPinTypeI2C(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7);
        I2CMasterEnable(I2C1_MASTER_BASE);
    I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), 0);//false
}



/*******************************************************************************
* Function Name  : InitRTC
* Description    : 初始化时钟芯片
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void InitRTC(void)
{       
#if TURE_RX8025
#if 0
        u8 tempchar[4];  
        tempchar[0] = 0xe0;          //“0”模式设置寄存器“E、F”
        tempchar[1] = 0x23;          //寄存器“E”的位设置
        tempchar[2] = 0x20;          //寄存器“F”的位设置
        I2C_Master_BufferWrite(&tempchar[0],3);
#else
    u8 tempchar[2];
        tempchar[0]=0x20;
    tempchar[1]=0x20;
    I2C_Master_BufferWriteoffset(&tempchar[0],2,0xe0);
#endif
#endif
#if DS1302
    Initial_ds1302();
#endif

        return;
}


/*******************************************************************************
* Function Name  : SetTime
* Description    : 将当前时间设置到时钟芯片(当前时间为公共变量)
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SETTime(struct Time_Struct *time)
{       
        U8 tempchar[8];
#if TURE_RX8025
        #if 1
        tempchar[0] = 0x00;          //“0”模式设置寄存器“0~F”
        tempchar[1] = bintobcd(time->Second);         
        tempchar[2] = bintobcd(time->Minute);         
        tempchar[3] = bintobcd(time->Hour);
        tempchar[4] = (time->Week >= 7)?0:time->Week;
        tempchar[5] = bintobcd(time->Day);          
        tempchar[6] = bintobcd(time->Month);         
        tempchar[7] = bintobcd(time->Year);
        #else
//        tempchar[0] = 0x00;          //“0”模式设置寄存器“0~F”
        tempchar[0] = bintobcd(time->Second);         
        tempchar[1] = bintobcd(time->Minute);         
        tempchar[2] = bintobcd(time->Hour);
        tempchar[3] = (time->Week >= 7)?0:time->Week;
        tempchar[4] = bintobcd(time->Day);          
        tempchar[5] = bintobcd(time->Month);         
        tempchar[6] = bintobcd(time->Year);       
        #endif
        I2C_Master_BufferWrite(&tempchar[0],8);
#endif
#if DS1302
    Wirte_DS1302 (WRITE_PROTECT,0x00);         //禁止写保护
        tempchar[0] = bintobcd(time->Second);         
        tempchar[1] = bintobcd(time->Minute);         
        tempchar[2] = bintobcd(time->Hour);
        tempchar[3] = bintobcd(time->Day);          
        tempchar[4] = bintobcd(time->Month);
        tempchar[5] = (time->Week >= 7)?0:time->Week;
        tempchar[6] = bintobcd(time->Year);       
        tempchar[7] = 0x80;
        DS1302_BurstWriteT(&tempchar[0]);//多字节写
        #if 0  //单字节写
        Wirte_DS1302 (WRITE_SECOND,tempchar[0]);          //秒位初始化
    Wirte_DS1302 (WRITE_MINUTE,tempchar[1]);         //分钟初始化
    Wirte_DS1302 (WRITE_HOUR,tempchar[2]);           //小时初始化
        Wirte_DS1302 (WRITE_DATE,tempchar[3]);          //初始化
    Wirte_DS1302 (WRITE_MONTH,tempchar[4]);         //初始化
    Wirte_DS1302 (WRITE_WEEK,tempchar[5]);           //   
    Wirte_DS1302 (WRITE_YEAR,tempchar[6]);           //   
    Wirte_DS1302 (WRITE_PROTECT,0x80);        //允许写保护
        #endif
#endif
        return;
}

/*******************************************************************************
* Function Name  : GetTime
* Description    : 从时钟芯片读取当前时间.以标准的读模式读取
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GETTime(struct Time_Struct *time)
{  
        u8 tempchar[8];  

#if TURE_RX8025
        I2C_Master_BufferRead(&tempchar[0],8);//用方式3读
        time->Second = bcdtobin(tempchar[1] & 0x7f);
        time->Minute = bcdtobin(tempchar[2] & 0x7f);
        time->Hour = bcdtobin(tempchar[3] & 0x7f);
        tempchar[4] &= 0x7;
        if (tempchar[4]>=7)        
                tempchar[4] = 0;         
    time->Week = tempchar[4];
        time->Day = bcdtobin(tempchar[5] & 0x3f);
        time->Month = bcdtobin(tempchar[6] & 0x1f);       
        time->Year = bcdtobin(tempchar[7]);
        return;
#endif
#if DS1302
    #if 0  //可以用两种方式来读写,1中是单字节读写,一种是突发式多字节读写
        tempchar[0]=Read_DS1302(0x81);
        tempchar[1]=Read_DS1302(0x83);
        tempchar[2]=Read_DS1302(0x85);
        tempchar[3]=Read_DS1302(0x87);
        tempchar[4]=Read_DS1302(0x89);
        tempchar[5]=Read_DS1302(0x8b);
        tempchar[6]=Read_DS1302(0x8d);
        #endif
        DS1302_BurstReadT(&tempchar[0]);//多字节读
        time->Second = bcdtobin(tempchar[0] & 0x7f);
        time->Minute = bcdtobin(tempchar[1] & 0x7f);
        time->Hour = bcdtobin(tempchar[2] & 0x7f);
        time->Day = bcdtobin(tempchar[3] & 0x3f);
        time->Month = bcdtobin(tempchar[4] & 0x1f);       
        tempchar[5] &= 0x7;
        if (tempchar[5]>=7)        
                tempchar[5] = 0;         
    time->Week = tempchar[5];
        time->Year = bcdtobin(tempchar[6]);
        return;
#endif
}


/*******************************************************************************
* Function Name  : I2C_Master_BufferWrite
* Description    : Send a buffer of bytes to the slave.
* Input          : - pBuffer: Buffer of bytes to be sent to the slave.
*                  - NumByteToRead: Number of bytes to be sent to the slave.
* Output         : None.
* Return         : None.
*******************************************************************************/
unsigned long I2CMasterState(unsigned long ulBase)
{
    unsigned long state;

    //
    // Check the arguments.
    //
    ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));

    //
    // Get the raw error state
    //
    state = HWREG(ulBase + I2C_O_MCS);
        return           state;
}
//从头写起
void I2C_Master_BufferWrite(u8* pBuffer,  u16 NumByteToWrite)
{

     OSSchedLock();
     while(I2CMasterBusy(I2C1_MASTER_BASE));  //如果i2c忙,则等待
     I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS , false); //设置主机从地址
         I2CMasterDataPut(I2C1_MASTER_BASE, 0x00);
     I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);//开始发送地址,后面进行第一个字节发送
         while (NumByteToWrite)
     {  
            while(I2CMasterBusy(I2C1_MASTER_BASE));        //等空闲时
        I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
        pBuffer++;
                NumByteToWrite--;
                I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);          //发送数据
                if(NumByteToWrite==1)
                {
               while(I2CMasterBusy(I2C1_MASTER_BASE));        //等空闲时
                   I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
           pBuffer++;                  
           I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);        //发送最后一个数据 后发送停止位
                   NumByteToWrite--;
                }
     }
         OSSchedUnlock();
}

//写到指定的位置
void I2C_Master_BufferWriteoffset(u8* pBuffer,  u16 NumByteToWrite,u8 ucoffset)
{
     OSSchedLock();
     while(I2CMasterBusy(I2C1_MASTER_BASE));  //如果i2c忙,则等待
     I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS , false); //设置主机从地址
         I2CMasterDataPut(I2C1_MASTER_BASE, ucoffset);
     I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START);//开始发送地址,后面进行第一个字节发送
         while (NumByteToWrite)
     {  
            while(I2CMasterBusy(I2C1_MASTER_BASE));        //等空闲时
        I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
        pBuffer++;
                NumByteToWrite--;
                I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);          //发送数据
                if(NumByteToWrite==1)
                {
               while(I2CMasterBusy(I2C1_MASTER_BASE));        //等空闲时
                   I2CMasterDataPut(I2C1_MASTER_BASE, *pBuffer);
           pBuffer++;                  
           I2CMasterControl(I2C1_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);        //发送最后一个数据 后发送停止位
                   NumByteToWrite--;
                }
     }
         OSSchedUnlock();
}

/*******************************************************************************
* Function Name  : I2C_Master_BufferRead
* Description    : Reads buffer of bytes  from the slave.
* Input          : - pBuffer: Buffer of bytes to read from the slave.
*                  - NumByteToRead: Number of bytes to be read by the Master.
*                  - NumByteToRead >= 3.
* Output         : None.
* Return         : None.
*******************************************************************************/
void I2C_Master_BufferRead(u8* pBuffer,  u16 NumByteToRead)
{
      OSSchedLock();
          while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY);
      I2CMasterSlaveAddrSet(I2C1_MASTER_BASE, RX8025_SLAVE_ADDRESS| (0x00 >> 8), false);
      I2CMasterDataPut(I2C1_MASTER_BASE, 0x00);
      I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_SINGLE_SEND);


     while(I2CMasterBusy(I2C1_MASTER_BASE));  //如果i2c忙,则等待
      I2CMasterSlaveAddrSet(I2C1_MASTER_BASE,0x32, true);
          I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
     while(I2CMasterBusy(I2C1_MASTER_BASE));  //如果i2c忙,则等待
         I2CMasterDataGet(I2C1_MASTER_BASE);
          I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
          while(NumByteToRead)
          {
                if(NumByteToRead!=1)
                {
          while(I2CMasterBusy(I2C1_MASTER_BASE));  //如果i2c忙,则等待
                  *pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
               NumByteToRead--;
               I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
                     if(NumByteToRead==1)
                   {                           
                                while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY);   //如果i2c忙,则等待
                            *pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
                    NumByteToRead--;
                                I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
                          
                   }
                 }               
                 else
                 {
                   while(I2CMasterState(I2C1_MASTER_BASE) & I2C_MCS_BUSY);  //如果i2c忙,则等待
                  *pBuffer++ = I2CMasterDataGet(I2C1_MASTER_BASE);
               NumByteToRead--;
                   I2CMasterControl(I2C1_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
                 }
          }
          OSSchedUnlock();
}

i2c_rx8025.rar

2.92 KB, 下载次数: 105

此帖出自ARM技术论坛

最新回复

可以给个联系方式吗?关于这个程序想请教一下  很着急  谢谢  详情 回复 发表于 2015-1-24 19:37
点赞 关注
 

回复
举报

2734

帖子

0

TA的资源

裸片初长成(初级)

沙发
 
天啊,直接贴代码,你也真够可以的了,呵呵,不是贬义
此帖出自ARM技术论坛
 
个人签名我爱电子!
 
 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
谢谢。。下来学习一下
此帖出自ARM技术论坛
 
 
 

回复

42

帖子

0

TA的资源

一粒金砂(中级)

4
 
我发送的是三个字节0xFF,0x01,0x0A,为什么接收到的是四个字节0x0A,0xFF,0x01,0x0A呢
此帖出自ARM技术论坛

点评

是不是开始多收了个0x0A,是每次都多收这个字节的数据还是说是个无规律的数据?  详情 回复 发表于 2013-1-15 21:30
 
 
 

回复

42

帖子

0

TA的资源

一粒金砂(中级)

5
 
读取函数要是可以读两个字节吗,函数是不是不变
此帖出自ARM技术论坛
 
 
 

回复

436

帖子

5

TA的资源

五彩晶圆(初级)

6
 
原帖由 微笑的不倒翁 于 2013-1-12 10:40 发表
我发送的是三个字节0xFF,0x01,0x0A,为什么接收到的是四个字节0x0A,0xFF,0x01,0x0A呢
是不是开始多收了个0x0A,是每次都多收这个字节的数据还是说是个无规律的数据?
此帖出自ARM技术论坛
 
 
 

回复

22

帖子

2

TA的资源

一粒金砂(中级)

7
 
可以给个联系方式吗?关于这个程序想请教一下  很着急  谢谢
此帖出自ARM技术论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表