[mw_shl_code=c,true]//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);
}
[/mw_shl_code]
我用的是飞思卡尔的单片机,不知道这么写问题在哪里,新手上路,跪求指点
|