共2条
1/1 1 跳转至页
问
请教关于用MSP430F149控制I2C接口的数字电位器X9241的问题. X9241是+5V工作的CMOS器件, 我用MSP430F149的P3.0与P3.1分别去控制X9241的SDA与SCL , 可是不行. MSP430F149送出的明明是1和0,可是测量所得的不一定是1和0. 此时, X9241的电压为3V ( 因为它是CMOS器件, +3V它可能会工作的吧 ) . 在SDA与SCL引脚分别用一个10K的电阻上拉至+3V. 后来, 考虑到可能X9241在+3V工作不正常, 就将它的电源电压改为+5V, 并在MSP430F149与X9241之间用74LVC4245进行电平转换, 在SDA与SCL引脚用10K电阻上拉到+5V , 并用MSP430F149的P4.4去控制74LVC4245的DIR, 可是仍然会出现上面所说的问题. 不知是什么原因, 还请各位大侠指点指点. 我的程序如下:
#include<MSP430x14x.h>
#include"include.h"
#define TIME 30
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P4DIR |=BIT4;
P4OUT &=~BIT4;
P3DIR |=BIT0;
P3DIR |=BIT1;
P3OUT |=BIT0;
P3OUT |=BIT1;
RF_ISendStr(0x50,0xC0,0x03); //写初始化数据到第0个数字电位器的R0
RF_ISendStr(0x50,0xA0,0x03); //写初始化数据到第0个数字电位器的WCR
for (;;)
{
//_BIS_SR(CPUOFF); // CPU off
_NOP(); // Required only for C-spy
}
}
INT8U RF_ISendStr(INT8U sla,INT8U suba,INT8U data) //三字节指令
{
INT8U ack;
I2C_RF_INIT();
I2C_RF_Start();
ack=I2C_RF_SendByte(sla);
if(ack==0) return(0);
ack=I2C_RF_SendByte(suba);
if(ack==0) return(0);
ack=I2C_RF_SendByte(data);
if(ack==0) return(0);
I2C_RF_Stop();
return(1);
}
INT8U I2C_RF_SendByte(INT8U c)
{
INT8U BitCnt,ack;
P3DIR |=BIT0; //SDA is out
for(BitCnt=0;BitCnt<8;BitCnt++)
{
P3OUT &=~BIT1;
Delay(TIME);
if((c<<BitCnt)&0x80)
{
P3OUT |=BIT0;
}
else
{
P3OUT &=~BIT0;
}
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
}
P3OUT &=~BIT1;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P4OUT |=BIT4;
P3DIR &=~BIT0;
Delay(TIME);
Delay(TIME);
Delay(TIME);
if(P3IN&0x01)
ack=0;
else
ack=1;
P3OUT &=~BIT1;
Delay(TIME);
P4OUT &=~BIT4;
P3DIR |=BIT0;
Delay(TIME);
return(ack);
}
void I2C_RF_INIT(void)
{
P3DIR |=BIT0;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
P3DIR |=BIT1;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
}
void Delay(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void I2C_RF_Start(void)
{
P3OUT |=BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P3OUT &=~BIT0;
Delay(TIME);
P3OUT &=~BIT1;
Delay(TIME);
}
void I2C_RF_Stop(void)
{
P3OUT &=~BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
} 答 1: 顶上来。这个问题已经困惑我很长时间了,还请大侠们多多指点,先谢了。
#include<MSP430x14x.h>
#include"include.h"
#define TIME 30
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
P4DIR |=BIT4;
P4OUT &=~BIT4;
P3DIR |=BIT0;
P3DIR |=BIT1;
P3OUT |=BIT0;
P3OUT |=BIT1;
RF_ISendStr(0x50,0xC0,0x03); //写初始化数据到第0个数字电位器的R0
RF_ISendStr(0x50,0xA0,0x03); //写初始化数据到第0个数字电位器的WCR
for (;;)
{
//_BIS_SR(CPUOFF); // CPU off
_NOP(); // Required only for C-spy
}
}
INT8U RF_ISendStr(INT8U sla,INT8U suba,INT8U data) //三字节指令
{
INT8U ack;
I2C_RF_INIT();
I2C_RF_Start();
ack=I2C_RF_SendByte(sla);
if(ack==0) return(0);
ack=I2C_RF_SendByte(suba);
if(ack==0) return(0);
ack=I2C_RF_SendByte(data);
if(ack==0) return(0);
I2C_RF_Stop();
return(1);
}
INT8U I2C_RF_SendByte(INT8U c)
{
INT8U BitCnt,ack;
P3DIR |=BIT0; //SDA is out
for(BitCnt=0;BitCnt<8;BitCnt++)
{
P3OUT &=~BIT1;
Delay(TIME);
if((c<<BitCnt)&0x80)
{
P3OUT |=BIT0;
}
else
{
P3OUT &=~BIT0;
}
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
}
P3OUT &=~BIT1;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P4OUT |=BIT4;
P3DIR &=~BIT0;
Delay(TIME);
Delay(TIME);
Delay(TIME);
if(P3IN&0x01)
ack=0;
else
ack=1;
P3OUT &=~BIT1;
Delay(TIME);
P4OUT &=~BIT4;
P3DIR |=BIT0;
Delay(TIME);
return(ack);
}
void I2C_RF_INIT(void)
{
P3DIR |=BIT0;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
P3DIR |=BIT1;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
}
void Delay(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++);
}
void I2C_RF_Start(void)
{
P3OUT |=BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P3OUT &=~BIT0;
Delay(TIME);
P3OUT &=~BIT1;
Delay(TIME);
}
void I2C_RF_Stop(void)
{
P3OUT &=~BIT0;
Delay(TIME);
P3OUT |=BIT1;
Delay(TIME);
P3OUT |=BIT0;
Delay(TIME);
} 答 1: 顶上来。这个问题已经困惑我很长时间了,还请大侠们多多指点,先谢了。
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |