This commit is contained in:
Christoph J. Scherr 2023-12-09 11:27:55 +01:00
parent e665d504be
commit d89ecc7062
6 changed files with 80 additions and 4 deletions

View File

@ -1,2 +1,2 @@
CompileFlags:
Add: -I/usr/avr/include
Add: -I/usr/lib/avr/include

View File

@ -1,7 +1,7 @@
/*
* Use the timers pwm to dim a light on demand.
*/
#define F_CPU 16000000UL
*/
#define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h>

View File

@ -12,12 +12,14 @@ else
avr-gcc -mmcu=atmega328p -DF_CPU=16000000L \
-DARDUINO=10607 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR \
-I/usr/avr/include \
main.c -o $BUILD/main.elf
-I/usr/lib/avr/include \
../include/uart.c main.c -o $BUILD/main.elf
avr-objcopy -O ihex -R .eeprom $BUILD/main.elf $BUILD/main.hex
avr-size -A $BUILD/main.elf
avrdude \
"-C/etc/avrdude/avrdude.conf" -v -V \
"-C/etc/avrdude.conf" -v -V \
-patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D \
-Uflash:w:$BUILD/main.hex:i

35
include/uart.c Normal file
View File

@ -0,0 +1,35 @@
#include "uart.h"
void uart_init() {
/* This make stuff not work, idk
// set baud rate
UBRR0H = (MYUBRR>>8);
UBRR0L = MYUBRR;
*/
/* Enable receiver and transmitter */
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C |= (1<<UCSZ01)|(3<<UCSZ00);
//Lokales interrupt enable
UCSR0B |= (1 << RXCIE0);
}
void uart_printc(char c) {
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
{
// wartet nur
}
/* Put data into buffer, sends the data */
UDR0 = c;
}
void uart_prints(char* s) {
while(*s)
uart_printc(*s++);
uart_printc('\n');
}
char uart_getc() {
while (UCSR0A & (1 << RXC0));
return UDR0;
}

12
include/uart.h Normal file
View File

@ -0,0 +1,12 @@
#define F_CPU 16000000UL
#define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#pragma once
void uart_init();
void uart_prints(char* s);
void uart_printc(char c);
char uart_getc();

27
print/main.c Normal file
View File

@ -0,0 +1,27 @@
#define F_CPU 16000000UL
#define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#include "../include/uart.h"
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
int main (void)
{
uart_init();
sei();
while (1)
{
// uart_prints("foofofofo");
// _delay_ms(1000);
}
}
ISR (USART_RX_vect) {
// echo the message
char c = UDR0;
uart_printc(c);
}