CS_0; // Set CS low
WriteSPI(Config_Value,0); // Write configuration to ADS1118
CS_1; // Set CS high
}
int ADS_Read(void)
{
unsigned int Data, Config_Value;
CS_0; // Set CS low
Data = WriteSPI(Config_Value,1); // Read data from ADS1118
CS_1; // Set CS high
return Data;
}
/*
* Mode 0: Only write config register to ADS1118
* Mode 1: Write config register to ADS1118 as well as read data from ADS1118
*/
signed int WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb;
unsigned int temp;
volatile signed int dummy;
temp = config;
if (mode==1) temp = config | 0x8000;
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp >> 8 ); // Write MSB of Config
while(!(UCB1IFG&UCRXIFG));
msb = UCB1RXBUF; // Read MSB of Data
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp & 0xff); // Write LSB of Config
while(!(UCB1IFG&UCRXIFG));
msb = (msb << 8) | UCB1RXBUF; // Read LSB of Data