msp430f5436_A读取stc3115电池电压
时间:10-02
整理:3721RD
点击:
现在要用msp430f5436_A用I2C读取stc3115的电压,代码如下
void i2c_init(void)
{
P9SEL |= 0x06; //配置I2C管脚P9.1,P9.2,但是暂时未配置P9.0,待看此管脚功能
UCB2CTL1 |= UCSWRST; //复位所有寄存器
UCB2CTL0 |= UCMST +UCMODE_3 +UCSYNC; //I2C主设备同步通信模式
UCB2CTL1 |= UCSWRST + UCSSEL_2; //时钟为SMCLK,保持复位
UCB2BR0 =12; //通信频率设置
UCB2BR1=0;
UCB2CTL0 &=~UCSLA10; //7位地址模式
UCB2I2CSA= 0x00E0; //此处应写从设备地址 0xe0
UCB2CTL1 &= ~UCSWRST;
}
void i2c_startwrite(unsigned int deviceaddress)
{
UCB2I2CSA=deviceaddress;
while(UCB2CTL1 & UCTXSTP);
UCB2CTL1 |= UCTR + UCTXSTT;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
void i2c_startread(unsigned int deviceaddress)
{
UCB2I2CSA=deviceaddress;
while(UCB2CTL1 & UCTXSTP);
UCB2CTL1 &= ~UCTR;
UCB2CTL1 |= UCTXSTT;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
void i2c_write1byte(unsigned char wdata) /
{
UCB2TXBUF=wdata;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
unsigned char Ucb0i2c_read1byte(void) //problem
{
unsigned char Rdata;
UCB2CTL1 &= ~UCTR; //i2c接受模式
UCB2CTL1 |= UCTXSTT; //发送一个开始信号
while(!(UCB2IFG & UCRXIFG)); //等待接受完成
UCB2IFG &= ~UCRXIFG; //清除USCI_B0接收标志位
UCB2CTL1 |= UCTXSTP;
Rdata= UCB2RXBUF;
return Rdata;
}
unsigned char i2c_read1byte(unsigned int dataaddress, unsigned int deviceaddress)
{
volatile unsigned char Rdata1,Rdata2,Rdata;
i2c_startwrite(deviceaddress);
//i2c_write1byte((unsigned char)(dataaddress>>8));
i2c_write1byte((unsigned char)dataaddress);
i2c_startread(deviceaddress);
Rdata1=Ucb0i2c_read1byte();
Rdata2=Ucb0i2c_read1byte();
Rdata=Rdata1+Rdata2*16;
return Rdata;
}
附上stc3115的I2C时序图
现在问题是从来没有读到电压值,求大佬帮忙看看,万分感激!
void i2c_init(void)
{
P9SEL |= 0x06; //配置I2C管脚P9.1,P9.2,但是暂时未配置P9.0,待看此管脚功能
UCB2CTL1 |= UCSWRST; //复位所有寄存器
UCB2CTL0 |= UCMST +UCMODE_3 +UCSYNC; //I2C主设备同步通信模式
UCB2CTL1 |= UCSWRST + UCSSEL_2; //时钟为SMCLK,保持复位
UCB2BR0 =12; //通信频率设置
UCB2BR1=0;
UCB2CTL0 &=~UCSLA10; //7位地址模式
UCB2I2CSA= 0x00E0; //此处应写从设备地址 0xe0
UCB2CTL1 &= ~UCSWRST;
}
void i2c_startwrite(unsigned int deviceaddress)
{
UCB2I2CSA=deviceaddress;
while(UCB2CTL1 & UCTXSTP);
UCB2CTL1 |= UCTR + UCTXSTT;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
void i2c_startread(unsigned int deviceaddress)
{
UCB2I2CSA=deviceaddress;
while(UCB2CTL1 & UCTXSTP);
UCB2CTL1 &= ~UCTR;
UCB2CTL1 |= UCTXSTT;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
void i2c_write1byte(unsigned char wdata) /
{
UCB2TXBUF=wdata;
while(!(UCB2IFG & UCTXIFG));
UCB2IFG &= ~UCTXIFG;
}
unsigned char Ucb0i2c_read1byte(void) //problem
{
unsigned char Rdata;
UCB2CTL1 &= ~UCTR; //i2c接受模式
UCB2CTL1 |= UCTXSTT; //发送一个开始信号
while(!(UCB2IFG & UCRXIFG)); //等待接受完成
UCB2IFG &= ~UCRXIFG; //清除USCI_B0接收标志位
UCB2CTL1 |= UCTXSTP;
Rdata= UCB2RXBUF;
return Rdata;
}
unsigned char i2c_read1byte(unsigned int dataaddress, unsigned int deviceaddress)
{
volatile unsigned char Rdata1,Rdata2,Rdata;
i2c_startwrite(deviceaddress);
//i2c_write1byte((unsigned char)(dataaddress>>8));
i2c_write1byte((unsigned char)dataaddress);
i2c_startread(deviceaddress);
Rdata1=Ucb0i2c_read1byte();
Rdata2=Ucb0i2c_read1byte();
Rdata=Rdata1+Rdata2*16;
return Rdata;
}
附上stc3115的I2C时序图
现在问题是从来没有读到电压值,求大佬帮忙看看,万分感激!
有用, 谢谢