wip commenting

This commit is contained in:
hugogogo
2025-03-13 16:02:27 +01:00
parent 24158c8f4d
commit 7ca02352d9
7 changed files with 62 additions and 36 deletions

27
module05/ex00/adc.c Normal file
View File

@@ -0,0 +1,27 @@
#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
#define ADC_PRESCALER 8
void adc_init() {
ADMUX = (1 << REFS0); // Table 24-3 : set voltage reference, AVCC with external capacitor at AREF pin
ADCSRA = (1 << ADEN) // 24.9.2 : enable ADC
| ADC_PRESCALE_SET(ADC_PRESCALER); // Table 24-5 : prescaler ADC
}
uint16_t adc_read(uint8_t channel) {
CLEAR(PRR, PRADC); // 24.3 : ensure power reduction is disabled for ADC, (10.11.3 : PRR Power Reduction Register)
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F); // Select ADC channel
ADCSRA |= (1 << ADSC); // 24.9.2 : Start conversion, ADSC: ADC Start Conversion
while (ADCSRA & (1 << ADSC)); // Wait for completion
return ADC;
}