feat(ex01): done

This commit is contained in:
Maix0 2026-04-19 18:05:42 +02:00
parent dd41c1f072
commit f49352a412
15 changed files with 921 additions and 0 deletions

38
ex01/src/main.c Normal file
View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mguillot <mguillot@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/04/18 18:50:52 by mguillot #+# #+# */
/* Updated: 2026/04/19 16:57:19 by mguillot ### ########.fr */
/* */
/* ************************************************************************** */
#include <avr/io.h>
#include <util/delay.h>
#include "adc.h"
#include "mystd.h"
#include "uart.h"
int main(void) {
uart_init();
adc_init(ADC_AVCC, ADC_PRESCALER_64);
while (true) {
uint8_t d_pot = adc_read_pin(ADC_ADC0);
uint8_t d_ldr = adc_read_pin(ADC_ADC1);
uint8_t d_ntc = adc_read_pin(ADC_ADC2);
uart_send_u8_hex(d_pot);
uart_tx(',');
uart_tx(' ');
uart_send_u8_hex(d_ldr);
uart_tx(',');
uart_tx(' ');
uart_send_u8_hex(d_ntc);
uart_sendstring("\r\n");
_delay_ms(20);
}
}

45
ex01/src/rgb.c Normal file
View file

@ -0,0 +1,45 @@
#include <avr/io.h>
#include "mystd.h"
#include "timer0.h"
#include "timer2.h"
#define D5_R PD5
#define D5_G PD6
#define D5_B PD3
#define RGB_MASK (_BV(D5_R) | _BV(D5_G) | _BV(D5_B))
void init_rgb(void) {
t0_init_fpwm_3(PRESCALER_64);
t2_init_fpwm_3(PRESCALER_64);
t0_set_out_mode(TO_A | TO_B, TOM_00);
t2_set_out_mode(TO_B, TOM_00);
DDRD |= RGB_MASK;
}
void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
if (r == 0x00) {
t0_set_out_mode(TO_B, TOM_00);
PORTD = (PORTD & ~_BV(D5_R));
} else {
t0_set_out_mode(TO_B, TOM_10);
t0_set_ocr(TO_B, r);
}
if (g == 0x00) {
t0_set_out_mode(TO_A, TOM_00);
PORTD = (PORTD & ~_BV(D5_G));
} else {
t0_set_out_mode(TO_A, TOM_10);
t0_set_ocr(TO_A, g);
}
if (b == 0x00) {
t2_set_out_mode(TO_B, TOM_00);
PORTD = (PORTD & ~_BV(D5_B));
} else {
t2_set_out_mode(TO_B, TOM_10);
t2_set_ocr(TO_B, b);
}
}

107
ex01/src/uart.c Normal file
View file

@ -0,0 +1,107 @@
#include "uart.h"
#include <avr/io.h>
#include "mystd.h"
#include "utils.h"
#define BAUD_RATE 115200
#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1)
// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit
// 115200 8N1
void uart_init(void) {
// Set baud rate
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
UBRR0L = (uint8_t)(UBRR_VALUE);
UCSR0A |= BV(U2X0);
// Enable transmitter
UCSR0B = BV(TXEN0) | BV(RXEN0);
// Set frame format: 8 data bits, no parity, 1 stop bit
UCSR0C = BV(UCSZ01) | BV(UCSZ00);
// Set TX (PD1) as output
DDRD |= BV(PD1);
}
void uart_tx(char data) {
// wait for transmit buffer to be empty
while (!(UCSR0A & BV(UDRE0)))
;
// load data into transmit register
UDR0 = data;
}
char uart_rx(void) {
while (!(UCSR0A & BV(RXC0)))
;
return UDR0;
}
void uart_sendstring(const char* str) {
if (!str)
return;
while (*str) {
uart_tx(*str);
str++;
}
}
void uart_send_u8(uint8_t val) {
if (val == 0)
return uart_tx('0');
char buf[4] = {0, 0, 0, 0};
uint8_t idx = 0;
bool print = false;
uint8_t modulus = 100;
while (modulus) {
uint8_t digit = val / modulus;
if (print || digit != 0) {
print = true;
buf[idx++] = '0' + digit;
}
val %= modulus;
modulus /= 10;
}
uart_sendstring(buf);
}
void uart_send_u16(uint16_t val) {
if (val == 0)
return uart_tx('0');
char buf[6] = {0, 0, 0, 0, 0, 0};
uint8_t idx = 0;
bool print = false;
uint16_t modulus = 10000;
while (modulus) {
uint8_t digit = val / modulus;
if (print || digit != 0) {
print = true;
buf[idx++] = '0' + digit;
}
val %= modulus;
modulus /= 10;
}
uart_sendstring(buf);
}
void uart_send_u8_hex(uint8_t val) {
char buf[3] = {0, 0, 0};
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
uart_sendstring(buf);
}
void uart_send_u16_hex(uint16_t val) {
char buf[5] = {0, 0, 0, 0, 0};
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
uart_sendstring(buf);
}

39
ex01/src/utils.c Normal file
View file

@ -0,0 +1,39 @@
#include "utils.h"
#include "mystd.h"
// this just burns cycles.
// the volatile is important, it means that the cpu can't optimize any
// read/writes for the value
static inline void spin_loop(volatile uint16_t counts) {
while (counts)
counts--;
}
void delay_ms(uint16_t ms) {
while (ms) {
// this value was taken using a delay of 500ms, and just recording the led
// blinking. it seems to be high enough such that each loop of delay_loop
// takes 1ms :D
spin_loop((F_CPU) / 5000);
ms--;
}
}
void ft_bzero(void* data, uint16_t size) {
char* d = data;
while (size) {
*d = 0;
d++;
size--;
}
}
uint8_t ft_stridx(const char* str, char chr) {
if (!str)
return -1;
for (uint8_t i = 0; str[i]; i++) {
if (str[i] == chr)
return i;
}
return -1;
}