void I2C_IO_Init()
{
P3SEL |=(BIT1+BIT2);
UCB0CTL1 |=UCSWRST;
UCB0CTL0 =(UCMST+UCMODE_3+UCSYNC);
UCB0CTL1 =(UCSSEL_2+UCSWRST);
UCB0BR0 =120;
UCB0BR1 =0;
UCB0CTL1 &=~UCSWRST;
}
//============================================================================================================
void WriteByte(uint8 addr,uint8 data)
{
UCB0I2CSA =0x38; // Slave Address is 38h
while(UCB0CTL1&UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |=UCTXSTT+UCTR; // I2C TX, start condition
while(!(UC0IFG&UCB0TXIFG)); // Wait USCI_B0 TX flag Flag reset automaticall
UCB0TXBUF =addr; // Load TX buffer
while(UCB0CTL1&UCTXSTT); //检测从机ACK信号,有ACK信号,则UCTXSTT自动清零
//以上顺序不能改变
while(!(UC0IFG&UCB0TXIFG));
UCB0TXBUF =data;
while(!(UC0IFG&UCB0TXIFG));
UCB0CTL1 |=UCTXSTP; // I2C stop condition
UC0IFG &=~UCB0TXIFG; // Clear USCI_B0 TX int flag
Delay1us(27056);
}
//============================================================================================================
void Readpage(uint8 *RamAddress,uint8 addr,uint8 bytes)
{
UCB0I2CSA =0x38; // Slave Address is 38h
while (UCB0CTL1&UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |=UCTR+UCTXSTT; // I2C TX, start condition
while(!(UC0IFG&UCB0TXIFG)); // Wait USCI_B0 TX flag Flag reset automatically
UCB0TXBUF =addr; // Load TX buffer
while(UCB0CTL1&UCTXSTT) ;
while(!(UC0IFG&UCB0TXIFG));
UC0IFG &=~UCB0TXIFG;
UCB0CTL1 &=~UCTR;
while(UCB0CTL1&UCTXSTP);
UCB0CTL1 |=UCTXSTT;
while(UCB0CTL1&UCTXSTT);
while(bytes!=1)
{
while(!(UC0IFG&UCB0RXIFG));
*RamAddress =UCB0RXBUF;
UCB0CTL1 &=~UCTXNACK;
RamAddress++;
bytes--;
}
UCB0CTL1 |=UCTXSTP+UCTXNACK; //发送停止信号必须再读取最后一个数据之前
while(!(UC0IFG&UCB0RXIFG));
*RamAddress =UCB0RXBUF;
//UCB1CTL1 |= UCTXSTP+UCTXNACK; 这里放就不可以了
while(UCB0CTL1&UCTXSTP);
Delay1us(27056);
}
//============================================================================================================
void Init_HMC5883()
{
WriteByte(0x02,0x00); //
}
代码如上,当执行Init_HMC5883()时(HMC5838L的地址是0x1c),调用void WriteByte(uint8 addr,uint8 data)子函数,程序将会吊死在该子函数的第七行while(!(UC0IFG&UCB0TXIFG)); 请教高手帮忙解答一下。
另外,我多方搜索,始终找不到TI给出的关于MSP430F2系列的硬件IIC程序。我们公司现在要开发一款新型罗盘,需要用的这IIC接口,模拟出来的IIC无法使用。专家能否提供一下MSP430F2系列的硬件IIC,最好能提供中断和查询方式各一个成功的例子?急急急。