我最近在G2553 Launchpad 用模拟IIC方法调试出 BH1750光照强度模块,现在想实现用硬件IIC查询法调试。为了便于清晰,我只上传了我的 iic初始化和 iic写函数。用CCSV5编译器进行调试的时候,程序总是卡在图中红线位置。不知道为啥啊?真心给跪了。都调试了两天,搞不懂原因,求大神们指点指点小弟!另外还有个问题是,如何收到从机发来的应答信号(ACK)?
/*
* I2C_Init(I2C_MODE i2c_mode, UCSSELx ucsselx)
* 初始化I2C
*
* 参数:
* i2c_mode --I2C主从模式设置
* |__ Slave
* |__ Master
* |
* ucsselx --I2C时钟信号选择
* |__ UCLKI
* |__ ACLK
* |__ SMCLK
* 输出:
* 无
*
*/
void I2C_Init(I2C_MODE i2c_mode, UCSSELx ucsselx)
{
_disable_interrupts();
P1SEL |= BIT6 + BIT7; // GPIO 配置为USCI_B0功能
P1SEL2|= BIT6 + BIT7; // GPIO 配置为USCI_B0功能
// Own address is a 7-bit address | Address slave with 7-bit address | Single master environment
switch(i2c_mode)
{
case Slave:
UCB0CTL0 = UCMODE_3 + UCSYNC; //同步通信I2C从机状态
break;
case Master:
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; //同步通信I2C主机状态
break;
}
switch(ucsselx)
{
case UCLKI:
UCB0CTL1 = UCSSEL_0 + UCSWRST; // Use UCLKI,USCI Software Reset
break;
case ACLK:
UCB0CTL1 = UCSSEL_1 + UCSWRST; // Use ACLK,USCI Software Reset
break;
case SMCLK:
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK,USCI Software Reset
break;
}
UCB0BR0 =I2C_BAUDRATE_DIV ; // 除了分频系数,实际波特率还与时钟有关
UCB0BR1 = 0; // 这一级别的分频一般不启用
UCB0I2CSA = SLAVE_ADDR; // I2C从机地址,可在宏定义中修改
UCB0CTL1 &= ~UCSWRST; // 开启I2C
_enable_interrupts();
}
void I2C_WriteData(uint8 data)
{
I2C_Init(Master,SMCLK);
i2c_delay_ms(1);
while((UCB0CTL1 & UCTXSTP)); // 确保总线空闲
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while((IFG2 & UCB0TXIFG)==0); // UCB0TXIFG is set when UCB0TXBUF is empty.
UCB0TXBUF = SlaveAddress; // Load TX buffer
while((IFG2 & UCB0TXIFG)==0); // UCB0TXIFG is set when UCB0TXBUF is empty.
UCB0TXBUF = data;
while((IFG2 & UCB0TXIFG)==0); // UCB0TXIFG is set when UCB0TXBUF is empty.
UCB0CTL1 |= UCTXSTP; // I2C stop condition
i2c_delay_ms(1);
}
此帖出自
小平头技术问答
建议楼主多分析一下MSP430G2553的用户指南的电子文档,其中457页(17.3.4.2 主控模式)开始详细描述了主控模式的发送和接收过程,并结合了操作时序图。需要一句一句读一读,多几遍就搞定了。
一周热门 更多>