fix rebase module05

This commit is contained in:
hugogogo
2025-03-15 16:31:30 +01:00
parent 8fef07135a
commit 1d5e8d4ee7
9 changed files with 46 additions and 22 deletions

View File

@@ -12,4 +12,14 @@
(value) == 128 ? (1<<ADPS2 | 1<<ADPS1 | 1<<ADPS0) : \
(0<<ADPS2 | 0<<ADPS1 | 0<<ADPS0))
// Table 24-6 : ADC Auto Trigger Source Selections
#define ADC_TRIGGER_FREE_RUNNING (0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0)
#define ADC_TRIGGER_ANALOG_COMPARE (0 << ADTS2) | (0 << ADTS1) | (1 << ADTS0)
#define ADC_TRIGGER_EXTERNAL_INTERRUPT_0 (0 << ADTS2) | (1 << ADTS1) | (0 << ADTS0)
#define ADC_TRIGGER_TIMER_0_COMPARE_A (0 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)
#define ADC_TRIGGER_TIMER_0_OVERFLOW (1 << ADTS2) | (0 << ADTS1) | (0 << ADTS0)
#define ADC_TRIGGER_TIMER_1_COMPARE_B (1 << ADTS2) | (0 << ADTS1) | (1 << ADTS0)
#define ADC_TRIGGER_TIMER_1_OVERFLOW (1 << ADTS2) | (1 << ADTS1) | (0 << ADTS0)
#define ADC_TRIGGER_TIMER_1_CAPTURE_EVENT (1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)
#endif // ADC_H

View File

@@ -1,14 +1,6 @@
#include "header.h"
// Table 14-6. Port C Pins Alternate Functions
// - PC0 -> 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

View File

@@ -12,6 +12,11 @@
#include "usart.h"
#include "adc.h"
//
// GLOBAL
//
extern volatile uint8_t adc_channel;
//
// PROTOTYPES
//

View File

@@ -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
// }
while(1);
}

9
module05/ex00/math.c Normal file
View File

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

View File

@@ -12,6 +12,11 @@
#include "usart.h"
#include "adc.h"
//
// GLOBAL
//
extern volatile uint8_t adc_channel;
//
// PROTOTYPES
//

View File

@@ -26,4 +26,4 @@ int main() {
timer_1B_init();
while(1);
}
}

9
module05/ex01/math.c Normal file
View File

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

View File

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