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