TI工程师您好!我在使用BQ7693006,遇到以下情况,读出来数据固定不变,始终一个数据,IIC程序在2408上验证过了,没问题,求解??
#define ADDR_76930_R 0x31
#define ADDR_76930_w 0x30
//iiC块程序
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure I2C2 pins: PB6->SCL and PB7->SDA */
RCC_APB2PeriphClockCmd(EEPROM_I2C_SCL_GPIO_RCC|EEPROM_I2C_SDA_GPIO_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SCL_PIN ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(EEPROM_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = EEPROM_I2C_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(EEPROM_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
}
static void I2C_delay(void)
{
uint8_t i=20;/* ÕâÀï¿ÉÒÔÓÅ»¯ËÙ¶È,¾²âÊÔ×îµÍµ½5»¹ÄÜдÈë */
while(i)
{
i--;
}
}
static FunctionalState I2C_Start(void)
{
SDA_H;
SCL_H;
I2C_delay();
if(!SDA_read)return DISABLE; /* SDAÏßΪµÍµçƽÔò×ÜÏßæ,Í˳ö */
SDA_L;
I2C_delay();
if(SDA_read) return DISABLE; /* SDAÏßΪ¸ßµçƽÔò×ÜÏß³ö´í,Í˳ö */
SDA_L;
I2C_delay();
return ENABLE;
}
static void I2C_Stop(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}
static void I2C_Ack(void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
static void I2C_NoAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
static FunctionalState I2C_WaitAck(void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
return DISABLE;
}
SCL_L;
return ENABLE;
}
/**
* @file I2C_SendByte
* @brief Êý¾Ý´Ó¸ßλµ½µÍλ
* @param - SendByte: ·¢Ë͵ÄÊý¾Ý
* @retval ÎÞ
*/
static void I2C_SendByte(uint8_t SendByte)
{
uint8_t i=8;
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80)
SDA_H;
else
SDA_L;
SendByte<<=1;
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}
/**
* @file I2C_ReceiveByte
* @brief Êý¾Ý´Ó¸ßλµ½µÍλ
* @param ÎÞ
* @retval I2C×ÜÏß·µ»ØµÄÊý¾Ý
*/
static uint8_t I2C_ReceiveByte(void)
{
uint8_t i=8;
uint8_t ReceiveByte=0;
SDA_H;
while(i--)
{
ReceiveByte<<=1;
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte|=0x01;
}
}
SCL_L;
return ReceiveByte;
}
FunctionalState I2C_WriteByte(uint8_t SendByte, uint16_t WriteAddress, uint8_t DeviceAddress)
{
if(!I2C_Start())return DISABLE;
I2C_SendByte((((WriteAddress & 0x0700) >>7) | DeviceAddress) & 0xFFFE); /*ÉèÖÃ¸ßÆðʼµØÖ·+Æ÷¼þµØÖ· */
if(!I2C_WaitAck()){I2C_Stop(); return DISABLE;}
I2C_SendByte((uint8_t)(WriteAddress & 0x00FF)); /* ÉèÖÃµÍÆðʼµØÖ· */
I2C_WaitAck();
I2C_SendByte(SendByte);
I2C_WaitAck();
I2C_Stop();
return ENABLE;
}
/**
* @file I2C_ReadByte
* @brief ¶Áȡһ´®Êý¾Ý
* @param
* - pBuffer: ´æ·Å¶Á³öÊý¾Ý
* - length: ´ý¶Á³ö³¤¶È
* - ReadAddress: ´ý¶Á³öµØÖ·
* - DeviceAddress: Æ÷¼þÀàÐÍ(24c16»òSD2403)
* @retval ·µ»ØÎª:=1³É¹¦¶ÁÈë,=0ʧ°Ü
*/
FunctionalState I2C_ReadByte(uint8_t* pBuffer, uint16_t length, uint16_t ReadAddress, uint8_t DeviceAddress)
{
if(!I2C_Start())return DISABLE;
I2C_SendByte((((ReadAddress & 0x0700) >>7) | DeviceAddress) & 0xFFFE); /* ÉèÖÃ¸ßÆðʼµØÖ·+Æ÷¼þµØÖ· */
if(!I2C_WaitAck()){I2C_Stop(); return DISABLE;}
I2C_SendByte((uint8_t)(ReadAddress & 0x00FF)); /* ÉèÖÃµÍÆðʼµØÖ· */
I2C_WaitAck();
I2C_Start();
I2C_SendByte(((ReadAddress & 0x0700) >>7) | DeviceAddress | 0x0001);
I2C_WaitAck();
while(length)
{
*pBuffer = I2C_ReceiveByte();
if(length == 1)I2C_NoAck();
else I2C_Ack();
pBuffer++;
length--;
}
I2C_Stop();
return ENABLE;
}
void bq76930_config(void)
{
I2C_WriteByte(0x08, 0x00, ADDR_76930_w);
I2C_WriteByte(0x00, 0x01, ADDR_76930_w);
I2C_WriteByte(0x00, 0x02, ADDR_76930_w);
//I2C_WriteByte(0x00, 0x03, ADDR_76930_w);
I2C_WriteByte(0x10, 0x04, ADDR_76930_w);
I2C_WriteByte(0x00, 0x05, ADDR_76930_w);
I2C_WriteByte(0x00, 0x06, ADDR_76930_w);
I2C_WriteByte(0x00, 0x07, ADDR_76930_w);
I2C_WriteByte(0x00, 0x08, ADDR_76930_w);
I2C_WriteByte(0xac, 0x09, ADDR_76930_w);
I2C_WriteByte(0x97, 0x0a, ADDR_76930_w);
I2C_WriteByte(0x19, 0x0b, ADDR_76930_w);
}
int main(void)
{
SYSTICK_Init(); //³õʼ»¯SYS
I2C_Configuration(); //³õʼ»¯II_C
bq76930_config(); //³õʼ»¯BQ76930
USART3_Config(); //³õʼ»¯UART3_485
NVIC_Configuration();
while (1)
{
I2C_ReadByte(ReadBuffer_76930[0],1,0x0c,ADDR_76930_R);
I2C_ReadByte(ReadBuffer_76930[1],1,0x0d,ADDR_76930_R);
I2C_ReadByte(ReadBuffer_76930[2],1,0x0e,ADDR_76930_R);
I2C_ReadByte(ReadBuffer_76930[3],1,0x0f,ADDR_76930_R);
printf("\r\n BQ76930_cell1 is: %d ",( ( (*ReadBuffer_76930[0]) & 0x3f)*256)+*ReadBuffer_76930[1]);
printf("\r\n BQ76930_cell2 is: %d ",( ( (*ReadBuffer_76930[1]) & 0x3f)*256)+*ReadBuffer_76930[2]);
delay_ms(1000);
}
}