数据始终为0,大神帮忙解答一下。
。
头文件应该是没问题的
file:///D:QQPCMgr(1)DocumentsTencent Files951141617ImageC2C(SGB3@_N(T}KUH5`3C(PH}U.jpgfile:///D:QQPCMgr(1)DocumentsTencent Files951141617ImageC2C(SGB3@_N(T}KUH5`3C(PH}U.jpg/*
* ADS1118.c
* ADS1118芯片描述:
* 体积小、低功耗、16位模数转换器
* 内部含温度可测量电路、PGA(可编程增益放大器)
* 4个差分输入/单端输入
* SPI串行通讯传输
* 包含单次转换模式(单次转换完成进入低功耗模式)和连续转换模式
* 输入多路复用
*
* 硬件描述:
* P6.0---->CS
* P6.1---->CLK
* P6.2---->IN
* P6.3---->OUT
*
*
*
* 软件描述: 保持SCLK低于28ms复位SPI通信接口
*
*
*
*
* Time:
* 2017年7月22日
*
* Author:
* Touch
*
*/
#include<msp430f5529.h>
#define ADS1118_CS BIT0
#define ADS1118_CLK BIT1
#define ADS1118_IN BIT2
#define ADS1118_OUT BIT3
#define ADS1118_Port_OUT P6OUT
#define ADS1118_Port_DIR P6DIR
#define ADS1118_Port_IN P6IN
#define ADS1118_CS_OUT (ADS1118_Port_DIR|=ADS1118_CS)
#define SET_ADS1118_CS (ADS1118_Port_OUT|=ADS1118_CS)
#define CLR_ADS1118_CS (ADS1118_Port_OUT&=~ADS1118_CS)
#define ADS1118_CLK_OUT (ADS1118_Port_DIR|=ADS1118_CLK)
#define SET_ADS1118_CLK (ADS1118_Port_OUT|=ADS1118_CLK)
#define CLR_ADS1118_CLK (ADS1118_Port_OUT&=~ADS1118_CLK)
#define ADS1118_OUT_IN (ADS1118_Port_DIR&=~ADS1118_OUT)
#define ADS1118_OUT_Val (ADS1118_Port_IN&ADS1118_OUT)
#define ADS1118_IN_OUT (ADS1118_Port_DIR|=ADS1118_IN)
#define SET_ADS1118_IN (ADS1118_Port_OUT|=ADS1118_IN)
#define CLR_ADS1118_IN (ADS1118_Port_OUT&=~ADS1118_IN)
//复用功能宏定义
#define SS BITF // x Unused in Continuous conversion mode(Always reads back as 0)
#define MUX2 BITE // 1
#define MUX1 BITD // 1
#define MUX0 BITC // 1 111 = AINP is AIN3 and AINN is GND
#define PGA2 BITB // 0
#define PGA1 BITA // 0
#define PGA0 BIT9 // 1 001 = FS is ±4.096 V
#define MODE BIT8 // 0 0 = Continuous conversion mode
#define FS 4.096
#define DR2 BIT7 // 1
#define DR1 BIT6 // 0
#define DR0 BIT5 // 0 100 = 128 SPS (default)
#define TS_MODE BIT4 // 0 0 = ADC mode (default) (1 = Temperature sensor mode)
#define PULL_UP_EN BIT3 // 1 1 = Pull-up resistor enabled on DOUT/DRDY pin (default)
#define NOP1 BIT2 // 0
#define NOP0 BIT1 // 1 01 = Valid data, update the Config register (default)
#define NOT_USED BIT0 // x Always reads '1'
#define Control_Regist (MUX2+MUX1+MUX0+PGA0+DR2+PULL_UP_EN+NOP0)
#define Control_Regist_MSB (MUX2+MUX1+MUX0+PGA0)
#define Control_Regist_LSB (DR2+PULL_UP_EN+NOP0)
float ADS1118_Voltage;
float ADS1118_Temperature=25.9;
void ADS1118_init(void) //端口配置
{
ADS1118_CS_OUT;
ADS1118_CLK_OUT;
ADS1118_IN_OUT;
ADS1118_OUT_IN;
CLR_ADS1118_CS;
_NOP();
CLR_ADS1118_CLK;
_NOP();
CLR_ADS1118_IN;
_NOP();
}
//SPI为全双工通信方式,在AD转换函数中调用
unsigned char ADS1118_Read(unsigned char data)
{
unsigned char i,temp,Din;
temp=data;
for(i=0;i<8;i++)
{
Din = Din<<1;
if(0x80&temp) //数据最高位置位
SET_ADS1118_IN;
else
CLR_ADS1118_IN;
_delay_cycles(1);
SET_ADS1118_CLK;
_delay_cycles(1);
if(ADS1118_OUT_Val) //数据输入
Din |= 0x01;
_delay_cycles(1);
CLR_ADS1118_CLK;
_delay_cycles(1);
temp = (temp<<1);
}
return Din;
}
void ADS1118_Get_Voltage(void) //模数转换
{
unsigned int i=0;
unsigned char Data_REG_H,Data_REG_L;
unsigned int Data_REG;
while((ADS1118_OUT_Val)&&(i<1000)) i++;//数据输入,延时稳定
Data_REG_H=ADS1118_Read((unsigned char)((Control_Regist_MSB>>8)));
Data_REG_L=ADS1118_Read((unsigned char)Control_Regist_LSB);
Data_REG=(Data_REG_H<<8)+Data_REG_L;
ADS1118_Read((unsigned char)((Control_Regist_MSB>>8)));
ADS1118_Read((unsigned char)Control_Regist_LSB);
if(Data_REG>=0x8000)
{
Data_REG=0xFFFF-Data_REG;//把0xFFFF改成0x10000
ADS1118_Voltage=(-1.0)*((Data_REG*FS/0x8000));
}
else
ADS1118_Voltage=(1.0)*((Data_REG*FS/32768));
}
void ADS1118_Get_Temperature(void)
{
unsigned int i=200;
unsigned char Data_REG_H,Data_REG_L;
unsigned int Data_REG;
while((ADS1118_OUT_Val)&&(i<200)) i++;
Data_REG_H=ADS1118_Read(0x83);
Data_REG_L=ADS1118_Read(0xFB);
Data_REG=(Data_REG_H<<8)+Data_REG_L;
ADS1118_Read(0x83);
ADS1118_Read(0xFB);
ADS1118_Temperature=(Data_REG>>2)*0.03125;
}
此帖出自
小平头技术问答
,没呢,你做到什么程度了呢?
一周热门 更多>