单步仿真都能过,但是F5直接运行会停在 while(!(IFG2 & UCB0TXIFG)); //判断UCB0TXBUF是否为空 ,查看标志位IFG2 = 0x82,发送数据后,UCB0TXIFG标志被清零。
请各位大神帮忙,在线等!!!感谢!!!!!
void I2C_Init(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗定时器
P1SEL |= (BIT6 + BIT7); // P1.6 = SCL, P1.7=SDA
P1SEL2 |= (BIT6 + BIT7); // P1.6 = SCL, P1.7=SDA
// Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; //复位
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //主模式+IIC模式+同步模式
UCB0CTL1 |= UCSSEL_2 + UCSWRST; //SMCLK+复位
UCB0BR0 = 10; //时钟分频设置(UCBxBR0 + UCBxBR1 × 256)
UCB0BR1 = 0;
UCB0I2CSA = (0x50>>1); //7位地址
UCB0CTL1 &= ~UCSWRST; //清复位标志
__delay_cycles(20000);
__delay_cycles(20000);
}
void I2C_write(unsigned char WriteValH,unsigned char WriteValL)
{
I2C_Init();
UCB0I2CSA = (0x50>>1);
while(UCB0CTL1 & UCTXSTP); //If ICC Stop
UCB0CTL1 |= UCTR;
UCB0CTL1 |= UCTXSTT; //IIC Start + Write
//while((IFG2 & UCB0TXIFG) != UCB0TXIFG); //判断UCB0TXBUF是否为空
//UCB0TXBUF = 0x50; //Send X9119 Address
//UCB0I2CSA = (0x50>>1);
//while((UCB0CTL1 & UCTXSTT) == UCTXSTT); //检测ACK信号,如果有ACK信号,则UCTXSTT自动清零
//__delay_cycles(1000);
//__delay_cycles(1000);
//__delay_cycles(1000);
//__delay_cycles(1000);
while(!(IFG2 & UCB0TXIFG)); //判断UCB0TXBUF是否为空
UCB0TXBUF = 0xA0; //Send X9119 Write
while((UCB0CTL1 & UCTXSTT) == UCTXSTT); //检测ACK信号,如果有ACK信号,则UCTXSTT自动清零
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
while(!(IFG2 & UCB0TXIFG)); //判断UCB0TXBUF是否为空
UCB0TXBUF = WriteValH; //Send H
while((UCB0CTL1 & UCTXSTT) == UCTXSTT); //检测ACK信号,如果有ACK信号,则UCTXSTT自动清零
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
while(!(IFG2 & UCB0TXIFG)); //判断UCB0TXBUF是否为空
UCB0TXBUF = WriteValL; //Send L
while((UCB0CTL1 & UCTXSTT) == UCTXSTT); //检测ACK信号,如果有ACK信号,则UCTXSTT自动清零
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
__delay_cycles(20000);
UCB0CTL1 |= UCTXSTP + UCTXNACK; //IIC Stop + NACK
//UCB0CTL1 |= UCTXSTP ; //IIC Stop
while((UCB0CTL1 & UCTXSTP) == 1);
}
此帖出自
小平头技术问答
波特率也需要注意下,你是做的10分频
一周热门 更多>