程序如下
#include <msp430f169.h>
#include"Init_clk.h"
#include"cryfuncs.h"
#include"ADS1115.h
void main( )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
Init_clk();
Ini_Lcd(); //LCD初始化
showADS1115();
P3SEL |= 0x0A; // Select I2C pins
U0CTL |= I2C + SYNC; // Recommended init procedure
U0CTL &= ~I2CEN; // Recommended init procedure
I2CTCTL |= I2CSSEL1; // SMCLK, transmit
I2CNDAT = 0x03; // Write Three bytes
I2CSA = 0x48; // Slave Address is 1001000
U0CTL |= I2CEN; // Enable I2C
Confige_1115(0); //通道选择
while(1)
{
Point_reg();
value=Read_1115();
}
}
/**************************************************************************************************/
void Confige_1115(uchar channel )
{
uchar chan;
switch(channel)
{
case 0: chan = 0xc4; break;//11000100
case 1: chan = 0x52; break;//01010010
case 2: chan = 0x62; break;//01100010
case 3: chan = 0x72; //01110010
}
U0CTL |= MST; // Master mode
I2CTCTL |= I2CSTT+I2CSTP+I2CTRX; // Initiate transfer
//I2CNDAT = 0x03;
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to be ready
I2CDRB = 0x01; // Points to config register
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to be ready
I2CDRB = chan; // Load Control Byte
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to be ready /***之前还会卡在这里,后来突然就好了,不得解****/
I2CDRB = 0x83; // Load Control Byte
while ((I2CTCTL & I2CSTP) == 0x02); // To prevent Arbitration Lost
}
/**********************************************************************************************/
uint Read_1115(void)
{
float Voltage;
uint ctlbyte;
U0CTL |= MST; // Master mode
I2CTCTL = I2CSTT+I2CSTP; // Initiate transfer
I2CNDAT = 0x02;
while ((I2CIFG & RXRDYIFG) == 0); // Wait for Receiver to be ready
ctlbyte = I2CDRB; // Receive MSByte from DAC
ctlbyte = ctlbyte << 8;
while ((I2CIFG & RXRDYIFG) == 0); // Wait for Receiver to be ready
ctlbyte = ctlbyte + I2CDRB; // Receive LSByte from DAC
while ((I2CTCTL & I2CSTP) == 0x02); // Wait for Stop Condition
Voltage=ctlbyte*2.048/32768;
Disp_float_num(1,2,Voltage);
return ctlbyte;
}
/****************************************************************************************************/
void Point_reg(void)
{
U0CTL |= MST; // Master mode
I2CTCTL |= I2CSTT+I2CSTP+I2CTRX; // Initiate transfer
I2CNDAT = 0x01;
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to be ready
I2CDRB = 0x00; // Points to conversin register
while ((I2CTCTL & I2CSTP) == 0x02); // Wait for Stop Condition
}
/*************************************************************************************/
void showADS1115(void)
{
Send(0,0×01); /*清屏,将DDRAM的位址计数器调整为“00H”*/
Disp_HZ(0x80,"A=",1);
}
为什么会读不了数??求救QAQ
AIN0什么都不接,读数是4398 接了一个直流电源以后就会不停地跳数但是没有一个对的
而且不论直流电源有没有输出都会跳,为什么?
Martin:
请先检查硬件电路连接是否正常
然后再检查读写时序是否正常,推荐用示波器查看时序然后与书册中的对照
可以读写个寄存器,比如说Config Register,以判断读写时序是否正常
Carter Liu:
showADS1115();在何时更新?
yuan wang3:
回复 Carter Liu:
showADS1115();只初始化一次(A= ),显示数据的代码在Read1115();里面
yuan wang3:
回复 Martin:
读到数了,接地有问题
但是想不明白
while ((I2CIFG & TXRDYIFG) == 0); // Wait for transmitter to be ready /***之前还会卡在这里,后来突然就好了,不得解****/I2CDRB = 0x83; // Load Control Byte
之前执行到while这里的时候I2CIFG会变成0,为什么??
yuan wang3:
回复 Carter Liu:
直接买的模块0 0,电源是单片机的3.3V
这个模块的某些部分似乎有点问题(通道4读数直接衰减了一半…),估计做的不是太好。。送了一个149的软件IIC例程调不通才自己写的硬件IIC
不过读数问题已经解决啦,O(∩_∩)O谢谢