Part Number:MSP430F5529
Hi
I am new to the MSP430 family, and any help will be appreciated.
I am trying to read from the onboard ADC. However I have 2 problems:
1. For some channel values (in the switch statement), no interrupt is called, so the processor gets stuck in the while loop waiting for the interrupt. (I know I need to add code to timeout of the loop)
2. For channel values where the interrupt is called, when I get the results it is always a small value <15.
Here is some code (heavily based on the example code in the DriverLib Users Guide:
In the main init function called at the top of main()
// Initialize ADC12 with ADC12’s built-in oscillator ADC12_A_init(ADC12_A_BASE, ADC12_A_SAMPLEHOLDSOURCE_SC, ADC12_A_CLOCKSOURCE_ADC12OSC, //ADC12_A_CLOCKDIVIDEBY_1); ADC12_A_CLOCKDIVIDER_1);
My Function for reading from the ADC
uint16_t read_analog(int channel) { //Switch ON ADC12 ADC12_A_enable(ADC12_A_BASE); // Setup sampling timer to sample-and-hold for 16 clock cycles ADC12_A_setupSamplingTimer(ADC12_A_BASE, ADC12_A_CYCLEHOLD_64_CYCLES, ADC12_A_CYCLEHOLD_4_CYCLES, 0); // Configure the Input to the Memory Buffer with the specified Reference Voltages ADC12_A_configureMemoryParam param = {0}; param.memoryBufferControlIndex = ADC12_A_MEMORY_0 + channel; param.inputSourceSelect = ADC12_A_INPUT_A0 + channel; param.positiveRefVoltageSourceSelect = ADC12_A_VREFPOS_AVCC; param.negativeRefVoltageSourceSelect = ADC12_A_VREFNEG_AVSS; param.endOfSequence = ADC12_A_NOTENDOFSEQUENCE; ADC12_A_configureMemory(ADC12_A_BASE, ¶m); // Start a single conversion, no repeating or sequences. ADC12_A_startConversion(ADC12_A_BASE, ADC12_A_MEMORY_0 + channel, ADC12_A_SINGLECHANNEL); // Wait for the Interrupt Flag to assert // Get appropriate interrupt id, also if the argument is a channel we do not use, return -1 switch (channel) { case 5 : while( !(ADC12_A_getInterruptStatus(ADC12_A_BASE,ADC12IFG5)) ); break; case 6 : while( !(ADC12_A_getInterruptStatus(ADC12_A_BASE,ADC12IFG6)) ); break; case 7 : while( !(ADC12_A_getInterruptStatus(ADC12_A_BASE,ADC12IFG7)) ); break; case 12 : while( !(ADC12_A_getInterruptStatus(ADC12_A_BASE,ADC12IFG12)) ); break; case 13 : while( !(ADC12_A_getInterruptStatus(ADC12_A_BASE,ADC12IFG13)) ); break; default: return(-1); } // Get the value uint16_t data = ADC12_A_getResults(ADC12_A_BASE, ADC12_A_MEMORY_0 + channel); // Disable the ADC ADC12_A_disable(ADC12_A_BASE); return(data); }