📄 ads1240.c
字号:
#include "ADS1240.h"
#include "hpa449lib.h"
int tosc = 40;
int ADS1240Init(void)
{
// initialize the pins to their default state
WriteExpandedGPIO(0, ~(ADS1240_POL | CLK_KICK_START_EN));
P3SEL = ~(ADS1240_DSYNC | ADS1240_PDWN | ADS1240_SCLK | ADS1240_CS |
ADS1240_DIN | ADS1240_DOUT | ADS1240_DRDY) & 0xff;
// define initial states
P3OUT &= ~(ADS1240_SCLK | ADS1240_DIN);
P3OUT |= (ADS1240_DSYNC | ADS1240_PDWN | ADS1240_CS);
// define outputs
P3DIR = (ADS1240_DSYNC | ADS1240_PDWN | ADS1240_SCLK | ADS1240_CS | ADS1240_DIN);
P2SEL &= ~(ADS1240_DRDY);
P2DIR &= ~(ADS1240_DRDY);
return ADS1240_NO_ERROR;
}
int ADS1240WaitForDataReady(int Timeout)
{
if (Timeout > 0)
{
// wait for /DRDY = 1
while (!(P2IN & ADS1240_DRDY) && (Timeout-- >= 0))
;
// wait for /DRDY = 0
while ( (P2IN & ADS1240_DRDY) && (Timeout-- >= 0))
;
if (Timeout < 0)
return ADS1240_TIMEOUT_WARNING;
}
else
{
// wait for /DRDY = 1
while (!(P2IN & ADS1240_DRDY))
;
// wait for /DRDY = 0
while ( (P2IN & ADS1240_DRDY))
;
}
return ADS1240_NO_ERROR;
}
void ADS1240AssertCS( int fAssert)
{
if (fAssert)
P3OUT &= ~ADS1240_CS;
else
P3OUT |= ADS1240_CS;
}
void ADS1240SendByte(int Byte)
{
int i;
for (i=0; i<8; i++)
{
if (Byte & 0x80)
P3OUT |= ADS1240_DIN;
else
P3OUT &= ~ADS1240_DIN;
P3OUT |= ADS1240_SCLK;
Pause(2*tosc);
P3OUT &= ~ADS1240_SCLK;
Pause(2*tosc);
Byte <<= 1;
}
}
unsigned char ADS1240ReceiveByte(void)
{
unsigned char Result = 0;
int i;
for (i=0; i<8; i++)
{
Result <<= 1;
P3OUT |= ADS1240_SCLK;
Pause(2*tosc);
if (P3IN & ADS1240_DOUT)
Result |= 1;
P3OUT &= ~ADS1240_SCLK;
Pause(2*tosc);
}
return Result;
}
/*
******************************************************************************
*/
long ADS1240ReadData(int fWaitForDataReady)
{
long Data;
// if requested, synchronize to /DRDY
if (fWaitForDataReady)
ADS1240WaitForDataReady(0);
// assert CS to start transfer
ADS1240AssertCS(1);
// send the command byte
ADS1240SendByte(ADS1240_CMD_RDATA);
// delay according to the data sheet
Pause(50 * tosc);
// get the conversion result
Data = ADS1240ReceiveByte();
Data = (Data << 8) | ADS1240ReceiveByte();
Data = (Data << 8) | ADS1240ReceiveByte();
// sign extend data
if (Data & 0x800000)
Data |= 0xff000000;
// de-assert CS
ADS1240AssertCS(0);
return Data;
}
int ADS1240ReadRegister(int StartAddress, int NumRegs, unsigned * pData)
{
int i;
// assert CS to start transfer
ADS1240AssertCS(1);
// send the command byte
ADS1240SendByte(ADS1240_CMD_RREG | (StartAddress & 0x0f));
// send the command argument
ADS1240SendByte(NumRegs-1);
// pause according to the data sheet
Pause(50 * tosc);
// get the register content
for (i=0; i< NumRegs; i++)
{
*pData++ = ADS1240ReceiveByte();
}
// de-assert CS
ADS1240AssertCS(0);
return ADS1240_NO_ERROR;
}
int ADS1240WriteRegister(int StartAddress, int NumRegs, unsigned * pData)
{
int i;
// assert CS to start transfer
ADS1240AssertCS(1);
// send the command byte
ADS1240SendByte(ADS1240_CMD_WREG | (StartAddress & 0x0f));
// send the command argument
ADS1240SendByte(NumRegs-1);
// pause according to the data sheet
Pause(50 * tosc);
// send the data bytes
for (i=0; i< NumRegs; i++)
{
ADS1240SendByte(*pData++);
}
// de-assert CS
ADS1240AssertCS(0);
return ADS1240_NO_ERROR;
}
int ADS1240SendResetCommand(void)
{
// assert CS to start transfer
ADS1240AssertCS(1);
// send the command byte
ADS1240SendByte(ADS1240_CMD_RESET);
// de-assert CS
ADS1240AssertCS(0);
// wait for t11 (16 tosc)
Pause(16 * tosc);
return ADS1240_NO_ERROR;
}
int ADS1240SetChannel(int MuxCode)
{
ADS1240WriteRegister(ADS1240_MUX_REGISTER, 0x01, &MuxCode);
return ADS1240_NO_ERROR;
}
int ADS1240SetGain(int GainCode)
{
unsigned Temp;
// the gain code is only part of the register, so we have to read it back
// and massage the new gain code into it
ADS1240ReadRegister(ADS1240_SETUP_REGISTER, 0x01, &Temp);
// clear prev gain code;
Temp &= ~0x07;
Temp |= GainCode & 0x07;
// write the register value containing the new gain code back to the ADS
ADS1240WriteRegister(ADS1240_SETUP_REGISTER, 0x01, &Temp);
return ADS1240_NO_ERROR;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -