diff --git a/module05/ex00/adc.h b/headers/adc.h similarity index 100% rename from module05/ex00/adc.h rename to headers/adc.h diff --git a/module05/ex00/adc.c b/module05/ex00/adc.c index 12a2ccc..4e0e062 100644 --- a/module05/ex00/adc.c +++ b/module05/ex00/adc.c @@ -10,18 +10,16 @@ // - disabling the Power Reduction ADC bit, PRADC // - writing a logical one to the ADC Start Conversion bit, ADSC -#define ADC_PRESCALER 8 - -void adc_init() { - ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin - ADCSRA = (1 << ADEN) // 24.9.2 : enable ADC - | ADC_PRESCALE_SET(ADC_PRESCALER); // Table 24-5 : prescaler ADC +void adc_init(uint8_t prescaler_value) { + ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin + ADCSRA = (1 << ADEN); // 24.9.2 : enable ADC + ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC } uint16_t adc_read(uint8_t channel) { - CLEAR(PRR, PRADC); // 24.3 : ensure power reduction is disabled for ADC, (10.11.3 : PRR – Power Reduction Register) - ADMUX = (ADMUX & 0xF0) | (channel & 0x0F); // Select ADC channel - ADCSRA |= (1 << ADSC); // 24.9.2 : Start conversion, ADSC: ADC Start Conversion - while (ADCSRA & (1 << ADSC)); // Wait for completion + CLEAR(PRR, PRADC); // 24.3 : ensure power reduction is disabled for ADC, (10.11.3 : PRR – Power Reduction Register) + ADMUX = (ADMUX & 0b11110000) | (channel & 0b1111); // Table 24-4 : Select ADC channel, (Table 14-6 : alternate function for RV1 on PC0 -> ADC0) + ADCSRA |= (1 << ADSC); // 24.9.2 : Start conversion, ADSC: ADC Start Conversion + while (ADCSRA & (1 << ADSC)); // Wait for completion return ADC; } diff --git a/module05/ex00/header.h b/module05/ex00/header.h index cf16a69..6b7ea32 100644 --- a/module05/ex00/header.h +++ b/module05/ex00/header.h @@ -18,13 +18,13 @@ // main.c void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits); // adc.c -void adc_init(); +void adc_init(uint8_t prescaler_value); uint16_t adc_read(uint8_t channel); // uart.c void uart_init(); void uart_tx(char c); void uart_printstr(const char* str); -void uart_printstr_endl(const char* str) +void uart_printstr_endl(const char* str); // // MACROS diff --git a/module05/ex00/main.c b/module05/ex00/main.c index 8a7bf92..b885c02 100644 --- a/module05/ex00/main.c +++ b/module05/ex00/main.c @@ -1,6 +1,11 @@ #include "header.h" -void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { +// 1.1.7 : AVCC is the supply voltage pin for the A/D Converter, PC3:0, and ADC7:6 +// 1.1.8 : AREF is the analog reference pin for the A/D Converter + +#define ADC_PRESCALER 8 + +void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // num_digits : number of digit of the output, ex 2 for 3FF (1023) -> FF for (uint8_t i = 0; i < num_digits; ++i) { uint8_t shift = (num_digits - 1 - i) * 4; out[i] = INT_TO_HEX_CHAR((value >> shift) & 0x0F); @@ -10,10 +15,10 @@ void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // description int main() { - const char buffer[4]; + char buffer[4]; SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable uart_init(); - adc_init(); + adc_init(ADC_PRESCALER); while(1) { uint16_t value = adc_read(0); // Read from ADC0 (A0)