#include "msp430x54xA.h"
#define CS_0 P1OUT &=~ 0x02;
#define CS_1 P1OUT |= 0x02;
void GPIO_Init(void);
void SPI_Init(void);
void ADS_Config(void);
int ADS_Read(void);
signed int WriteSPI(unsigned int Config, unsigned char mode);
//V=ADC_Result /65536*VREF
void main(void)
{
volatile int ADC_Result,T;
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
GPIO_Init();
SPI_Init();
ADS_Config();
while(1)
{
ADC_Result = 2*ADS_Read(); // Read data from ADS1118
T= ADC_Result;
}
}
void 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 |= BIT0; // P5.0 Output
P5OUT |= BIT0; // P5.0 Output High
}
void SPI_Init(void)
{
UCB1CTL1 |= UCSWRST; // **Put state machine in reset**
UCB1CTL0 |= UCMST+UCSYNC+UCMSB; //+UCCKPH 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCB1CTL1 |= UCSSEL_2; // SMCLK
UCB1BR0 = 200; // /2
UCB1BR1 = 1; //
UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
__delay_cycles(100); // Wait for slave to initialize
}
/************************************************
0x058B A1/A2 +/-2.048 0x038B A1/A2 +/-4.096
0x458B A1/GND +/-2.048 0x438B A1/GND +/-4.096
0x558B A2/GND +/-2.048 0x538B A2/GND +/-4.096
0x658B A3/GND +/-2.048 0x638B A3/GND +/-4.096
0x758B A4/GND +/-2.048 0x738B A4/GND +/-4.096
**********************************************/
void ADS_Config(void)
{
signed int Config_Value;
Config_Value = 0x558B; // Initial Config Register
// ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT
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;
Config_Value = 0x558B;
// ADS1118 configuration AIN0/AIN1, FS=+/-2.048, DR=128sps, PULLUP on DOUT
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
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;
}
此帖出自
小平头技术问答
一周热门 更多>