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.

[参考译文] CCS/MSP430G2553:我的传感器提供不变的值。

Guru**** 2040210 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/569797/ccs-msp430g2553-my-sensor-gives-non-changing-values

器件型号:MSP430G2553

工具/软件: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();

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    仅仅发布代码并不足够、您还需要提供系统的行为、例如从器件名称、硬件连接、代码挂起、调试工作以及逻辑分析仪或示波器截屏。 您应该从 TI 提供的基本 I2C 代码示例开始、确认基本通信、然后从此处构建您的应用。 e2e.ti.com/.../msp-i2c-getting-started-guide

    此致、
    Ryan