update math to add convert int to string

This commit is contained in:
hugogogo
2025-03-15 15:20:02 +01:00
parent 4c25c39e32
commit e33261846b
3 changed files with 27 additions and 17 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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--;
}
}