📄 ads7871a.c
字号:
/****************************************************************/
/* FILENAME: main.c */
/* DESCRIPTION: This program uses the MSP430F449 to read 500 */
/* samples from the ADS7871 14-bit 41KSPS ADC. */
/* The samples are stored in the buffer adc_data. */
/* Refer to the .map file created by the compiler for the */
/* address in memory for adc_data. The LCD displays */
/* decimal values based on converted input/digital code */
/* AUTHOR: Tom Hendrick, Data Acquisition Products, */
/* Dallas Texas */
/* CREATED 2003(C) BY TEXAS INSTRUMENTS INCORPORATED. */
/* VERSION: 2.1 */
/****************************************************************/
#include <msp430x44x.h>
#include "SBLCDA2.h"
#include "ADS787x.h"
#define ADS7870 (0)
#define ADS7871 (1)
#define HPA449 (1)
#define SITE1 (1)
#define EXT_CLK (1) //Open W7 if set to zero
#define HDW_RST (1)
#define DIRECTMODE (0)
/**************** Function prototypes ***********************************/
/* */
void init_sys(void); /* MSP430 Initialisation routine */
void init_adc(void); /* adc initialisation routine */
void delay(int); /* Software delay */
void adc_convert(void); /* Do the adc conversion */
void adc_read(int); /* Read the adc conversion */
void RESET_ADC(void);
void SET_R3(int);
void SET_R6(int);
void SET_R7(int);
void SET_R24(int);
void SET_R31(void);
void Read_REG(int);
/**************** HPA449 Function prototypes ****************************/
/* */
void timer(void);
void display(void);
void InitializeLCD( void );
void clearDisplay( void );
void clearMajor( void );
void sortMajor( unsigned int );
void displayMinor( int, int);
void displaySpecial( long int );
void displayMinor( int, int );
void displayHPA449(void);
void TX_complete(void); /* Transmit ready */
void RX_complete(void); /* Receive ready */
/* */
/************************************************************************/
/************************************************************************/
/* */
/* Global variable declarations */
/* */
/************************************************************************/
unsigned int adc_data[500]; // Storage for converted data
unsigned int hi_byte, lo_byte, adc_hi;
int i, j;
/************************************************************************/
/* */
/* main() variable declarations */
/* */
/************************************************************************/
#if (SITE1)
#define CONVERT 0x20 //p3.5
#define ADC_CS 0x80 //p3.7
#define CONV_CLK 0x0F //p1.2
#define ADC_RST 0x01 //p3.0
#define OSC_ENA 0x20 //p3.5
#else
#define CONVERT 0x20 //p3.5
#define ADC_CS 0x80 //p3.7
#define CONV_CLK 0x04 //p1.2
#define ADC_RST 0x01 //p3.0
#define OSC_ENA 0x20 //p3.5
#endif
void main(void)
{
// int test;
init_sys(); // Initialise the MSP430
init_adc(); // Initialise the adc
if (HPA449)
{ InitializeLCD();
clearDisplay();
}
do {
clearDisplay();
for(i=0; i<50; i++)
{
adc_convert(); // Do conversions from each channel
//delay(10);
adc_read(i); // Do conversions from each channel
sortMajor( adc_data[i] ); //Display CODES as Decimal
//displaySpecial(SoftBaugh);
if (ADS7871){
displayMinor( 5, UL_SEVEN );
displayMinor( 4, UL_EIGHT );
displayMinor( 3, UR_SEVEN );
displayMinor( 2, UR_ZERO );
}
if (ADS7870){
displayMinor( 5, UL_SEVEN );
displayMinor( 4, UL_EIGHT );
displayMinor( 3, UR_SEVEN );
displayMinor( 2, UR_ONE );
}
delay(50);
}
} while (1); //
}
/************************************************************/
/* Prototype - init_sys */
/* */
/* !NA init_sys */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV * */
/* Description */
/* This prototype initialises the MSP430F149 */
/************************************************************/
void init_sys(void)
{
void setupXT2(void); /* Local function prototype */
void setupports(void); /* Local function prototype */
void setupSPI(void); /* Local function prototype */
setupXT2();
setupports();
setupSPI();
}
/************************************************************/
/* Prototype - setupXT2 */
/* */
/* !NA XT2_osc */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV * */
/* Description */
/* This prototype sets-up the XT2 oscillator and tests */
/* that it has settled before moving on */
/************************************************************/
void setupXT2 (void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FLL_CTL0&=XT2OFF; // Switch on the XT2 osc.
FLL_CTL1|=SELM_XT2+SELS; // Select XT2 osc. for
// SMCLK & MCLK
// Test the osc. flag bit
do {
IFG1 &= ~OFIFG; // Clear the OFIFG bit
} while (OFIFG&IFG1); //
if EXT_CLK
{P3OUT &= ~OSC_ENA;
timer();
}
else P3OUT |= OSC_ENA;
}
/************************************************************/
/* Prototype - setupports */
/* */
/* !NA setupports */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV * */
/* Description */
/* This prototype sets-up the GPIO ports as appropriate */
/************************************************************/
void setupports (void)
{
//SetupPort1
P1DIR = CONV_CLK;
P1SEL = CONV_CLK;
//SetupPort3
P3SEL = BIT3 + BIT2 + BIT1; // Bits 3, 2 & 1 are assigned as SPI specific pins
P3DIR |= CONVERT + ADC_CS + ADC_RST + OSC_ENA; // Set the ADC_CS bit as an output
P3OUT = CONVERT + ADC_CS + ADC_RST; // De-assert ADC_CS for the adc - HIGH
//if EXT_CLK
// {P3OUT &= ~OSC_ENA;
// timer();
// }
//else P3OUT |= OSC_ENA;
}
/************************************************************/
/* Prototype - setupSPI */
/* */
/* !NA setupSPI */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV * */
/* Description */
/* This prototype sets-up the P3 for communication via SPI */
/************************************************************/
void setupSPI (void)
/************************************************************/
/* System definitions */
/************************************************************/
#define SPI0en 0x40
#define SPI1en 0x10
{
ME1 |= USPIE0; // Module Enable - SPI
//
U0CTL &= ~SWRST; // Make sure the RESET bit is off
U0CTL |= CHAR + SYNC + MM; // USART0 module operation
// CHAR = 1 => 8-bit data
// SYNC = 1 => SPI selected
// MM = 1 => master mode,
// MSP is the master
U0TCTL |= CKPH+SSEL0 + SSEL1 + STC; // USART0 Tranmit control register
// SSEL0 = 1 & SSEL1 = 1
// => SMCLK is used for baud-rate generation
// STC = 1 => 3-pin SPI mode selected
U0BR0 = 0x02; // Divide SMCLK by 4 => transfer clock
U0BR1 = 0x00; //
U0MCTL = 0x00; // Modulation control - not used. Ensure
// all these bits are reset
ME2 |= USPIE1; // Module Enable - SPI
//
U1CTL &= ~SWRST; // Make sure the RESET bit is off
U1CTL |= CHAR + SYNC + MM; // USART1 module operation
// CHAR = 1 => 8-bit data
// SYNC = 1 => SPI selected
// MM = 1 => master mode,
// MSP is the master
U1TCTL |= CKPH + SSEL0 + SSEL1 + STC; // USART1 Tranmit control register
// SSEL0 = 1 & SSEL1 = 1
// => SMCLK is used for baud-rate generation
// STC = 1 => 3-pin SPI mode selected
U1BR0 = 0x04; // Divide SMCLK by 4 => transfer clock
U1BR1 = 0x02; //
U1MCTL = 0x00; // Modulation control - not used. Ensure
// all these bits are reset
}
/************************************************************/
/* Prototype - init_adc */
/* */
/* !NA init_adc */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV * */
/* Description */
/* This prototype sets-up the TLV2553 ADC */
/************************************************************/
void init_adc (void)
{
RESET_ADC();
SET_R7(REFE+BUFE);
SET_R3(BIN+MODE0+DIV2);
SET_R6(IO3+IO2+IO1+IO0);
// SET_R24(TWO_Wire_A);
// Set ADC Configuration
P3OUT &= ~ADC_CS ; // Assert ADC_CS LOW
U0TXBUF = (REGISTER+WRITE+REG4); // Send clocks to the ADC, this shifts
// in the first 8 bits of data.
TX_complete(); // Wait until all 8 bits have been transmitted.
U0TXBUF = (GAIN01+DIFF0); // Send clocks to the ADC, this shifts
// in the first 8 bits of data.
TX_complete(); // Wait until all 8 bits have been transmitted.
P3OUT |= ADC_CS ; // Assert ADC_CS HIGH
}
/************************************************************/
/* Prototype - RX_complete */
/* */
/* !NA complete */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV i */
/* Description */
/* This prototype checks the URXIFG0-bit. If the receiver */
/* is still receiving data the program waits until all bits*/
/* are received and the URXIFG0 bit is asserted */
/************************************************************/
void RX_complete(void)
{
#if (SITE1)
do
{
IFG2 &=~ URXIFG1;
}
while (URXIFG1 & IFG1);
#else
do
{
IFG1 &=~ URXIFG0;
}
while (URXIFG0 & IFG1);
#endif
}
/************************************************************/
/* Prototype - TX_complete */
/* */
/* !NA complete */
/* !LA ANSI C */
/* !PI * */
/* !PO * */
/* !LV i */
/* Description */
/* This prototype checks the URXIFG0-bit. If the receiver */
/* is still receiving data the program waits until all bits*/
/* are received and the URXIFG0 bit is asserted */
/************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -