学430单片机自带的IIC模块使用,现在编了段小程序,可是运行不正确,程序如下,问题在最后面。
//初始化IIC模块
UCB0CTL1 |= UCSWRST;
UCB0CTL0 |=UCMST+UCMODE1+UCMODE0+UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCMODE1+UCMODE0+UCSWRST; // Use SMCLK, keep software reset
P3SEL |=0x06; //p3.1为UCB0SDA,P3.2为UCB0SCL
UCB0CTL1 = UCSSEL_3+UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 =10; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear software reset, resume operation
//写函数
UCB0CTL0 |=UCMST+UCMODE_3+UCSYNC; // I2C Master, synchronous mode
//UCB0CTL1 = UCMODE1+UCMODE0+UCSWRST; // Use SMCLK, keep software reset
UCB0I2CSA = 0x3a; //这个地址是真实地址后面加读/写位组成
UCB0CTL0 &=~UCSLA10; //从机地址为7位
UCB0CTL1 |= UCTR;//transmitter mode 发送模式
UCB0CTL1 |= UCTXSTT; //start condition,
while((IFG2 & UCB0TXIFG) != UCB0TXIFG);//检测UCB0TXBUF是否为空,为空则UCB0TXIFG为1.
for(i=0;i<1;i++)
{
UCB0TXBUF = 0x0d;//IIC设备中寄存器的地址
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);//检测从机ACK信号,有ACK信号,则UCTXSTT自动清零//以上顺序不能改变
while((IFG2 & UCB0TXIFG) != UCB0TXIFG);//检测UCB0TXBUF是否为空,
}
UCB0CTL1 |= UCTXSTP;//发送STOP信号 |