/******************c file************************************************/
#include "msp430x26x.h"
#include "ADS1115.h"
uchar value_H, value_L, value_buf; //Set up two receive buffers
/********************************************
Function name: ADS1115_delayFunction
: delay function
Parameter: xmsReturn
value: None
********************************************/
void ADS1115_delay(uint xms)
{
uint i,j;
for(i = xms; i > 0; i--)
for(j = 250; j > 0; j--);
}
/*******************************************
Function name: ADS1115_delay_usFunction
: delay function
Parameter: xmsReturn
value: None
********************************************/
void ADS1115_delay_us(uint xus)
{
uint i;
i = xus;
while(i > 0)i--;
}
/*******************************************
Function name: ADS1115_InitFunction
: Initialize ADS1115
Parameters: None
Return value: None
********************************************/
void ADS1115_Init(void)
{
SCLK_DIR; //Init IO port
SDA_OUT_DIR; //setting as output
SCLK_H;
SDA_H; //high
}
/***********************************************
Function name: ADS1115_STARTFunction
: Start AD
Parameters: None
Return value: None
********************************************/
void ADS1115_START(void)
{
uint i = 0;
SCLK_DIR;
SDA_OUT_DIR;
SCLK_H;
i++;
SDA_H;
i++;
SDA_L;
i++;
SCLK_L; //CLOCK PIN LOW
}
/*******************************************
Function name: ADS1115_STOPFunction
: Stop starting ADParameter
: NoneReturn
value: None
********************************************/
void ADS1115_STOP(void)
{
uint i = 0;
SCLK_DIR;
SDA_OUT_DIR;
SDA_L;
i++;
SCLK_H;
i++;
SDA_H;
i++;
i++;
}
/***********************************************
Function name: ADS1115_ACKFunction
: ADS1115 response signal
Parameter: NoneReturn
value: None
********************************************/
void ADS1115_ACK(void)
{
uint i = 0;
SCLK_H;
i++;
SDA_IN_DIR; //BIT1 INPUT
while(((P1IN & BIT1) == BIT1)&&(i<255)) i++; //
i++;
SCLK_L;
}
/*******************************************
Function name: ADS1115_writebyteFunction
: ADS1115 writes a byte
Parameter: NoneReturn
value: None
********************************************/
void ADS1115_writebyte(uchar data)
{
uchar bit_data,j;
uint i = 0;
uchar temp;
SDA_OUT_DIR; //SDA_OUTPUT
temp = data;
for(j = 0; j < 8; j++) //write 1 byte and add one bit for waiting for the response signal from slave
{
bit_data = temp & 0x80;
if(bit_data == 0x80) SDA_H;
else SDA_L;
i++;
SCLK_H;
temp <<= 1; //move left 1 bit
SCLK_L; //MAICHONG
}
SCLK_H;
i++; //release the data wire
SCLK_L;
i++;
}
/*******************************************
Function name: ADS1115_readbyteFunction
: ADS1115 reads a word
Parameter: NoneReturn
value: None
********************************************/
void ADS1115_readbyte(void)
{
uint i = 0,j = 0;
value_buf = 0;
uchar BYTE = 0X80;
SDA_IN_DIR;
for(j = 0; j < 8; j++)
{
SCLK_H;
if(P1IN & 0X02) value_buf = value_buf | BYTE;//1000 00000
else value_buf = value_buf & (~BYTE); //0111 1111
BYTE = BYTE >> 1;
SCLK_L;
}
SCLK_H;
i++;
SDA_IN_DIR;
SDA_L;
i++;
SCLK_L;
i++;
SCLK_H;
i++;
}
/*******************************************
Function name: ADS1115_configFunction
: ADS1115 write configuration register
Parameter: NoneReturn
value: None
********************************************/
void ADS1115_config(uchar data)
{
uchar data1;
switch(data)
{
case 0 : data1 = 0xc0;break;
case 1 : data1 = 0xd0;break;
case 2 : data1 = 0xe0;break;
case 3 : data1 = 0xf0;break;
}
ADS1115_START();
ADS1115_writebyte(0x90); //IIC address
ADS1115_ACK();
ADS1115_writebyte(0x01); //point to config register
ADS1115_ACK();
ADS1115_writebyte(data1); //config register 6.144 CONTINUE CONVERSION MODE
ADS1115_ACK();
ADS1115_writebyte(0xE3); //config register DISABLE COMPARTOR
ADS1115_ACK();
ADS1115_STOP();
}
/*******************************************
Function name: ADS1115_pointer
Function: ADS1115 write pointer register
Parameter: No
return value :None
********************************************/
void ADS1115_pointer(void)
{
ADS1115_START();
ADS1115_writebyte(0x90); //IIC address write
ADS1115_ACK();
ADS1115_writebyte(0x00); //IIC conversion
ADS1115_ACK();
ADS1115_STOP();
}
/*******************************************
Function name: ADS1115_readFunction
: ADS1115 read register
Parameter: NoneReturn
value: None
********************************************/
void ADS1115_read(void)
{
ADS1115_START();
ADS1115_writebyte(0x91); //IIC address read
ADS1115_ACK();
ADS1115_readbyte();
value_H = value_buf;
SDA_OUT_DIR;
SDA_L;
ADS1115_readbyte();
value_L = value_buf;
SDA_OUT_DIR;
SDA_L;
ADS1115_STOP();
value = value_H;
value = value << 8; //move left 8 bits
value = value | value_L; //change completely
value = (uint)(value*0.0001876*1000+0.5);
}
/****************************h file****************************************/
#ifndef __ADS1115_H__
#define __ADS1115_H__
#define uchar unsigned char
#define uint unsigned int
#define SCLK_DIR P1DIR |= BIT0 //Set the clock as output
#define SDA_OUT_DIR P1DIR |= BIT1 //Set the data as output
#define SDA_IN_DIR P1DIR &= ~BIT1 //Set the data as input
#define ADS1115_OUT P1OUT |= 0X03
#define SCLK_HP1OUT |= BIT0
#define SCLK_LP1OUT &= ~BIT0
#define SDA_HP1OUT |= BIT1
#define SDA_LP1OUT &= ~BIT1
extern uint value;
void ADS1115_delay(uint xms);
void ADS1115_delay_us(uint xus);
void ADS1115_Init(void);
void ADS1115_START(void);
void ADS1115_STOP(void);
void ADS1115_ACK(void);
void ADS1115_writebyte(uchar data);
void ADS1115_readbyte(void);
void ADS1115_config(uchar data);
void ADS1115_pointer(void);
void ADS1115_read(void);
#endif
////Brief introduction to ADS1115
ADS1115 is a 16-bit AD, 4-channel I2C communication mode, you can choose different internal reference sources, you can measure positive and negative voltages, negative voltages are stored in the form of complement, there is a PGA inside for reference source programming, then a 16-bit AD conversion storage area, and then the internal I2C, the calculation formula for the digital quantity converted by I2C is: (data /32768)*FS.
|