/************************************************************************
* @device MSP430F169
* @auther Qiyan Ke
* @brief drivers for ADS1120
* @date 2014.7.7
**********************************************************************
ACLK=32.768KHz
MCLK=8MHz
SMCLK=4MHz
***********************************************************************/
#include <msp430x16x.h>
#include "ADS1120.h"
#define RESET 0X06
#define START 0X08
#define POWERDOWN 0X02
#define RDATA 0X10
#define RREG 0X20
#define WREG 0X40
#define PXOUT P1OUT
#define PXIN P1IN
#define PXDIR P1DIR
#define SCLK_L PXOUT &= ~BIT0
#define SCLK_H PXOUT |= BIT0
#define CS_L PXOUT &= ~BIT1
#define CS_H PXOUT |= BIT1
#define DRDY_IN PXIN&BIT2
#define DOUT PXIN&BIT3
#define DIN_L PXOUT &= ~BIT4
#define DIN_H PXOUT |= BIT4
#define uchar unsigned char
#define uint unsigned int
unsigned char Init_Config[4],channel0[8],channel1[8],channel2[8],channel3[8];
unsigned char i;
//extern unsigned int Result;
//unsigned int Result;
/***************************************************************************
* Function Name: ADS1120_Init()
* Purpose: ADS120 initialize
* Params :
*
*************************************************************************/
void ADS1120_Init(void)
{
PXDIR |= BIT0 + BIT1 + BIT4;
PXDIR &= ~(BIT2 + BIT3);
CS_H;
SCLK_L;
ADReset();
}
/***************************************************************************
* Function Name: WriteOneByte(unsigned char command)
* Purpose: write one byte to ADS1120
* Params :
*
*************************************************************************/
void WriteOneByte(unsigned char command)
{
unsigned char i;
for(i = 0; i < 8;i++)
{
if(command&0x80)
DIN_H;
else
DIN_L;
command <<= 1;
SCLK_H;
SCLK_L;
}
}
/***************************************************************************
* Function Name: ReadOneByte()
* Purpose: read one byte from ADS1120
* Params : result
*
*************************************************************************/
unsigned char ReadOneByte(void)
{
unsigned char result,i;
SCLK_L;
for(i = 0;i<8; i++)
{
SCLK_H;
result <<= 0x01;
if(DOUT)
result |= 0X01;
SCLK_L;
}
return result;
}
/***************************************************************************
* Function Name: ReadData()
* Purpose: read data from ADS1120
* Params : result 0~65535
*
*************************************************************************/
unsigned int ReadData(void)
{
unsigned int result;
CS_L;
WriteOneByte(RDATA);
result = ReadOneByte();
result = (result<<8) | ReadOneByte();
CS_H;
return result;
}
/***************************************************************************
* Function Name: ADReset()
* Purpose: reset ADS1120
* Params :
*
*************************************************************************/
void ADReset(void)
{
CS_L;
WriteOneByte(RESET);
CS_H;
}
/***************************************************************************
* Function Name: ADPowerDown()
* Purpose:
* Params :
*
*************************************************************************/
void ADPowerDown(void)
{
CS_L;
WriteOneByte(POWERDOWN);
CS_H;
}
/***************************************************************************
* Function Name: ADStartConversion()
* Purpose: start conversion
* Params :
*
*************************************************************************/
void ADStartConversion(void)
{
CS_L;
WriteOneByte(START);
CS_H;
}
/***************************************************************************
* Function Name: ReadRegister()
* Purpose: read data from register
* Params : data
*
*************************************************************************/
void ReadRegister(void)
{
unsigned char i;
unsigned long Data;
CS_L;
WriteOneByte(RREG|0x03);
// get the register content
for (i=0; i< 4; i++)
{
Data += ReadOneByte();
}
CS_H;
return;
}
/***************************************************************************
* Function Name: WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)
* Purpose: write data to register
* steps: 1.write address 2.write data
* Params NumRegs-----the numbers of register
*************************************************************************/
void WriteRegister(unsigned char StartAddress, unsigned char NumRegs, unsigned char * pData)
{
unsigned char i;
CS_L;
// send the command byte
WriteOneByte(WREG | (((StartAddress<<2) & 0x0c) |((NumRegs-1)&0x03)));
// send the data bytes
for (i=0; i< NumRegs; i++)
{
WriteOneByte(*pData);
pData++;
}
CS_H;
return;
}
/***************************************************************************
* Function Name: CofigAD(unsigned char channel)
* Purpose: set the sampling channel
* Params :
*
*************************************************************************/
void CofigAD(unsigned char channel)
{
switch(channel)
{
case 0: Init_Config[0] = 0x81;
break;
case 1: Init_Config[0] = 0x91;
break;
case 2: Init_Config[0] = 0xA1;
break;
case 3: Init_Config[0] = 0xB1;
break;
}
Init_Config[1] = 0X00;//Init_Config[1] = 0XC0;
Init_Config[2] = 0X50;//Init_Config[2] = 0X40;
Init_Config[3] = 0X00;
WriteRegister(0x00,0x04,Init_Config);
ReadRegister();
}
/***************************************************************************
* Function Name: SetChannelPGA(uchar channel, uchar gain)
* Purpose: set the sampling channel and the gain
* Params :
*
*************************************************************************/
void SetChannelPGA(uchar channel, uchar gain)
{
switch(channel)
{
case 0:
if(gain==1)
channel0[0] = 0x81;
if(gain==2)
channel0[1] = 0x83;
if(gain==4)
channel0[2] = 0x85;
if(gain==8)
channel0[3] = 0x87;
if(gain==16)
channel0[4] = 0x89;
if(gain==32)
channel0[5] = 0x8B;
if(gain==64)
channel0[6] = 0x8D;
if(gain==128)
channel0[7] = 0x8F;
break;
case 1:
if(gain==1)
channel1[0] = 0x91;
if(gain==2)
channel1[1] = 0x93;
if(gain==4)
channel1[2] = 0x95;
if(gain==8)
channel1[3] = 0x97;
if(gain==16)
channel1[4] = 0x99;
if(gain==32)
channel1[5] = 0x9B;
if(gain==64)
channel1[6] = 0x9D;
if(gain==128)
channel0[7] = 0x9F;
break;
case 2: Init_Config[0] = 0xA1;
break;
case 3: Init_Config[0] = 0xB1;
break;
}
}
/***************************************************************************
* Function Name: GetAD(unsigned char channel)
* Purpose: get the result
* Params : Result 0~65535
*
*************************************************************************/
unsigned int GetAD(unsigned char channel)
{
unsigned int Result;
CofigAD(channel);
ADStartConversion();
while(DRDY_IN);
Result = ReadData();
ADPowerDown();
return Result;
}
/***************************************************************************
* Function Name: GetAD(unsigned ch
评论0