我试图用msp430G2 launchpad与 DS3231模块进行I2c的时候出现以下问题:
当我执行初始化程序后,有时我执行完 UCB0CTL1 &= ~UCSWRST后,UCBBUSY就一直是1,后来我用电表测量,又做了机组对照,发现以下问题:
1. 与硬件IIC模块有关的程序,当下载进MCU后,SDA总线就一直处于低电平。
2.经过对照,应该是DS3231拉低了总线,但是这应该不是DS3231的问题,因为其他模块也会出现类似的问题。
3.有些代码开始无法运行,但是重新上电就可以了。但是还有一部分代码不行。但是我认为应该不是代码的问题,因为总线拉低是出现在某些代码发挥作用之前。
比如:
#include <msp430.h>
#define DS3231ADDR 0X68
unsigned int TEMP[9];
unsigned int i = 0;
unsigned int TEMP[9];
unsigned int i = 0;
void I2CINIT();
void DS3231RX(unsigned char ADDR);
void DS3231TX(unsigned char ADDR, unsigned DATA);
void DS3231RX(unsigned char ADDR);
void DS3231TX(unsigned char ADDR, unsigned DATA);
void main()
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
I2CINIT();
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
I2CINIT();
DS3231TX(0X03, 0X02);
DS3231RX(0X03);
DS3231RX(0X00);
DS3231RX(0X03);
DS3231RX(0X00);
I2CINIT();
//尤其注意初始化这句,如果加上这句,那么单片机上电后,sda就会被立刻拉低。而去掉这句,重新上电以后就可以运行。
//不止这一句会产生这样的影响,比如曾经我想套用一个例程,我只是把停止位换了下位置(方便其他函数调用),调用后补上停止位,也发生了上述情况
DS3231RX(0X01);
P1DIR |= BIT0;
P1OUT |= BIT0;
while(1);
}
P1DIR |= BIT0;
P1OUT |= BIT0;
while(1);
}
void I2CINIT()
{
P1SEL |= BIT6 + BIT7;
P1SEL2|= BIT6 + BIT7;
{
P1SEL |= BIT6 + BIT7;
P1SEL2|= BIT6 + BIT7;
UCB0CTL1 |= UCSWRST;
UCB0CTL1 = UCSSEL_2 + UCSWRST;
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
UCB0CTL1 = UCSSEL_2 + UCSWRST;
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
UCB0BR0 = 14;
UCB0BR1 = 0;
UCB0I2CSA = 0X68;
UCB0BR1 = 0;
UCB0I2CSA = 0X68;
UCB0CTL1 &= ~UCSWRST;
}
}
void DS3231TX(unsigned char ADDR, unsigned DATA)
{
while ((UCB0STAT & UCBBUSY)||(UCB0CTL1&UCTXSTP));
UCB0CTL1 |= UCTXSTT + UCTR;
{
while ((UCB0STAT & UCBBUSY)||(UCB0CTL1&UCTXSTP));
UCB0CTL1 |= UCTXSTT + UCTR;
while(!(IFG2 & UCB0TXIFG));
UCB0TXBUF = ADDR;
while(!(IFG2 & UCB0TXIFG));
UCB0TXBUF = DATA;
UCB0TXBUF = ADDR;
while(!(IFG2 & UCB0TXIFG));
UCB0TXBUF = DATA;
while(!(IFG2 & UCB0TXIFG));
UCB0CTL1 |= UCTXSTP;
}
UCB0CTL1 |= UCTXSTP;
}
void DS3231RX(unsigned char ADDR)
{
while ((UCB0STAT & UCBBUSY)||(UCB0CTL1&UCTXSTP));
UCB0CTL1 |= UCTXSTT + UCTR;
{
while ((UCB0STAT & UCBBUSY)||(UCB0CTL1&UCTXSTP));
UCB0CTL1 |= UCTXSTT + UCTR;
while(!(IFG2 & UCB0TXIFG));
UCB0TXBUF = ADDR;
UCB0TXBUF = ADDR;
while(!(IFG2 & UCB0TXIFG));
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT;
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT;
while(!(IFG2 & UCB0RXIFG));
TEMP[i] = UCB0RXBUF;
i++;
while(!(IFG2 & UCB0RXIFG));
TEMP[i] = UCB0RXBUF;
i++;
TEMP[i] = UCB0RXBUF;
i++;
while(!(IFG2 & UCB0RXIFG));
TEMP[i] = UCB0RXBUF;
i++;
while(!(IFG2 & UCB0RXIFG));
TEMP[i] = UCB0RXBUF;
i++;
while(!(IFG2 & UCB0RXIFG));
UCB0CTL1 |= UCTXSTP;
TEMP[i] = UCB0RXBUF;
i++;
while(!(IFG2 & UCB0RXIFG));
UCB0CTL1 |= UCTXSTP;
}
灰小子:
楼主,你好。能否上传下你的电路图看看
Ling Zhu2:
IIC加上拉了么?
shayne ward:
回复 Ling Zhu2:
juring_2016
IIC加上拉了么?
shayne ward:
回复 灰小子:
dirtwillfly
楼主,你好。能否上传下��的电路图看看