update math to add convert int to string
This commit is contained in:
@@ -10,27 +10,13 @@ void adc_init(uint8_t prescaler_value) {
|
|||||||
ADCSRA |= ADC_PRESCALE_SET(prescaler_value); // Table 24-5 : prescaler ADC
|
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
|
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) {
|
void adc_print_dec(uint16_t value) {
|
||||||
char buffer[17] = {0};
|
char buffer[17] = {0};
|
||||||
|
uint16_to_string(value, buffer);
|
||||||
// handle zero case
|
uart_printstr(buffer);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ extern volatile uint8_t adc_channel;
|
|||||||
void timer_1B_init();
|
void timer_1B_init();
|
||||||
// math.c
|
// math.c
|
||||||
void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits);
|
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
|
// adc.c
|
||||||
void adc_init(uint8_t prescaler_value);
|
void adc_init(uint8_t prescaler_value);
|
||||||
uint16_t adc_read(uint8_t channel);
|
uint16_t adc_read(uint8_t channel);
|
||||||
|
|||||||
@@ -7,3 +7,26 @@ void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) { // nu
|
|||||||
}
|
}
|
||||||
out[num_digits] = '\0';
|
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--;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user