TI中文支持网
TI专业的中文技术问题搜集分享网站

msp430g2553用I2C读取bq27541,读不出数据

请大侠帮忙看下,实现的功能是按下按键,单片机读取BQ27541里面的SOC,然后显示电量,软件调试OK,用实物调试的时候,读不出数据,IE2不能置位,附件是原理图,请大侠帮忙看看,谢谢

Rongxi LV:

附件是程序文件,用I2C通讯,I2c程序是TI官网推荐程序,软件调试OK,硬件调试的时候,读不出数据,IE2不能置位。以下是程序,附件是I2C程序文件,请大侠帮忙看看,谢谢!

#include "io430g2553.h"#include "io430.h"#include "F:\项目\程序编写\2016\lib\I2C_USCI\TI_USCI_I2C_master.h"#include "bq27541.h"#define _I2C_USCI#define I2CSLAVEADDR           0x55           // 7-bit slave address#define    uint    unsigned int#define BUFFERSIZE             32           // # of bytes for Tx & Rx buffersunsigned char TxData[BUFFERSIZE];           // Stores data bytes to be TX'dunsigned char RxData[BUFFERSIZE];           // Stores data bytes that are RX'dunsigned int  SOC;                          // Stores State of Chargeunsigned int transBytes2UnsignedInt(unsigned char msb, unsigned char lsb);void MSP430_bq27541_read(unsigned char cmd, unsigned int bytes);unsigned int transBytes2UnsignedInt(unsigned char msb, unsigned char lsb){  unsigned int tmp;    tmp = ((msb << 8) & 0xFF00);  return ((unsigned int)(tmp + lsb) & 0x0000FFFF);  }void MSP430_bq27541_read(unsigned char cmd, unsigned int bytes){  unsigned char tx[1];  tx[0] = cmd;  TI_USCI_I2CWrite(I2CSLAVEADDR, 1, 1, tx);  TI_USCI_I2CRead(I2CSLAVEADDR, bytes, 0, RxData); }void main(void){    WDTCTL = WDTPW +WDTHOLD;                  // Stop Watchdog Time    BCSCTL1 = CALBC1_1MHZ;                    // Set DCO to 1 MHz    DCOCTL = CALDCO_1MHZ;    P1IES |= BIT0;                            // P1.0选择下降沿中断    P1IE  |= BIT0;                            // 打开中断使能    P2DIR |= BIT1+BIT2+BIT3+BIT4;             // 设置P2.1 ,2.2 ,2.3, 2.4 为输出状态    P2OUT |= BIT1+ BIT2 + BIT3 + BIT4;        // 设置P2.1 ,2.2 ,2.3, 2.4 输出为高    for(unsigned int i=0;i<65000;i++);                             // 延时     P2OUT &= ~(BIT1+ BIT2 + BIT3 + BIT4);     // 设置P2.1 ,2.2 ,2.3, 2.4 输出为低   while (1)    {            __enable_interrupt();       TI_USCI_I2C_transmitinit(I2CSLAVEADDR,I2CCLOCKPRESCALER);       while ( TI_USCI_I2C_notready() );       MSP430_bq27541_read(bq27541CMD_SOC_LSB, 2);       SOC = transBytes2UnsignedInt(RxData[1], RxData[0]);       __bis_SR_register(SCG1+SCG0+CPUOFF + GIE);       P2OUT &= ~(BIT1+ BIT2 + BIT3 + BIT4);    }}#pragma vector = PORT1_VECTOR__interrupt void  PORT1_ISR(void){         if(P1IFG & 0x01)      {         for(unsigned int i=0;i<6000;i++);         if(P1IFG & 0x01)         {            P1IFG ^= 0x01;            __enable_interrupt();            TI_USCI_I2C_transmitinit(I2CSLAVEADDR,I2CCLOCKPRESCALER);            while ( TI_USCI_I2C_notready() );            MSP430_bq27541_read(bq27541CMD_SOC_LSB, 2);            SOC = transBytes2UnsignedInt(RxData[1], RxData[0]); //         SOC = 90;          if (SOC == 0)          {            while(!(P1IN & BIT0))            {               P2OUT |= BIT1 + BIT4;            }          }                      else if (0<SOC <= 25)          {                 while (!(P1IN & BIT0))             {                P2OUT ^= BIT1;                for(unsigned int i=0;i<35000;i++);             }          }          else if(25<SOC && SOC<=55)          {             while (!(P1IN & BIT0))             {               P2OUT |= BIT1+ BIT2;             }          }          else if(55<SOC && SOC<=85)          {             while (!(P1IN & BIT0))             {               P2OUT |= BIT1+ BIT2 + BIT3;             }          }          else if(85<SOC)          {             while (!(P1IN & BIT0))             {                P2OUT |= BIT1+ BIT2 + BIT3 + BIT4;             }          }        }      }  __bic_SR_register_on_exit(SCG1+SCG0+CPUOFF);}

赞(0)
未经允许不得转载:TI中文支持网 » msp430g2553用I2C读取bq27541,读不出数据
分享到: 更多 (0)