HI,TI
MSP430G2755做主机模拟I2C读取BQ40Z50已通讯成功。现需要实现430 USCI硬件I2C做从机,地址为0X16,用EV2300去读,430程序总是跑飞,一直执行RX中断。下面是通讯测试code,请TI大神帮忙看看,困了一周了。
TI有没有EV2300做主机,430做从机的USCI I2C例程。
#include "io430.h"
unsigned char *PRxData; // Pointer to RX data
unsigned char RXByteCtr;
volatile unsigned char RxBuffer[128]; // Allocate 128 byte of RAM
#define CPU_F ((double)1000000)//1MHZ
//#define CPU_F ((double)8000000)//8MHZ
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz 频率校准
DCOCTL = CALDCO_1MHZ;
BCSCTL1 &=~XTS; //XT1低频模式
BCSCTL1 |= DIVA_0 + XT2OFF; //关闭XT2,XT1不分频
BCSCTL2 |= SELM_0 + DIVM_0 + DIVS_0;//+ SELS //MCLK选择DCO时钟,不分频,SMCLK选择DCO,不分频
BCSCTL3 |= XT2S_2 + LFXT1S_2; //XT2频率范围为3MHz到16MHz,ACLK为VLO=12KHz
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 |= UCMODE_3 + UCSYNC; // I2C Slave, synchronous mode
UCB0CTL1 |= UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 10; // Set prescaler 1M/10~100KHZ
UCB0BR1 = 0;
UCB0I2COA = 0x0B; // Own Address 0x16>>1
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0I2CIE |=UCSTPIE + UCSTTIE; // Enable STT, STP interrupt
IE2 |= UCB0RXIE; // Enable RX TX interrupt
P2DIR|=BIT3;//P2.3设置为输出
P2OUT|=BIT3;
P3DIR|=BIT5;//P3.5设置为输出
P3OUT|=BIT5;
__enable_interrupt(); // Enable GIE bit
while(1)
{
P3OUT^=BIT5;
delay_ms(100);
}
}
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
RxBuffer[RXByteCtr]= UCB0RXBUF; // Get RX'd byte into buffer
RXByteCtr++;
}
yue feng1:
打开BQ上位机后程序一直在执行RX中断,while跑飞
灰小子:
回复 yue feng1:
可能上位机一直有数据发出。你用逻辑分析仪观察一下是不是一直有数据
Butterfly:
回复 灰小子:
给你个I2C做从机的例子
yue feng1:
回复 Butterfly:
Thanks,Butterfly:
EV2300可以读到数据了,但是bqstudio提示一直连不上,advanced comm SMB可以读到数据。PACKet checksum mismatch;
另,EV2300读取从机是带有PEC的吗,那从机也必须做PEC处理。
还有,我MSP430做主机读取BQ40Z50时,read处理放在定时器1S定时读取,会导致定时器定时不准,程序发生紊乱。而放在while中无异常。
Butterfly:
回复 yue feng1:
定时器定时精度和输入的时钟源有关系,内部时钟DCO和VLO精度都差点,定时精度有要求的话得用外部LFXT1CLK: