first version m05e00

This commit is contained in:
hugogogo
2025-03-13 13:14:07 +01:00
parent 3120668a34
commit 24158c8f4d
4 changed files with 128 additions and 2 deletions

View File

@@ -15,6 +15,10 @@
// PROTOTYPES
//
// main.c
// uart.c
void uart_init();
void uart_tx(char c);
void uart_printstr(const char* str);
//
// MACROS

View File

@@ -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
// }

72
module05/ex00/uart.c Normal file
View File

@@ -0,0 +1,72 @@
#include "header.h"
// MACROS
// USART
#define USART_BAUDRATE 115200
#define INPUT_SIZE 7
// GLOBAL VARIABLES
//
// FUNCTIONS
//
void uart_init() {
UBRR0H = (unsigned char) (BAUD_PRESCALER(USART_BAUDRATE) >> 8); // 20.11.5 : UBRRnL and UBRRnH USART Baud Rate Registers
UBRR0L = (unsigned char) BAUD_PRESCALER(USART_BAUDRATE);
UCSR0C = DATA_EIGHT_BIT; // 20.11.4 : set Frame Format
UCSR0B = TRANSMITTER_ENABLED; // 20.11.3 : enable Receiver, Transmitter, and interrupts
}
// char uart_rx(void) {
// while (!TEST(UCSR0A, RXC0)); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data)
// return UDR0; // 20.11.1 : get data in buffer, UDRn USART I/O Data Register (read and write)
// }
void uart_tx(char c) {
while (!TEST(UCSR0A, UDRE0)); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty)
UDR0 = (unsigned char) c; // 20.11.1 : Put data into buffer, UDRn USART I/O Data Register (read and write)
}
void uart_printstr(const char* str) {
while (*str) {
uart_tx(*str);
str++;
}
}
// ISR(USART_RX_vect) { // Table 12-6 : we select the code for USART Receive
// char received_char = UDR0; // read received character
// if (received_char == '\b' || received_char == 127) { // if backspace is received
// if (input_index <= 0) {
// return;
// }
// if (input_index >= INPUT_SIZE) {
// return;
// }
// remove_last_character();
// input_index--;
// uart_tx('\b'); // Move cursor back
// uart_tx(' '); // Erase the character on screen
// uart_tx('\b'); // Move cursor back again
// } else if (received_char == '\n' || received_char == '\r') { // if enter is received
// if (input_index != INPUT_SIZE) {
// return;
// }
// set_color((char *)color_input);
// reset_input();
// } else { // any other character
// if (input_index >= INPUT_SIZE) {
// return;
// }
// if (!is_valid_color_input(received_char)) {
// return;
// }
// fill_str(received_char);
// uart_tx(received_char);
// input_index++;
// }
// }
// ISR(USART_TX_vect) { // Table 12-6 : we select the code for USART Transmit
// char received_char = UDR0; // read received character
// }

2
rush00

Submodule rush00 updated: 4afab54715...e4ea057ebf