This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ADC on the CC430F6137

Other Parts Discussed in Thread: CC430F6137

Hi,

I'm trying to use the ADC port on the CC430F6137 when I interface it with an IR Sensor and try to read the analog value on P2.0 (A0 channel). However, the values I keep getting are not very accurate at all. I'm not sure if this is caused by noise or not using the ADC port correctly. Below is the code I'm using...any suggestions??

#include <cc430x613x.h>
#include <stdio.h>
#define MHZ_915

// Function declaration
void initialize_Adc(void); // Intiate ADC Port
void read_position(void); // This function for IR sensor one
void value_array(void); // This function average the values in the array
void sorting_array(void); // This function sorts the values in the array

// Global Declaration
float position_digital = 0;
float position_analog = 0;
float valuearray[30];
float final_position = 0;
float final_sorting = 0;

void main(void){

WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer

P2SEL |= BIT0;

initialize_Adc();

for(;;){

read_position();
value_array();
sorting_array();
}// Infinite for loop ended

} // Main ends

/******************************************************************************************************/
void initialize_Adc (void) {


// 12 Bit ADC intiatlization
ADC12CTL0 = ADC12ON+ADC12SHT02; // Turn on ADC12, set sampling time
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12MCTL0 = ADC12INCH_0; // Input from channel A0

__delay_cycles(75); // 75 us delay @ ~1MHz

ADC12CTL0 |= ADC12ENC; // Enable conversions

}// End ADC function

/********************************************************************************************************/
void read_position(void){

int i = 0;

for(i=0; i < 30; i++){
ADC12CTL0 |= ADC12SC; // Start Conversion
position_digital = ADC12MEM0;
position_analog = (5.0 * position_digital) / (4096); // First IR sensor // 5020
valuearray[i] = position_analog;

}// End of for loop

}// This function Reads the value from ADC
/*******************************************************************************************************/

void value_array(void){

float temp = 0;
int j = 0;

for (j = 0; j < 30; j++){
temp += valuearray[j];
} // End of for loop

final_position = temp / 50;

}// End of the value_array function
/*******************************************************************************************************/

void sorting_array(void){

int i = 0;
int j = 0;
float temp = 0;


while(j < 28){

j = j + 1;
i = 0;

for( i = 0; i < 29; i++) {

if (valuearray[i] > valuearray[i + 1]) {
temp = valuearray[i];
valuearray[i] = valuearray[i + 1];
valuearray[i + 1] = temp;
} // End of if loop
} // End of for loop

} // End of While loop

final_sorting = valuearray[20];

}// End of Sorting array

  • Hi,

    after a quick look in your code, i found the following:

    Ahmad Alkhatib said:

    /********************************************************************************************************/

    void read_position(void){

    int i = 0;

    for(i=0; i < 30; i++){
    ADC12CTL0 |= ADC12SC; // Start Conversion
    position_digital = ADC12MEM0;
    position_analog = (5.0 * position_digital) / (4096); // First IR sensor // 5020
    valuearray[i] = position_analog;

    }// End of for loop

    }// This function Reads the value from ADC

    You need to wait until the ADC conversion is completed. This is signed by ADC12IFGx bits in ADC12IFG register.

    Try to do this:

        ADC12CTL0 |= ADC12SC; // Start Conversion

       while(!(ADC12IFG & ADC12IFG0)); 

       position_digital = ADC12MEM0;

    However of course this is not the recommended way since it wastes power by polling the flag. The best was is to use interrupt and go to sleep mode to wait for the interrupt until the conversion completes. Even better you use DMA if you want to sample a couple of times.

    Please refer to the example codes here: http://www.ti.com/litv/zip/slac279b

  • I tried that but I'm still getting very inaccurate and unstable readings.

  • can you help me how to configure the analog sensor in input channel A1. 

    initialize ADC channel A1 ? 

**Attention** This is a public forum