first version m05e00
This commit is contained in:
@@ -1,6 +1,56 @@
|
||||
#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
|
||||
|
||||
#define INT_TO_HEX_CHAR(n) ((n) < 10 ? ('0' + (n)) : ('A' + ((n) - 10)))
|
||||
void int_to_hex_string(uint64_t value, char *out, uint8_t num_digits) {
|
||||
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';
|
||||
}
|
||||
void word_to_hex(uint16_t value, char *out) {
|
||||
out[0] = INT_TO_HEX_CHAR((value >> 8) & 0x0F);
|
||||
out[1] = INT_TO_HEX_CHAR((value >> 4) & 0x0F);
|
||||
out[2] = INT_TO_HEX_CHAR(value & 0x0F);
|
||||
out[3] = '\0';
|
||||
}
|
||||
|
||||
void adc_init() {
|
||||
ADMUX = (1 << REFS0); // AVcc reference, ADC0
|
||||
ADCSRA = (1 << ADEN) // Enable ADC
|
||||
| (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Prescaler = 128
|
||||
}
|
||||
|
||||
uint16_t adc_read(uint8_t channel) {
|
||||
ADMUX = (ADMUX & 0xF0) | (channel & 0x0F); // Select ADC channel
|
||||
ADCSRA |= (1 << ADSC); // Start conversion
|
||||
while (ADCSRA & (1 << ADSC)); // Wait for completion
|
||||
return ADC;
|
||||
}
|
||||
|
||||
// description
|
||||
int main() {
|
||||
while(1);
|
||||
char buffer[4];
|
||||
SREG |= ENABLE_GLOBAL_INTERRUPT; // 7.3.1 : Status Register, bit 7 : I – Global Interrupt Enable
|
||||
uart_init();
|
||||
|
||||
adc_init();
|
||||
|
||||
while(1) {
|
||||
uint16_t value = adc_read(0); // Read from ADC0 (A0)
|
||||
// snprintf(buffer, sizeof(buffer), "ADC: %u\r\n", value);
|
||||
word_to_hex(value, buffer);
|
||||
uart_printstr(buffer);
|
||||
uart_printstr("\r\n");
|
||||
_delay_ms(20); // Wait 20ms
|
||||
}
|
||||
}
|
||||
|
||||
// ISR(ADC_vect) { // Table 12-6 : interrupt vector for ADC Conversion Complete
|
||||
// }
|
||||
Reference in New Issue
Block a user