主题中讨论的其他器件:BQ34Z100
大家好、
从 bq34z100读取数据会卡在中断中、无法跳出。
请查看以下代码:
#include <msp430fr2433.h>
#include <stdint.h>
#define DEVICE_ADDR 0x55 //Address of BQ34Z100 chip
#define SDA_PIN BIT7 // Program the SDA data line to use pin P1.7
#define SCL_PIN BIT6 // Program the SCL clock line to use the P1.6 pin
//Function declaration
void i2c_write(uint8_t reg_addr, uint8_t data);
void i2c_read(uint8_t reg_addr, uint8_t *rx, uint8_t bytes);
uint16_t soc, soh, cc, rm;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Turn off the watchdog, otherwise the CPU will be reset
// Configure the P1.6 and P1.7 pins for I2C functional mode
P1SEL0 |= SCL_PIN | SDA_PIN;
P1SEL1 &= ~(SDA_PIN | SCL_PIN);
UCB0CTL1 |= UCSWRST; //Enables software reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //I2C master mode, synchronous communication mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; //Select SMCLK to keep the software reset
UCB0BR0 = 12; //fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = DEVICE_ADDR; //Address of BQ34Z100 chip
UCB0CTL1 &= ~UCSWRST; //Clear the software reset to resume operation
//read SOC
i2c_read(0x02, (uint8_t *)&soc, 2);
//read SOH
i2c_read(0x2e, (uint8_t *)&soh, 2);
//read CC
i2c_read(0x2c, (uint8_t *)&cc, 2);
//read RM
i2c_read(0x04, (uint8_t *)&rm, 2);
return 0;
}
//Write register function
void i2c_write(uint8_t reg_addr, uint8_t data)
{
uint8_t tx_data[2];
tx_data[0] = reg_addr;
tx_data[1] = data;
UCB0CTL1 |= UCTR + UCTXSTT; //Send the start byte and set to write mode
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = tx_data[0]; //Sets the register address
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = tx_data[1]; //Transmit data
while(UCB0STAT & UCBBUSY); //Wait for the transfer to complete
}
//Read register function
void i2c_read(uint8_t reg_addr, uint8_t *rx, uint8_t bytes)
{
UCB0CTL1 |= UCTR + UCTXSTT;
while(!(UCB0IFG & UCTXIFG));
UCB0TXBUF = reg_addr; //Set the register address to be read
//Reinitiates the start signal and prepares to receive data
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT;
while(!(UCB0IFG &=~ UCRXIFG));
uint8_t *data = rx;
int i;
for (i = 0; i < bytes-1; i++)
{
data[i] = UCB0RXBUF;
UCB0CTL1 |= UCTXACK;
while(!(UCB0IFG &=~UCRXIFG)); //Wait for a byte to be received
}
//Determines if an ACK is also required to be sent for reading (based on the ACK returned from the slave)
if (bytes > 1) {
data[i] = UCB0RXBUF;
UCB0CTL1
|= UCTXSTP; //Transmit stop bit flag
} else {
UCB0CTL1
|= UCTXSTP + UCTXNACK; //There is no need to send an ACK again with only one byte
}
}
您能帮助检查这个问题吗? 谢谢。
此致、
樱桃