ADS1118的MSP430驱动代码,非常方便移植

/*注意事项,测试使用的是ADS1118的模拟输入三口,AIN3,差分的时候,AIN3作为P,N是设置为GND,get_Voltgage函数是在这个条件下,可以测试-4.096~+4.096V的电压,实测有零点漂移需要自行校准,代码关键部位有注释进行相关解释,移植的时候只需要在头文件里面修改P口就行,其他的硬件配置要对,比如测试AIN3以外的口就没输出了,需要自己更改函数配置部分,spi双工通信模块可以不动,测试温度的不需要输入,可以直接用*/
/*********代码部分***************/
 
  
#ifndef __ADS1118_H
#define __ADS1118_H
#include "GlobalDefine.h"

#define      ADS1118_CS         BIT0
#define      ADS1118_CLK        BIT1
#define      ADS1118_OUT        BIT3
#define      ADS1118_IN         BIT2


#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      (PGA0+DR2+PULL_UP_EN+NOP0)
//#define      Control_Regist_MSB  (PGA0)
#define      Control_Regist_LSB  (DR2+PULL_UP_EN+NOP0)
extern float ADS1118_Voltage;
extern float ADS1118_Temperature;

/*
#define      Control_Regist       0x068B
#define      Control_Regist_MSB   0x06
#define      Control_Regist_LSB   0x8B
extern float Temperature;
*/


extern void ADS1118_init(void);
extern unsigned char ADS1118_Read(unsigned char);
extern void ADS1118_Get_Voltage(void);
extern void ADS1118_Get_Temperature(void);

#endif

/**********************************************************************************************/


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();
}

/*
unsigned char Read_ADS1118_Data(unsigned char Write_Data)   //SPI为全双工通信方式
{
 unsigned char i;
 unsigned char temp;
 unsigned char Read_Data=0x00;
 unsigned char A=0x80,B=0x01;
 temp=Write_Data;
 Read_Data=0x00;
 for(i=0;i<8;i++)
   {
    if(temp&A)           SET_ADS1118_IN;
    else                 CLR_ADS1118_IN;
    _NOP();
    SET_ADS1118_CLK;//The rising edge of CLK send Data,DIN Send the MSB of the Data firstly
    _NOP();
    if(ADS1118_OUT_Val)  Read_Data|=B;
    CLR_ADS1118_CLK;//The falling edge of CLK send Data,DOUT Send the LSB of the Data firstly
    A=(A>>1);
    B=(B<<1);
   }
 A=0x80;B=0x01;
 return Read_Data;
}
*/


unsigned char ADS1118_Read(unsigned char data)   //SPI为全双工通信方式
{
 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_us(1);
    SET_ADS1118_CLK;
    delay_us(1);
   if(ADS1118_OUT_Val)
    Din |= 0x01;
    delay_us(1);
    CLR_ADS1118_CLK;
    delay_us(1);
    temp = (temp<<1);
  }
  return Din;
}



float ADS1118_Voltage;
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));
}


float ADS1118_Temperature=25.9;
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;
}

/*最后附上ADS1118自己写的IO430.h风格的寄存器定义表头文件,非常的好用*/

你可能感兴趣的:(单片机,AD芯片)