diff --git a/makefile b/makefile new file mode 100644 index 0000000..9124fd3 --- /dev/null +++ b/makefile @@ -0,0 +1,45 @@ +MCU=attiny2313 +F_CPU=8000000 +PROG=usbasp + +CC=avr-g++ +OBJCOPY=avr-objcopy +CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11 + +SRC=$(wildcard src/*.cpp) + +default: clean build flash + +build: + mkdir -p bin + ${CC} ${CFLAGS} -o bin/main.bin ${SRC} -w + ${CC} ${CFLAGS} -o bin/main.elf ${SRC} -w + ${OBJCOPY} -j .text -j .data -O ihex bin/main.bin bin/main.hex + +flash: + avrdude -V -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex + +clean: + rm -f bin/* + +debug: flash avarice + avr-gdb -ex "target remote :4242" bin/main.elf + +avarice: + sleep 1 + avarice --file bin/main.elf --part ${MCU} --${DEBUGGER} :4242 & + +term: + screen /dev/ttyUSB0 1000000 + +eeprom: + sh -c "avrdude -p ${MCU} -c ${PROG} -t << END \ + write eeprom 0 1 2 3 \ + dump eeprom 0 16 \ + quit \ + END" + +fuse: + avrdude -p ${MCU} -c ${PROG} -U lfuse:w:0xe4:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m +lock: + avrdude -p ${MCU} -c ${PROG} -U lock:w:0xC0:m \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..29fb240 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,151 @@ +#include +#include +#include + +#define F_CPU 8000000UL // CPU Clock Frequency in Hz + +#define BAUD_RATE 9600 +#define BAUD_RATE_DIVISOR ((F_CPU / (16UL * BAUD_RATE)) - 1) + +// Define segments for each digit +#define A (1<= 0 && data <= 9 && currentDigit < 6) { + digits[d] = data; + d++; + } + // Echo back the received data + while (!(UCSRA & (1 << UDRE))); // Wait for the transmit buffer to be empty + UDR = data; // Send the received data back +} + +int uart_putchar(char c, FILE *stream) { + if (c == '\n') { + uart_putchar('\r', stream); + } + // Wait for empty transmit buffer + while (!(UCSRA & (1 << UDRE))); + // Put data into buffer, sends the data + UDR = c; + return 0; +} + + +int main() { + // Set PORTB as output for segments + DDRB = 0xFF; + // Set PORTA and PORTD as output for digit control + DDRA |= (1<> 8); + UBRRL = BAUD_RATE_DIVISOR; + // Enable receiver and RX complete interrupt + UCSRB |= (1<