From d6f38d0dfc4f602a6c790fd470deb2b2fd23da77 Mon Sep 17 00:00:00 2001 From: Michael Gernoth Date: Sun, 22 Aug 2010 16:16:14 +0200 Subject: [PATCH 1/1] serial support --- bmc/.gitignore | 3 +++ bmc/Makefile | 4 ++-- bmc/bmc.c | 52 +++++++++++++++++++++++++++----------------------- bmc/usart.c | 35 +++++++++++++++++++++++++++++++++ bmc/usart.h | 3 +++ 5 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 bmc/.gitignore create mode 100644 bmc/usart.c create mode 100644 bmc/usart.h diff --git a/bmc/.gitignore b/bmc/.gitignore new file mode 100644 index 0000000..1fcc48e --- /dev/null +++ b/bmc/.gitignore @@ -0,0 +1,3 @@ +bmc +bmc.bin +*.o diff --git a/bmc/Makefile b/bmc/Makefile index 2e566d0..7915e96 100644 --- a/bmc/Makefile +++ b/bmc/Makefile @@ -1,5 +1,5 @@ CC=avr-gcc -CFLAGS=-mmcu=atmega16 -g +CFLAGS=-mmcu=atmega16 -DF_CPU=8000000UL -Wall -g OBJCOPY=avr-objcopy PROG=dragon_jtag PORT=usb @@ -7,7 +7,7 @@ PMCU=m16 all: bmc.bin -bmc: bmc.o +bmc: bmc.o usart.o bmc.bin: bmc $(OBJCOPY) -j .text -j .data -O binary $^ $@ diff --git a/bmc/bmc.c b/bmc/bmc.c index 06a3829..a0387b3 100644 --- a/bmc/bmc.c +++ b/bmc/bmc.c @@ -1,24 +1,28 @@ -#define F_CPU 1000000UL - -#include - -int main(void) -{ - uint8_t pb = 0x00; - int8_t dir = 1; - volatile uint16_t i; - - DDRB = 0xff; - - while(1) { - pb += dir; - PORTB = pb; - - for (i = 0; i < (pb<<5); i++) {} - - if ((pb == 0) || (pb == 0xff)) - dir = -dir; - } - - return 0; -} +#include +#include +#include "usart.h" + +int main(void) +{ + uint8_t pb = 0x00; + int8_t dir = 1; + volatile uint16_t i; + + DDRB = 0xff; + + usart_init(); + + printf("Hallo!\n"); + + while(1) { + pb += dir; + PORTB = pb; + + for (i = 0; i < (pb<<5); i++) {} + + if ((pb == 0) || (pb == 0xff)) + dir = -dir; + } + + return 0; +} diff --git a/bmc/usart.c b/bmc/usart.c new file mode 100644 index 0000000..36ba83c --- /dev/null +++ b/bmc/usart.c @@ -0,0 +1,35 @@ +#include +#include + +#include "usart.h" + +#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) +#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) +#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) + +#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010)) +#error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch! +#endif + +int usart_put(char c, FILE *stream) +{ + if (c == '\n') + usart_put('\r', stream); + + while(!(UCSRA & (1<> 8; + UBRRL = UBRR_VAL & 0xFF; + + stdout = &usart_stdout; +} diff --git a/bmc/usart.h b/bmc/usart.h new file mode 100644 index 0000000..d0ce4a5 --- /dev/null +++ b/bmc/usart.h @@ -0,0 +1,3 @@ +#define BAUD 9600UL + +void usart_init(); -- 2.39.5