added uart module

This commit is contained in:
Sven Vogel 2023-12-09 11:22:31 +01:00
parent 6eaf4518fa
commit 586644e26f
8 changed files with 117 additions and 2 deletions

View File

@ -13,4 +13,8 @@
#define SET_OUTPUT(port, pin) SET_BIT(port, pin) #define SET_OUTPUT(port, pin) SET_BIT(port, pin)
#define SET_INPUT(port, pin) CLR_BIT(port, pin) #define SET_INPUT(port, pin) CLR_BIT(port, pin)
// makes pin 4 of port B a pull up resistor
// example: PULL_UP_RESISTOR(DDRB, PORTB, DDB4)
#define PULL_UP_RESISTOR(port, output, pin) CLR_BIT(port, pin); SET_BIT(output, pin)
#endif //ARDUINO_BITS_H #endif //ARDUINO_BITS_H

View File

@ -15,5 +15,6 @@
// custom header files // custom header files
#include "bits.h" #include "bits.h"
#include "interrupt.h" #include "interrupt.h"
#include "uart.h"
#endif //ARDUINO_PRELUDE_H #endif //ARDUINO_PRELUDE_H

25
include/uart.h Normal file
View File

@ -0,0 +1,25 @@
//
// Created by servostar on 09.12.23.
//
#ifndef ARDUINO_UART_H
#define ARDUINO_UART_H
#include "prelude.h"
#include <stddef.h>
#include <inttypes.h>
void uart_init(uint16_t baud);
uint8_t uart_read();
void uart_read(uint8_t* buffer, size_t size);
void uart_write(uint8_t frame);
void uart_write_bytes(const uint8_t* buffer, size_t bytes);
void uart_write_string(const char* string);
#endif //ARDUINO_UART_H

View File

@ -21,4 +21,5 @@ build_src_filter =
-<button_poll/> -<button_poll/>
-<trafficlight/> -<trafficlight/>
-<summer/> -<summer/>
+<summer_controlled/> -<summer_controlled/>
+<uart/>

View File

@ -7,7 +7,8 @@ extern void loop();
//#include "button_interupt/button.h" //#include "button_interupt/button.h"
//#include "trafficlight/trafficlight.h" //#include "trafficlight/trafficlight.h"
//#include "summer/summer.h" //#include "summer/summer.h"
#include "summer_controlled/summer.h" //#include "summer_controlled/summer.h"
#include "uart/uart_read_write.h"
int main() { int main() {
init(); init();

49
src/uart.cpp Normal file
View File

@ -0,0 +1,49 @@
//
// Created by servostar on 09.12.23.
//
#include "uart.h"
void uart_init(uint16_t baud) {
uint16_t ubrr = F_CPU / 16 / baud - 1;
UBRR0H = (uint8_t) (ubrr >> 8);
UBRR0L = (uint8_t) (ubrr & 0xFF);
UCSR0B |= (1 << RXEN0) | (1 << TXEN0); // enable receiver and transmitter
UCSR0C |= (1 << UCSZ01) | (3 << UCSZ00); // set frame: 8 bit
UCSR0C &= ~(1 << USBS0); // 1 stop bit
UCSR0B |= (1 << RXCIE0); // enable receiver interrupt
}
void uart_write(uint8_t frame) {
// wait until TX is free
while (!(UCSR0A & (1 << UDRE0)));
// write data
UDR0 = frame;
}
uint8_t uart_read() {
// wait until RX is free
while (!(UCSR0A & (1 << RXC0)));
// read data
return UDR0;
}
void uart_read_bytes(uint8_t* buffer, size_t size) {
for (size_t i = 0; i < size; i++) {
buffer[i] = uart_read();
}
}
void uart_write_bytes(uint8_t* buffer, size_t bytes) {
for (size_t i = 0; i < bytes; i++) {
uart_write(buffer[i]);
}
}
void uart_write_string(const char* string) {
while(*string)
uart_write(*string++);
}

View File

@ -0,0 +1,21 @@
//
// Created by servostar on 09.12.23.
//
#include "uart_read_write.h"
#include "uart.h"
#define BAUD 9600
ISR (USART_RX_vect)
{
uart_write_string("Hello, World!\n");
}
void init() {
uart_init(BAUD);
}
void loop() {
}

View File

@ -0,0 +1,13 @@
//
// Created by servostar on 09.12.23.
//
#ifndef ARDUINO_UART_READ_WRITE_H
#define ARDUINO_UART_READ_WRITE_H
#include "prelude.h"
void init();
void loop();
#endif // ARDUINO_UART_READ_WRITE_H