diff --git a/module05/ex02/adc.c b/module05/ex02/adc.c index 9022c6a..e70ff09 100644 --- a/module05/ex02/adc.c +++ b/module05/ex02/adc.c @@ -10,27 +10,13 @@ void adc_init(uint8_t prescaler_value) { ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC ADCSRB = ADC_TRIGGER_TIMER_1_COMPARE_B; // Table 24-6 : ADC Auto Trigger Source - ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b1111); // Table 24-4 : Select ADC channel 0 + ADMUX = (ADMUX & 0b11110000) | (adc_channel & 0b00001111); // Table 24-4 : Select ADC channel 0 } void adc_print_dec(uint16_t value) { char buffer[17] = {0}; - - // handle zero case - if (value == 0) { - buffer[15] = '0'; - uart_printstr(&buffer[15]); - return; - } - - uint8_t pos = 0; - while (value) { - uint8_t digit = value % 10; - buffer[15 - pos] = digit + '0'; - value /= 10; - pos++; - } - uart_printstr(&buffer[16 - pos]); // send only from first digit + uint16_to_string(value, buffer); + uart_printstr(buffer); } ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete diff --git a/module05/ex02/header.h b/module05/ex02/header.h index 810b00f..722e6b7 100644 --- a/module05/ex02/header.h +++ b/module05/ex02/header.h @@ -25,6 +25,7 @@ extern volatile uint8_t adc_channel; void timer_1B_init(); // math.c void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits); +void uint16_to_string(uint16_t value, char *out); // adc.c void adc_init(uint8_t prescaler_value); uint16_t adc_read(uint8_t channel); diff --git a/module05/ex02/math.c b/module05/ex02/math.c index 1f61877..aa4f250 100644 --- a/module05/ex02/math.c +++ b/module05/ex02/math.c @@ -7,3 +7,26 @@ void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // nu } out[num_digits] = '\0'; } + +void uint16_to_string(uint16_t value, char buffer[17]) { + // handle zero case + if (value == 0) { + buffer[0] = '0'; + return; + } + + uint8_t size = -1; + uint16_t copy = value; + + while (copy) { + copy /= 10; + size++; + } + + while (value) { + uint8_t digit = value % 10; + buffer[size] = digit + '0'; + value /= 10; + size--; + } +} \ No newline at end of file