最近几天在用MSP430F5438的硬件IIC跟FM24CL64做通信,所用的程序基本就是用TI的官方例程改编的,实现的功能是不断往存储器地址为0x01的空间内写入数据0x01
代码如下:
#include "msp430f5438.h"
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
P3DIR |= 0xf0;
P3OUT &= 0x0f; //p3.4 5 6 7 输出低电平 for A0 A1 A2 & WP
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 255; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 255;
UCB0I2CSA = 0x50; // Slave Address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
while(1)
{
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while(!(UCB0IFG&UCTXIFG)); // Wait before interrupts
UCB0TXBUF = 0x01; // Load TX buffer
//执行完这一句话之后UCTXIFG就自动清零了
while(!(UCB0IFG&UCTXIFG));
UCB0TXBUF = 0x01;
while(!(UCB0IFG&UCTXIFG));
UCB0CTL1 |= UCTXSTP; // I2C stop condition
UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag
}
}
但实际跑起来发现第一次运行完UCB0TXBUF = 0x01; 之后UCTXIFG就自动清零了。如果换成标准例程那种利用中断的写法的话,UCB0TXBUF = 0x01;之后就无法再次进入中断程序了 |