|
MSP430F1611内部IIC给跪了!求帮助啊。
[复制链接]
使用1611内部的IIC模块读取ADS1100,读回的数据不对.
调了好久了,也看了好久IIC,1100的文档,实在不知道怎么回事了,
求帮助啊
程序如下:
void main(void)
{
MasterClock_XT2(8000000);
_DINT();
//内部DA输出;
VerefDac12Init();
DacSetVolate(1.5);
I2C_Init();//IIC初始化;
I2C_TxStr(0x90,0x8c);//写配置;1100内部寄存器配置
_EINT();
for(;;)
{
I2C_RxStr(0x8c);//1100内部寄存器再次配置;
}
}
void I2C_Init(void)
{
P3SEL |= 0x0a; //Assign I2C pins to module
P3DIR &= ~0x0A;//确保IIC总线的初始化
U0CTL |= I2C + SYNC; //Switch USART0 to I2C mode
U0CTL &= ~I2CEN; //Recommended I2C init procedure
I2CTCTL |= I2CSSEL_2 ;//+ I2CWORD; //SMCLK ,8m
I2CPSC = 79;
I2CSCLH = 0x03; //High period of SCL
I2CSCLL = 0x03; //Low period of SCL
U0CTL |= MST; // Master
I2CSA= (0x90>>1);//直接初始化从机地址;
U0CTL |= I2CEN;// Enable I2C
}
//I2C发送一字节数据
void I2C_TxByte(unsigned char cdata)
{
while(!(I2CIFG & TXRDYIFG));//等待发送准备好信号 准备好写了, TXRDYIFG 置位;
I2CDRB = cdata;//发送1字节数据
}
//I2C接收一字节数据
unsigned char I2C_RxByte(void)
{
while(!(I2CIFG & RXRDYIFG));//等待接收准备好信号 准备好读了, XRDYIFG 置位;
return(I2CDRB);//返回接受到的数据
}
void I2C_TxStr(unsigned char SubAddr,unsigned char SetAddr) //I2C_TxStr(0x90)
{
while(I2CTCTL & I2CSTP); // 等待发送停止;发送停止时自动清零;To prevent Arbitration Lost (防止失去仲裁)
I2CNDAT = 2; // 在启动STT后不能轻易改变;发送两个字节后停止;0x90.0x8c.
I2CTCTL |= I2CTRX; //发送模式
I2CTCTL |= I2CSTT + I2CSTP;
I2C_TxByte(SubAddr);//子地址,0x90写操作;
I2C_TxByte(SetAddr); //配置1100内部寄存器
}
void I2C_RxStr(unsigned char SlaverAddr,unsigned char SubAddr,unsigned char SetAddr)
{
unsigned char i;
while (I2CDCTL & I2CBUSY);
I2CNDAT = 1; // Transmit one byte
U0CTL |= MST;//改动这句话对输出值影响很大;
//不清会死在读的那里
I2CIFG &= ~TXRDYIFG; //清除发送准备好标志位 ;
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CTRX; //发送模式;
I2CTCTL &= ~I2CSTP; //清除I2C停止操作位
I2CTCTL |= I2CSTT; //启动发送 这里不能写成I2CTCTL |= I2CSTT + I2CSTP;,否则读不出来,不知为什么
I2C_TxByte(SetAddr); //0x80,再次配置一下寄存器,如果这里写0x91,出来的数据与之前不同,但也是不对的
while (!(I2CIFG & ARDYIFG)); //等待数据发送完成
I2CNDAT = 2; // recive two byte
//接受数据;
I2CTCTL &= ~I2CTRX; //清除发送位,切换到接受模式
I2CIFG &= ~ARDYIFG; // clear Access ready interrupt flag
I2CTCTL |= I2CSTT + I2CSTP;//In master mode, transmit and receive operation is controlled with the
for(i = 0;i<2;i++)
{
str=I2C_RxByte();//ADS1100 16位,分两次接收
}
ADS1100Get = (str[1]<<8)+str[0];//(str<<8);// + str;
ADS1100Val = (ADS1100Get/65535.0)*5.0;
}
|
|