/*
Based on the dual-channel AD acquisition program of msp430f169/149, internal 2.5V reference voltage, interrupt processing mode, sliding average filtering
mode, the stable voltage acquisition is concentrated in the two decimal places and remains unchanged during the test.
*/
#include "ADC.h"
#include "stdio.h"
#include
#define ADC_BUF_LEN 200
uint16_t ADC_Buf0[ADC_BUF_LEN] = {0}; //ADC data buffer
uint16_t ADC_Buf1[ADC_BUF_LEN] = {0};
uint32_t ADC_Buf0_Sum = 0 ;
uint32_t ADC_Buf1_Sum = 0 ;
void ADC_Init(void)
{
P6SEL|=(BIT0 + BIT1); //Select ADC channel
ADC12CTL0 &= ~ENC ; //Some important bits of the ADC12CTL0 register need bit1 == 0 to modify
ADC12CTL0 |= ADC12ON + SHT0_15 + REF2_5V + REFON + ADC12OVIE + ADC12TOVIE ; //ADC power control on, 16 CLK, internal reference 2.5V
//ADC12CTL1|= ADC12SSEL1 + ADC12SSEL1 + ADC12DIV_1; //SMCLK as clock source
ADC12CTL1 = SHP + CONSEQ_3 ;
ADC12MCTL0 = INCH_0 + SREF_1; //p60, internal reference voltage 2.5
ADC12MCTL1 = INCH_1 + SREF_1 + EOS ; //p61, internal reference voltage 2.5V
//ADC12MCTL2 = INCH_10 + SREF_1 + EOS; //Internal temperature sensor
ADC12IE |= BIT0 + BIT1 ; //Interrupt enable
}
/*Start ADC conversion*/
void ADC_Start(void)
{
ADC12CTL0 |= ADC12SC + ENC;
}
/*ADC interrupt service function*/
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
/*Use sliding average filter*/
uint16_t res ;
float vol ;
static uint8_t index = 0 ;
uint8_t i ;
ADC12CTL0 &= ~ENC ;
ADC12CTL0 &= ~ADC12SC ;
ADC12CTL0 &= ~(BIT0+BIT1) ;
ADC12IE &= ~(BIT0|BIT1) ; //Disable interrupt
//while((ADC12CTL1&0x01)==1); //If ADC is busy, wait, otherwise read ADC conversion valuei
= index % ADC_BUF_LEN ;
res = ADC12MEM0 ; //Transfer channel 0 AD dataADC_Buf0_Sum
-= ADC_Buf0[i] ; //The last array space is for the array
sumADC_Buf0[i] = res & 0x0FFF ;
ADC_Buf0_Sum += ADC_Buf0[i] ; //Add new data to the sumres
= ADC_Buf0_Sum / ADC_BUF_LEN ;
vol = 1.0 * res * 2.5 / 4095 ;
//vol = vol * 1.2184 ; //Development board parametersvol
= vol * 0.9954324 ; //My experimental board
//printf("%u\t%f\t\t",res,vot) ;
res = ADC12MEM1 ; //Transfer channel 1 AD data
ADC_Buf1_Sum -= ADC_Buf1[i] ; //The last array space is used to store the sum of the array
ADC_Buf1[i] = res & 0x0FFF ;
ADC_Buf1_Sum += ADC_Buf1[i] ; //Add the new data to the sum
res = ADC_Buf1_Sum / ADC_BUF_LEN ;
vol = 1.0 * res * 2.5 / 4095 ;
//vol = vol * 1.2184 ; //Parameters of the development board
vol = vol * 0.9954324 ; //My experimental board
index++ ;
ADC12IE |= BIT0 + BIT1 ; //Interrupt enable
ADC12CTL0 |= (BIT0+BIT1) ;
ADC_Start() ;
}
|