打印
[其他模拟产品-信号链]

I2C TI BQ32000读写程序

[复制链接]
2336|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
assange|  楼主 | 2014-8-25 15:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//BQ32000д¼Ä´æÆ÷
void BQ3200_WriteRegister(uint32_t I2Cx, unsigned char  RegisterAddress, unsigned char  Data)
{
    //·¢Ë͵ØÖ·
    I2C_GenerateSTART(BQ32000_I2C_PORT);
    I2C_Send7bitAddress(BQ32000_I2C_PORT,BQ32000_IIC_ADDRESS,kI2C_Write);
    I2C_WaitAck(BQ32000_I2C_PORT);
    //·¢ËÍÐèҪдÈëµÄ¼Ä´æÆ÷µØÖ·
    I2C_SendData(BQ32000_I2C_PORT,RegisterAddress);
    I2C_WaitAck(BQ32000_I2C_PORT);
    //·¢ËÍÊý¾Ý
    I2C_SendData(BQ32000_I2C_PORT,Data);
    I2C_WaitAck(BQ32000_I2C_PORT);
    //½áÊø
    I2C_GenerateSTOP(BQ32000_I2C_PORT);
    while(I2C_IsBusy(BQ32000_I2C_PORT) == 0);
}


//BQ32000¶Á¼Ä´æÆ÷
uint8_t BQ32000_ReadRegister(uint32_t I2Cx, uint8_t Addr)
{
    uint8_t result;
    //·¢ËÍ´Ó»úµØÖ·
    I2C_GenerateSTART(I2Cx);
    I2C_Send7bitAddress(I2Cx,BQ32000_IIC_ADDRESS,kI2C_Write);
    I2C_WaitAck(I2Cx);
    //ÐèÒª¶ÁÈ¡µÄ¼Ä´æÆ÷
    I2C_SendData(I2Cx, Addr);
    I2C_WaitAck(I2Cx);
    //ÖØпªÊ¼
    I2C_GenerateRESTART(I2Cx);
    //Ö÷»ú¶Á
    I2C_Send7bitAddress(I2Cx,BQ32000_IIC_ADDRESS,kI2C_Read);
    I2C_WaitAck(I2Cx);
    //ÉèÖÃKinetisΪ½ÓÊÕģʽ
    I2C_SetMasterMode(I2Cx,kI2C_Read);

    result = I2C_ReadData(I2Cx);
                I2C_GenerateAck(I2Cx);
    I2C_WaitAck(I2Cx);
    //·¢ËÍÍ£Ö¹ÐźÅ
    I2C_GenerateSTOP(I2Cx);
    //¶ÁÈ¡Êý¾Ý
    result = I2C_ReadData(I2Cx);
                while(I2C_IsBusy(BQ32000_I2C_PORT) == 0);
    return result;
}

//³õʼ»¯BQ32000
void BQ32000_Init(void)
{       
        I2C_QuickInit(I2C0_SCL_PB00_SDA_PB01, 96000);
}


//ÉèÖóõʼʱ¼ä
void Settime(char tyear,char tmonth,char tdayofweek,char tdate,char thour, char tminute, char tseconds)
{
                secondunits=(tseconds%10);
    secondtens=(tseconds/10);
    mintens=(tminute/10);
    minunits=(tminute%10);
    hourtens=(thour/10);
    hourunits=(thour%10);
    tdayofweek=tdayofweek;
    datetens=(tdate/10);
    dateunits=(tdate%10);
    monthtens=(tmonth/10);
    monthunits=(tmonth%10);
    yeartens=(tyear/10);
    yearunits=(tyear%10);

    timearrW[0]=secondunits+(secondtens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x00,timearrW[0]);
       
    timearrW[1]=minunits+(mintens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x01,timearrW[1]);
       
    timearrW[2]=hourunits+(hourtens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x02,timearrW[2]);
               
    timearrW[3]=tdayofweek;
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x03,timearrW[3]);
               
    timearrW[4]=dateunits+(datetens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x04,timearrW[4]);
               
    timearrW[5]=monthunits+(monthtens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x05,timearrW[5]);
               
    timearrW[6]=yearunits+(yeartens<<4);
                BQ3200_WriteRegister(BQ32000_I2C_PORT,0x06,timearrW[6]);
   
}

void Readtime(void)
{
        timearrR[0] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x00);
        timearrR[1] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x01);
        timearrR[2] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x02);
        timearrR[3] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x03);
        timearrR[4] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x04);
        timearrR[5] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x05);
        timearrR[6] = BQ32000_ReadRegister(BQ32000_I2C_PORT,0x06);
}
不知论坛大神可否指点在下一二 新手上路

相关帖子

沙发
xyz549040622| | 2014-8-25 16:57 | 只看该作者
对于此类情况,我只想说一句,抓波形,找例程

使用特权

评论回复
板凳
mmuuss586| | 2014-8-25 19:08 | 只看该作者
IIC的程序即使有问题,也不一定看的出来;
你联系TI代理看看有没有参考程序;

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

9

主题

38

帖子

0

粉丝