adc input voltage code example

Example: 2.5V (512 in binary)

int main(void) {
    int val = 0;
    while(1) {
        val = RMS(30);    //accumulate x mesures. Recomended x=30;
        print("Digital: %d",val);
        print("Analog: %d",to_analog(val));
    }
    return 0;
}     

int RMS(int repeat){
    accumulated = 0;
    do repeat times {
        digital_value = read(analog_value);   //convert the analog-value (voltage) to digital one (0-1024)
        accumulated = accumulated + digital_value*digital_value;      //accumulate all the digital-values “repeat” times
    }
    average = accumulated / repeat;
    return sqrt(average);
}

/*
the row above should convert the digital manipulated value to a voltage/ analogic signal again acording to the following rule:
5v - 1023        1023 x voltage = average x 5
voltage - average    voltage(Vo) = averagex 5/1023
*/
}

Tags:

Misc Example