This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
工具/软件:Code Composer Studio
#include
#include
#include
字符 E = 0;
INT A = 0;
char itgAddress = 0x69;
void init_I2C (void);
void transmit (void);
接收无效(无效);
int main (void){
initMSP();
WDTCTL = WDTPW + WDTHOLD;
unsigned char data[5];
I2CSensorConfiguration();
int i = 0;
while (1){
ReadBMI160 (&data[0]);
对于(i = 0;i < 5;i++){
serialPrintln ("Data[");
serialPrintInt (I);
serialPrintln ("]);
serialPrintInt (data[i]);
}
}
}
///----------------------------------
// USCI_B0数据 ISR 用于移动从 I2C 从设备接收到的数据
//到 MSP430内存。 它的结构使其可用于接收
///----------------------------------
void init_I2C (void){
P1SEL |= BIT6 + BIT7;//将 I2C 引脚分配给 USCI_B0
P1SEL2 |= BIT6 + BIT7;//将 I2C 引脚分配给 USCI_B0
UCB0CTL1 |= UCSWRST;//启用 SW 复位
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;// I2C 主器件、同步模式
UCB0CTL1 = UCSSEL_2 + UCSWRST;//使用 SMCLK、保持软件复位
UCB0BR0 = 10;// fSCL = SMCLK/12 =~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0;//从器件地址为069h
UCB0I2COA = 0;//从地址为069h
UCB0CTL1 &=~UCSWRST;//清除 SW 复位,恢复运行
}
unsigned char I2CStartWrite (unsigned char slave_address、
unsigned char first_data){
while ((UCB0STAT & BUSY)!= 0)
;
UCB0I2CSA = SLAVE_ADDRESS;
UCB0CTL1 |= UCTR;
UCB0CTL1 |= UCTXSTT;
UCB0TXBUF = FIRST_DATA;
while ((IFG2 & UCB0TXIFG)=0)
;
if ((UCB0STAT & UCNACKIFG)!= 0){
返回0;
}否则{
返回1;
}
}
unsigned char I2CWrite (unsigned char c){
while ((UCB0STAT & BUSY)!= 0)
;
UCB0TXBUF = c;
while ((IFG2 & UCB0TXIFG)=0)
;
if ((UCB0STAT & UCNACKIFG)!= 0){
返回0;
}否则{
返回1;
}
}
void I2Cstop (void){
while ((UCB0STAT & BUSY)!= 0)
;
UCB0CTL1 |= UCTXSTP;
}
unsigned char I2CStartRead (unsigned int slave_address){
while ((UCB0STAT & BUSY)!= 0)
;
UCB0I2CSA = SLAVE_ADDRESS;
UCB0CTL1 &=~UCTR;
UCB0CTL1 |= UCTXSTT;
while ((UCB0CTL1 & UCTXSTT)!= 0)
;
if ((UCB0STAT & UCNACKIFG)!= 0){
返回0;
}否则{
返回1;
}
}
unsigned char I2CRead8 (void){
unsigned char 数据;
while ((UCB0STAT & BUSY)!= 0)
;
while ((IFG2 & UCB0RXIFG)=0)
;
数据= UCB0RXBUF;
返回数据;
}
void ReadBMI160 (unsigned char *数据){
unsigned char aux;
init_i2C();
I2CStartWrite (itgAddress、0xFF);
I2CStartRead (itgAddress);
对于(aux = 0;aux < 5;aux++、data++){
*data = I2CRead8();
}
UCB0CTL1 |= UCTXNACK;
I2Cstop();
*data = I2CRead8();
_DELAY_CYCLES (15);
I2Cstop();
}
void I2CSensorConfiguration (void){
init_i2C();
I2CStartWrite (itgAddress、0x40);
I2CWrite (0x2B);
I2CWrite (0x03);
I2CWrite (0x06);
I2CWrite (0x00);
I2Cstop();
}