diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-10-11 22:54:42 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-10-11 22:54:42 +0200 |
commit | de2427619c329cdee324feefe39fc9002283190b (patch) | |
tree | 6662a189e17136fe07245e8039a059d5a54cee36 | |
parent | added pwm based motor speed control (diff) |
slightly improved ADC reading
-rw-r--r-- | motor-pwm/motor-pwm.c | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/motor-pwm/motor-pwm.c b/motor-pwm/motor-pwm.c index 715d758..7fd6108 100644 --- a/motor-pwm/motor-pwm.c +++ b/motor-pwm/motor-pwm.c @@ -69,17 +69,22 @@ void adc_init(void) { ADC_Init(ADC_FREE_RUNNING); ADC_SetupChannel(0); - adc_avg = 0; + + ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | ADC_CHANNEL0); } void adc_task(void) { - int16_t adc_val = (ADC_GetChannelReading(ADC_REFERENCE_AVCC | ADC_CHANNEL0) >> 4) + 193; - adc_val = (adc_val >= 255) ? 255 : adc_val; + if(ADC_IsReadingComplete()) { + int16_t adc_val = (ADC_GetResult() >> 4) + 193; + adc_val = (adc_val >= 255) ? 255 : adc_val; - adc_avg += (adc_val > adc_avg) ? 1 : ((adc_val < adc_avg) ? -1 : 0); - pwm_set(adc_avg); + printf("ADC: %4d\r", adc_val); + + adc_avg += (adc_val > adc_avg) ? 1 : ((adc_val < adc_avg) ? -1 : 0); + pwm_set(adc_avg); + } } void handle_cmd(uint8_t cmd) @@ -109,6 +114,7 @@ int main(void) adc_init(); sei(); + led_on(); for(;;) { int16_t BytesReceived = usbio_bytes_received(); while(BytesReceived > 0) { |