From e9e3cd09dd06594c78bbeb045aefb6a9bb6052f5 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 15 Mar 2025 14:42:03 +0100 Subject: [PATCH] fix 8 bit precision in ex00 and ex01 --- module05/ex00/adc.c | 3 ++- module05/ex01/adc.c | 8 +++++--- module05/ex01/main.c | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/module05/ex00/adc.c b/module05/ex00/adc.c index 097e7ab..fee381c 100644 --- a/module05/ex00/adc.c +++ b/module05/ex00/adc.c @@ -12,6 +12,7 @@ void adc_init(uint8_t prescaler_value) { ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin + ADMUX |= (1 << ADLAR); // 24.9.1 : result is left adjusted, meaning the first 8 bits values are readable in ADCH, (24.9.3.2 : ADC data register is not updated util ADCH is read) ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC ADCSRA |= (1 << ADATE); // 24.9.2 : enable Auto Trigger -> it will start a conversion on the selected channel in ADMUX when the selected source (in ADCSRB) is triggered ADCSRA |= (1 << ADIE); // 24.9.2 : enable ADC Interrupt @@ -29,6 +30,6 @@ void adc_print_hex(uint8_t value) { } ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete - uint16_t value = ADC; + uint8_t value = ADCH; // 24.9.3.2 : read ADCH 8 bits precision adc_print_hex(value); } diff --git a/module05/ex01/adc.c b/module05/ex01/adc.c index ffb6dc0..eda29de 100644 --- a/module05/ex01/adc.c +++ b/module05/ex01/adc.c @@ -4,6 +4,7 @@ void adc_init(uint8_t prescaler_value) { ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin + ADMUX |= (1 << ADLAR); // 24.9.1 : result is left adjusted, meaning the first 8 bits values are readable in ADCH, (24.9.3.2 : ADC data register is not updated util ADCH is read) ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC ADCSRA |= (1 << ADATE); // 24.9.2 : enable Auto Trigger -> it will start a conversion on the selected channel in ADMUX when the selected source (in ADCSRB) is triggered ADCSRA |= (1 << ADIE); // 24.9.2 : enable ADC Interrupt @@ -13,7 +14,6 @@ void adc_init(uint8_t prescaler_value) { ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel 0 } - void adc_print_hex(uint8_t value) { char buffer[3] = {0}; int_to_hex_string(value, buffer, 2); @@ -21,10 +21,12 @@ void adc_print_hex(uint8_t value) { } ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete - uint16_t value = ADC; + uint8_t value = ADCH; // 24.9.3.2 : read ADCH 8 bits precision adc_print_hex(value); + + // loop through channel adc_channel = (adc_channel + 1) % 3; // loop through channels - ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel + ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select next ADC channel if (adc_channel != 0) { uart_printstr(", "); ADCSRA |= (1 << ADSC); // 24.9.2 : start next conversion diff --git a/module05/ex01/main.c b/module05/ex01/main.c index eb13df2..3a8d080 100644 --- a/module05/ex01/main.c +++ b/module05/ex01/main.c @@ -16,7 +16,7 @@ volatile uint8_t adc_channel = 0; // Table 14-6 : alternate function for RV1 on PC0 -> ADC0, LDR on PC1 -> ADC1, NTC on PC2 -> ADC2 ... -// read 3 analog values and print them in uart as hexadecimal 2-number : potentiometer RV1 (Variable Resistor), photoresistor LDR (Light Dependent Resistor), thermistor NTC (Negative Temperature Coefficient) +// read 3 analog values at 8 bits and print them in uart as hexadecimal 2-number : potentiometer RV1 (Variable Resistor), photoresistor LDR (Light Dependent Resistor), thermistor NTC (Negative Temperature Coefficient) int main() { char buffer[4]; SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable