Other Parts Discussed in Thread: MSP430G2332, MSP430WARE
Hi all,
This is my first project with an MSP430, namely the MSP430G2332. I am trying to implement the ADC10 functionality, in "single-channel, single-conversion" mode, triggered by setting the ADC10SC bit in ADC10CTL0. My understanding from the User Guide is that, as long as everything is configured correctly all I need to do is set the ADC10SC bit to trigger an ADC sample and convert, with the result being made available in ADC10MEM.
Unfortunately I must be doing something wrong, as my code below does not trigger an ADC sample and convert. I can, however, get it to work properly if I toggle ENC manually, but from my understanding of the User Guide this should not be necessary. Can anyone see what it is that I'm missing?
Here is my code, many thanks in advance for any suggestions!:
(main.cpp)
#include <msp430.h>
void initialise(void) {
/* ADC Control register 0 settings:
* ADC10ON - enables ADC
* REFON - enables use of internal voltage reference
* SREF_1 - uses internal voltage reference as ADC reference
* REF2_5V - sets internal voltage reference to 2.5V
* ADC10SR - "Sample Rate" bit reduces current consumption (sampling rate must be < 50ksps)
* ADC10SHT_3 - "Sample and Hold Time", sets sampling period to 64 clock cycles
* */
ADC10CTL0 = ADC10ON + REFON + SREF_1 + REF2_5V + ADC10SR + ADC10SHT_3;
/* ADC Control register 1 settigns:
* SHS_0 - "Sample and Hold Source", ADC sampling is triggered by setting ADC10SC (not triggered by timers)
* ADC10DIV_7 - Clock divisor set to 7
* ADC10SSEL_2 - ADC clock set to MCLK
* */
ADC10CTL1 = SHS_0 + ADC10DIV_7 + ADC10SSEL_2;
/* "Analog Enable" - Disables port pin buffer on pins being used for analog
* sensing (eliminates risk of possible parasitic current increasing power
* consumptionfrom analog signals on these pins that are near the digital
* threshold level) */
ADC10AE0 = BIT2;
}
// Read ADC on channel A2
unsigned int readADC(void) {
// Select ADC channel 2 to sample and convert.
// Channel to convert is defined by INCHx, in the upper 4 bits in ADCCTRL1
ADC10CTL1 |= INCH_2;
// Issue instruction to sample and convert, by setting ADC10SC bits in ADCCTRL0
ADC10CTL0 |= ADC10SC;
// Wait whilst sample is being taken and converted, and stored in ADC10MEM
// by checking the ADC10BUSY bit in ADCCTRL1
while( (ADC10CTL1 & ADC10BUSY) );
// Save ADC reading in results array
unsigned int val = ADC10MEM;
return val;
}
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
initialise();
for(;;) {
unsigned int ADC_reading = readADC();
// Now do something with ADC_reading
}
return 0;
}