大家好!我在对MSP430g2553的I2C调试,参考的是msp430g2xx3_uscib0_i2c_12.c示例代码,The master transmits to the slave, then a repeated start is generated followed by a receive operation. This is the master code.
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
while(1){
//Transmit process
Setup_TX();
RPT_Flag = 1;
Transmit();
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//Receive process
Setup_RX();
Receive();
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
}
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if(RX == 1){ // Master Recieve?
RXByteCtr--; // Decrement RX byte counter
if (RXByteCtr)
{
*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
}
else
{
if(RPT_Flag == 0)
UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition
if(RPT_Flag == 1){ // if Repeated Start: do nothing
RPT_Flag = 0;
}
*PRxData = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}}
else{ // Master Transmit
if (TXByteCtr) // Check TX byte counter
{
UCB0TXBUF = MSData++; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
if(RPT_Flag == 1){
RPT_Flag = 0;
PTxData = &MSData; // TX array start address
TXByteCtr = NUM_BYTES_TX; // Load TX byte counter
__bic_SR_register_on_exit(CPUOFF);
}
else{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}
}
}
我将程序流程理一下,大家看哪里出现问题:
(1)程序首先对主机发送模式进行配置,当完成后,调用Transmit(),开中断,此时由于TxBUF为空,发送中断标志位置一。进入中断,不断发送MSData++数据。当TXByteCtr为零,进入RPT_Flag=1语句,此时TXByteCtr重新被赋值,跳出低功耗。
我想问一下,此时总中断没关,TxBUF为空,那么会再次进入到发送中断,程序不是进行不下去了。
(2)就算程序会运行到接收程序,就会跳过下面的程序,它始终不会被执行,那么
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
代码的作用是什么?
(3)另外对于repeat start模式,是在主机发送完数据后,当从机有/ACK信号时,再次将主机设置为接收和STT,那什么时候从机会发出/ACK信号?发送和接受模式切换,STT信号什么时间给?
(4)对于接受数据时,前面发送的中断已经将RPT_Flag置零了,那么下面的判断有什么作用?
if(RPT_Flag == 1){ // if Repeated Start: do nothing
RPT_Flag = 0;
}
谢谢了!