Quantcast
Channel: Forums - Recent Threads
Viewing all articles
Browse latest Browse all 262198

ADC help

$
0
0

I have written the following code. Can someone point out how to use the ADC to automatically load all the samples into the array "reg" and why isn't the adc interrupt firing?

#include <msp430.h>

const int period = 200;
const int dutyCycleScaler = 4;

unsigned int reg[10];
unsigned int avg=0;
unsigned char state = 0; //used as boolean (false = 0, true = 1)
unsigned char change = 0; //used as boolean
unsigned char i = 0;
unsigned char adcFlag = 0;

int getAverage(){ //calculates average of current adc values
int runningAverage = 0;
for(i = 0; i < 10; i++){
runningAverage = (runningAverage + reg[i])/2;
}
return runningAverage;
}

int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_8MHZ;
DCOCTL = CALDCO_8MHZ;


//port settings
P1DIR |= BIT0;

//ADC setup
ADC10CTL0 |= ADC10SHT_2 + ADC10IE + ADC10ON;
ADC10CTL1 |= INCH_2 + ADC10DIV_7 + CONSEQ_2 + SHS_2;
ADC10DTC1 = 10;

//PWM timer settings
//TA0CCR0 = period; //period (clock ticks)
//TA0CCR1 = period/dutyCycleScaler; //25% PWM (duty cycle)
//TA0CCTL1 = OUTMOD_7; //up-down mode
//TA0CTL = TASSEL_2 + MC_1; //select master clock

//ADC Timer Settings
CCTL0 = OUTMOD_4; // CCR0toggle mode
CCR0 = 3000;
TACTL = TASSEL_2 + MC_3; // SMCLK, up-downmode


while(1){
//Run ADC
P1OUT |= 0x01;
ADC10CTL0 &= ~ENC;
while (ADC10CTL1 & BUSY); // Wait if ADC10 core is active
ADC10SA = 0x200; // Data buffer start
ADC10CTL0 |= ENC; // Sampling and conversion ready
reg[i] = ADC10MEM;
P1OUT &= ~0x01;

__bis_SR_register(GIE); //enable interrupts


if(adcFlag == 1){

avg=getAverage();

}
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
adcFlag = 1; //set adc
__bic_SR_register_on_exit(LPM0_bits); // Clear LPM3 bits from 0(SR)
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void){

__bic_SR_register_on_exit(LPM0_bits);
}


Viewing all articles
Browse latest Browse all 262198

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>