diff --git a/headers/adc.h b/headers/adc.h index 7a11906..09ba4fd 100644 --- a/headers/adc.h +++ b/headers/adc.h @@ -12,4 +12,14 @@ (value) == 128 ? (1< ADC0 (ADC Input Channel 0) -// -> PCINT8 (Pin Change Interrupt 8) -// // 24.2 : The ADC generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL -// -// START A CONVERSTION in single conversion mode : -// - disabling the Power Reduction ADC bit, PRADC -// - writing a logical one to the ADC Start Conversion bit, ADSC void adc_init(uint8_t prescaler_value) { ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin diff --git a/module05/ex00/header.h b/module05/ex00/header.h index c58a847..810b00f 100644 --- a/module05/ex00/header.h +++ b/module05/ex00/header.h @@ -12,6 +12,11 @@ #include "usart.h" #include "adc.h" +// +// GLOBAL +// +extern volatile uint8_t adc_channel; + // // PROTOTYPES // diff --git a/module05/ex00/main.c b/module05/ex00/main.c index 01ca708..dbc21c5 100644 --- a/module05/ex00/main.c +++ b/module05/ex00/main.c @@ -16,20 +16,14 @@ volatile uint8_t adc_channel = 0; -// description +// read potentiometer value and print it in uart as hexadecimal 2-number int main() { char buffer[4]; SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable uart_init(); adc_init(ADC_PRESCALER); - while(1) { - uint16_t value = adc_read(0); // Read from ADC0 (A0) - int_to_hex_string(value, buffer, 2); - uart_printstr_endl(buffer); - _delay_ms(20); // Wait 20ms - } -} + timer_1B_init(); -// ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete -// } \ No newline at end of file + while(1); +} \ No newline at end of file diff --git a/module05/ex00/math.c b/module05/ex00/math.c new file mode 100644 index 0000000..0177fc0 --- /dev/null +++ b/module05/ex00/math.c @@ -0,0 +1,9 @@ +#include "header.h" + +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); + } + out[num_digits] = '\0'; +} \ No newline at end of file diff --git a/module05/ex01/header.h b/module05/ex01/header.h index c58a847..810b00f 100644 --- a/module05/ex01/header.h +++ b/module05/ex01/header.h @@ -12,6 +12,11 @@ #include "usart.h" #include "adc.h" +// +// GLOBAL +// +extern volatile uint8_t adc_channel; + // // PROTOTYPES // diff --git a/module05/ex01/main.c b/module05/ex01/main.c index 3a8d080..70a8c46 100644 --- a/module05/ex01/main.c +++ b/module05/ex01/main.c @@ -26,4 +26,4 @@ int main() { timer_1B_init(); while(1); -} +} \ No newline at end of file diff --git a/module05/ex01/math.c b/module05/ex01/math.c new file mode 100644 index 0000000..0177fc0 --- /dev/null +++ b/module05/ex01/math.c @@ -0,0 +1,9 @@ +#include "header.h" + +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); + } + out[num_digits] = '\0'; +} \ No newline at end of file diff --git a/module05/ex02/adc.c b/module05/ex02/adc.c index 859edb0..780e11b 100644 --- a/module05/ex02/adc.c +++ b/module05/ex02/adc.c @@ -21,9 +21,9 @@ uint16_t calibrate_temperature(uint16_t value, uint8_t speed) { // 24.8 : Tempe } void adc_print_dec(uint16_t value) { - if (adc_channel == ADC_THERMISTOR) { - value = calibrate_temperature(value, 5); - } + // if (adc_channel == ADC_THERMISTOR) { + // value = calibrate_temperature(value, 5); + // } char buffer[17] = {0}; uint16_to_string(value, buffer); uart_printstr(buffer);