From 2f5d151fbb46fb584581e6237378a5aad1847499 Mon Sep 17 00:00:00 2001 From: Harvie Date: Sat, 14 Jan 2012 07:08:58 +0100 Subject: [PATCH] First experiments with AVR USART --- avr/usart/Makefile | 30 ++++++++++++++++++++++++++++++ avr/usart/main.c | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 avr/usart/Makefile create mode 100644 avr/usart/main.c diff --git a/avr/usart/Makefile b/avr/usart/Makefile new file mode 100644 index 0000000..2012d57 --- /dev/null +++ b/avr/usart/Makefile @@ -0,0 +1,30 @@ +#MCU=at90s2313 +MCU=attiny2313 +PORT=/dev/ttyUSB0 +CC=avr-gcc +OBJCOPY=avr-objcopy +PROJECT=main +RESET=false +# optimize for size: +CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues +#------------------- +all: $(PROJECT).hex +#------------------- +$(PROJECT).hex : $(PROJECT).out + $(OBJCOPY) -R .eeprom -O ihex $(PROJECT).out $(PROJECT).hex +$(PROJECT).out : $(PROJECT).o + $(CC) $(CFLAGS) -o $(PROJECT).out -Wl,-Map,$(PROJECT).map $(PROJECT).o +$(PROJECT).o : $(PROJECT).c + $(CC) $(CFLAGS) -Os -c $(PROJECT).c +asm : $(PROJECT).c + $(CC) $(CFLAGS) -O -S $(PROJECT).c +# you need to erase first before loading the program. +# load (program) the software into the eeprom: +load: $(PROJECT).hex + $(RESET) && stty -F $(PORT) 19200 hupcl && sleep 2 && stty -F $(PORT) 19200 -hupcl && sleep 2 || true + avrdude -cstk500v1 -P$(PORT) -b19200 -p $(MCU) -Uflash:w:$(PROJECT).hex:i || $(MAKE) RESET=true MAKE=true load + # uisp -dlpt=/dev/parport0 --erase --upload --verify if=$(PROJECT).hex -dprog=dapa -v=3 --hash=12 +#------------------- +clean: + rm -f *.o *.map *.out *.hex +#------------------- diff --git a/avr/usart/main.c b/avr/usart/main.c new file mode 100644 index 0000000..853f82a --- /dev/null +++ b/avr/usart/main.c @@ -0,0 +1,27 @@ +#include +#define F_CPU 1000000UL //1MHz default internal RC oscillator setting +//util/delay.h and util/setbaud.h + +#define USART_BAUDRATE 9600 +#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1) + +int main (void) +{ + char ReceivedByte; + + UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry + //UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes (not in attiny2313?) + + UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register + UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register + + for (;;) // Loop forever + { + while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR + ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived" + + while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it + UDR = ReceivedByte; // Echo back the received byte back to the computer + } +} + -- 2.30.2