我以前写的驱动。。希望对你有帮助。
#include "msp430x54xA.h"
void ADS1118_GPIO_Init(void)
{
P1OUT |= 0x02; // Set P1.1 for CS
P1DIR |= 0x02; // Set P1.1 to output direction
P3SEL |= 0x80; // P3.7 option select
P5SEL |= 0x30; // P5.4,5 option select
P5DIR |= 0x01; // Set P5.0 to output direction
}
void ADS1118_SPI_Init(void)
{
UCB1CTL1 |= UCSWRST; // **Put state machine in reset**
UCB1CTL0 |= UCMST+UCSYNC+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCB1CTL1 |= UCSSEL_2; // SMCLK
UCB1BR0 = 0x05; // /2
UCB1BR1 = 0; //
UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(100); // Wait for slave to initialize
}
void ADS1118_ADS_Config(signed int temp_config_value)
{
signed int Config_Value;
Config_Value = temp_config_value;
P1OUT &=~ 0x02; // Set CS low
__delay_cycles(100); // Wait for slave to initialize
ADS1118_WriteSPI(Config_Value,0); // Write configuration to ADS1118
__delay_cycles(100); // Wait for slave to initialize
P1OUT |= 0x02; // Set CS high
}
int ADS1118_ADS_Read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0;
P1OUT &=~ 0x02; // Set CS low
Data = ADS1118_WriteSPI(Config_Value,1); // Read data from ADS1118
P1OUT |= 0x02; // 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 ADS1118_WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb;
unsigned int temp;
signed int dummy;
temp = config;
if (mode==1) temp = 0;
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
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp >> 8 ); // Write MSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = UCB1RXBUF; // Read MSB of Config
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF= (temp & 0xff); // Write LSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = (dummy <<8) | UCB1RXBUF; // Read LSB of Config
__delay_cycles(100);
return msb;
}
void main(void)
{
volatile int ADC_Result;
float Voltage_ch1;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
ADS1118_GPIO_Init();
ADS1118_SPI_Init();
//ADS_Config();
while(1)
{
ADS1118_ADS_Config(0xC3E3); //Only Select AIN1,860SPS,+-4.096V scan voltage range;
ADC_Result = ADS1118_ADS_Read(); // Read data from ch1,the last time result
Voltage_ch2 = ADC_Result*1.0/32768*4.096;
__delay_cycles(10000);
}
}
我以前写的驱动。。希望对你有帮助。
#include "msp430x54xA.h"
void ADS1118_GPIO_Init(void)
{
P1OUT |= 0x02; // Set P1.1 for CS
P1DIR |= 0x02; // Set P1.1 to output direction
P3SEL |= 0x80; // P3.7 option select
P5SEL |= 0x30; // P5.4,5 option select
P5DIR |= 0x01; // Set P5.0 to output direction
}
void ADS1118_SPI_Init(void)
{
UCB1CTL1 |= UCSWRST; // **Put state machine in reset**
UCB1CTL0 |= UCMST+UCSYNC+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCB1CTL1 |= UCSSEL_2; // SMCLK
UCB1BR0 = 0x05; // /2
UCB1BR1 = 0; //
UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(100); // Wait for slave to initialize
}
void ADS1118_ADS_Config(signed int temp_config_value)
{
signed int Config_Value;
Config_Value = temp_config_value;
P1OUT &=~ 0x02; // Set CS low
__delay_cycles(100); // Wait for slave to initialize
ADS1118_WriteSPI(Config_Value,0); // Write configuration to ADS1118
__delay_cycles(100); // Wait for slave to initialize
P1OUT |= 0x02; // Set CS high
}
int ADS1118_ADS_Read(void)
{
unsigned int Data, Config_Value;
Config_Value = 0;
P1OUT &=~ 0x02; // Set CS low
Data = ADS1118_WriteSPI(Config_Value,1); // Read data from ADS1118
P1OUT |= 0x02; // 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 ADS1118_WriteSPI(unsigned int config, unsigned char mode)
{
signed int msb;
unsigned int temp;
signed int dummy;
temp = config;
if (mode==1) temp = 0;
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
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF = (temp >> 8 ); // Write MSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = UCB1RXBUF; // Read MSB of Config
while(!(UCB1IFG&UCTXIFG));
UCB1TXBUF= (temp & 0xff); // Write LSB of Config
while(!(UCB1IFG&UCRXIFG));
dummy = (dummy <<8) | UCB1RXBUF; // Read LSB of Config
__delay_cycles(100);
return msb;
}
void main(void)
{
volatile int ADC_Result;
float Voltage_ch1;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
ADS1118_GPIO_Init();
ADS1118_SPI_Init();
//ADS_Config();
while(1)
{
ADS1118_ADS_Config(0xC3E3); //Only Select AIN1,860SPS,+-4.096V scan voltage range;
ADC_Result = ADS1118_ADS_Read(); // Read data from ch1,the last time result
Voltage_ch2 = ADC_Result*1.0/32768*4.096;
__delay_cycles(10000);
}
}
举报