Hi everyone,
I am trying to do a single-conversion adc with several arbitrary inputs, i.e config pin 1, convert, stop then config pin 2, convert, stop etc, take average of 50 samples, store in memory and display via UART. I want to do several single conversion, one after another instead of a sequence. I have luck with single input, but not with several inputs. Can anyone point out what's wrong and is this the correct approach? I just started learning C and hardware programming, so bear with me. Below is my code:
#include<msp430g2553.h>
#include"UART.h"
volatileint voltRaw[5];
volatilefloat voltage = 0;
unsignedint avg_voltage = 0;
unsignedint average[50];
unsignedint i = 0;
int ii = 0;
int a = 0;
voidConfig_ADC(void);
voidStart_ADC1(void);
voidStart_ADC2(void);
voidStart_ADC3(void);
voidStart_ADC4(void);
voidStart_ADC5(void);
voidDisplay_ADC(int a);
voidmain(void) {
Config_ADC();
Start_ADC1();
_enable_interrupt();
Display_ADC(a);
Start_ADC2();
_enable_interrupt();
Display_ADC(a);
Start_ADC3();
_enable_interrupt();
Display_ADC(voltRaw[2]);
Start_ADC4();
_enable_interrupt();
Display_ADC(voltRaw[3]);
Start_ADC5();
_enable_interrupt();
Display_ADC(voltRaw[4]);
}
voidConfig_ADC()
{
ADC10CTL0 = SREF_1 + REF2_5V + REFON + ADC10ON + ADC10IE + ADC10SHT_3 + MSC;
ADC10CTL1 = ADC10DIV_5 + ADC10SSEL_3 + CONSEQ_2;
}
voidStart_ADC1() // Connected to an external multiplexer
{
P2DIR = BIT7;
P2OUT = 0;
P2DIR = BIT6;
P2OUT = 1;
_delay_cycles(100);
ADC10CTL1 |= INCH_0;
ADC10CTL0 |= ENC + ADC10SC;
while (ADC10CTL1 & ADC10BUSY);
a = 6;
voltRaw[0] = avg_voltage;
}
voidStart_ADC2() // Connected to the same external multiplexer
{
P2DIR = BIT7;
P2OUT = 1;
P2DIR = BIT6;
P2OUT = 1;
_delay_cycles(100);
ADC10CTL1 |= INCH_0;
ADC10CTL0 |= ENC + ADC10SC;
while (ADC10CTL1 & ADC10BUSY);
a = 5;
voltRaw[1] = avg_voltage;
}
voidStart_ADC3()
{
ADC10CTL1 &= 0x0FFF;
ADC10CTL1 |= INCH_3;
ADC10CTL0 |= ENC + ADC10SC;
while(ADC10CTL1 & ADC10BUSY);
a = 4;
voltRaw[2] = avg_voltage;
}
voidStart_ADC4()
{
ADC10CTL1 &= 0x0FFF;
ADC10CTL1 |= INCH_4;
ADC10AE0 = BIT4;
ADC10CTL0 |= ENC + ADC10SC;
while(ADC10CTL1 & ADC10BUSY);
a = 3;
voltRaw[3] = avg_voltage;
}
voidStart_ADC5()
{
ADC10CTL1 &= 0x0FFF;
ADC10CTL1 |= INCH_5;
ADC10CTL0 |= ENC + ADC10SC;
while(ADC10CTL1 & ADC10BUSY);
a = 0;
voltRaw[4] = avg_voltage;
}
voidDisplay_ADC(int a)
{
uart_puts_numbers(a);
put('-');
uart_puts_numbers(avg_voltage);
put(' ');
voltage = (avg_voltage * 2.5)/1024;
_delay_cycles(8000000);
}
#pragma vector = ADC10_VECTOR
__interruptvoidADC10_ISR(void)
{
// voltRaw = ADC10MEM;
average[i] = ADC10MEM;
if (i < 50)
{
i++;
}
else
{
avg_voltage = 0;
for(ii = 0; ii < 50; ii++)
{
avg_voltage = avg_voltage + average[ii];
}
avg_voltage = avg_voltage/50;
i = 0;
ADC10CTL0 &= ~ENC;
}
}