BQ76920 I2C通信无法正确读取
各位大神救我!
我的控制器是TM4C123G开发板,按照bq76920说明书上的事例电路图,做了一个电路,但是控制器从bq76920上读取寄存器信息的时候,获得的结果永远都是255,(也就是说SDA一直是高电位?),在CCS里调试的时候,运行写指令的时候就会提示:
CORTEX_M4_0: Error: Debug Port error occurred.
CORTEX_M4_0: Trouble Halting Target CPU
这是为什么呢?我是完全按照事例电路连接的,(事例电路上没有上拉电阻,我在自己的电路里给SCL端和SDL端又加了上拉电阻以及3.3v的电压)。如何判断到底是电路出问题还是我的代码有问题呢? 还有I2C通信的时候,一定要有CRC吗? 如果我不需要充电放电保护,我可不可以直接把DSG和CHG都接地或者啥都不接?
这个是我的初始化程序,以及读和写的代码。真是不知道问题在哪里了T_T
//initialize I2C module 0
//Slightly modified version of TI's example code
void InitI2C0(void)
{
//enable I2C module 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
//reset module
SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
//enable GPIO peripheral that contains I2C 0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Configure the pin muxing for I2C0 functions on port B2 and B3.
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
// Select the I2C function for these pins.
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
// Enable and initialize the I2C0 master module. Use the system clock for
// the I2C0 module. The last parameter sets the I2C data transfer rate.
// If false the data rate is set to 100kbps and if true the data rate will
// be set to 400kbps.
I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
//clear I2C FIFOs
HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000;
UARTprintf("InitI2C0 \n");
}
////////////////////////////////////////////////////////////////////////////////////////
//read specified register on slave device
uint32_t I2CReceive(uint32_t slave_addr, uint8_t reg)
{
//specify that we are writing (a register address) to the
//slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//specify register to be read
I2CMasterDataPut(I2C0_BASE, reg);
//send control byte and register address byte to slave device
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
//specify that we are going to read from slave device
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, true);
//send control byte and read from the register we
//specified
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
//wait for MCU to finish transaction
while(I2CMasterBusy(I2C0_BASE));
//return data pulled from the specified register
return I2CMasterDataGet(I2C0_BASE);
}
///////////////////////////////////////////////////////////////////////////////////////////
//sends an I2C command to the specified slave
void I2CSend(uint8_t slave_addr, uint8_t num_of_args, ...)
{
// Tell the master module what address it will place on the bus when
// communicating with the slave.
I2CMasterSlaveAddrSet(I2C0_BASE, slave_addr, false);
//stores list of variable number of arguments
va_list vargs;
//specifies the va_list to "open" and the last fixed argument
//so vargs knows where to start looking
va_start(vargs, num_of_args);
//put data to be sent into FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint8_t));
//if there is only one argument, we only need to use the
//single send I2C function
if(num_of_args == 1)
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//"close" variable argument list
va_end(vargs);
}
//otherwise, we start transmission of multiple bytes on the
//I2C bus
else
{
//Initiate send of data from the MCU
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//send num_of_args-2 pieces of data, using the
//BURST_SEND_CONT command of the I2C module
int i =1;
for( i = 1; i < (num_of_args - 1); i++)
{
//put next piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint8_t));
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
}
//put last piece of data into I2C FIFO
I2CMasterDataPut(I2C0_BASE, va_arg(vargs, uint8_t));
//send next data that was just placed into FIFO
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
// Wait until MCU is done transferring.
while(I2CMasterBusy(I2C0_BASE));
//"close" variable args list
va_end(vargs);
}
}
万望各位前辈斧正
拜谢
建议你用EV2300连接上机位软件先确认是不是能正常通讯。
除了用EV2300就没有别的办法了吗? 我这里没有EV2300。。。
可不可能是通信代码有问题,导致无法通信呢?我用GPIOA PA0和PA1 和电脑通信,不会干扰控制器和bq76920通信吧?
找到问题所在了,是扩展和针脚焊接的问题^_^