Compare commits

...

4 Commits

Author SHA1 Message Date
jimmy 36b4410cfb Ignore bin folder 2024-04-20 09:39:27 +12:00
jimmy ddbeac535d Working version 2024-04-20 09:36:44 +12:00
jimmy 234001628a Add lib folder 2024-04-20 09:36:16 +12:00
jimmy 81d6df812d Add usart lib 2024-04-20 09:34:50 +12:00
5 changed files with 43 additions and 65 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "lib/usart"]
path = lib/usart
url = git@git.technical.kiwi:avr/usart.git

1
lib/usart Submodule

@ -0,0 +1 @@
Subproject commit 05034f925671f06abe15097326b05eceeef0bc1e

View File

@ -7,13 +7,14 @@ OBJCOPY=avr-objcopy
CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11
SRC=$(wildcard src/*.cpp)
LIB=$(wildcard lib/*/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
${CC} ${CFLAGS} -o bin/main.bin ${SRC} ${LIB} -w
${CC} ${CFLAGS} -o bin/main.elf ${SRC} ${LIB} -w
${OBJCOPY} -j .text -j .data -O ihex bin/main.bin bin/main.hex
flash:

View File

@ -1,12 +1,10 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>
#include "../lib/usart/src/usart.h"
#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<<PB5)
#define B (1<<PB7)
@ -17,8 +15,6 @@
#define G (1<<PB0)
#define DP (1<<PB2)
// Segment patterns for digits 0 to 9
const uint8_t numbers[] = {
(A | B | C | D | E | F), // 0
@ -33,19 +29,14 @@ const uint8_t numbers[] = {
(A | B | C | F | G) // 9
};
bool decimalpoint[6] = {false, false, false, false, false, false};
// Array to store digits to be displayed
volatile uint8_t digits[] = {0, 0, 0, 0, 0, 0};
// Current digit index
volatile uint8_t currentDigit = 0;
// Function to display a single digit
void displayDigit(uint8_t digit) {
PORTB = numbers[digit];
}
// Function to turn on a specific digit
void turnOnDigit(uint8_t digit) {
decimalpoint[digit] ? PORTB |= DP : PORTB &= ~DP;
switch (digit) {
case 0:
PORTA |= (1<<PA1);
@ -70,48 +61,16 @@ void turnOnDigit(uint8_t digit) {
}
}
// Function to turn off all digits
void turnOffAllDigits() {
PORTA &= ~((1<<PA1) | (1<<PA0));
PORTD &= ~((1<<PD2) | (1<<PD3) | (1<<PD4) | (1<<PD5));
}
// Timer/Counter1 Compare Match A interrupt
ISR(TIMER1_COMPA_vect) {
turnOffAllDigits(); // Turn off all digits
displayDigit(digits[currentDigit]); // Display the current digit
static uint8_t currentDigit = 0;
PORTA &= ~((1<<PA1) | (1<<PA0));
PORTD &= ~((1<<PD2) | (1<<PD3) | (1<<PD4) | (1<<PD5));
PORTB = numbers[digits[currentDigit]];
turnOnDigit(currentDigit); // Turn on the corresponding digit
currentDigit = (currentDigit + 1) % 6; // Move to the next digit
}
// USART RX Complete interrupt
ISR(USART_RXC_vect) {
static uint8_t d = 0;
uint8_t data = UDR; // Read the received data
if(data == 'X') {
d = 0; // Reset currentDigit to start receiving digits
} else if (data >= 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;
@ -128,23 +87,36 @@ int main() {
// Enable Timer/Counter1 Compare Match A interrupt
TIMSK |= (1 << OCIE1A);
// Initialize USART
// Set baud rate to 9600
UBRRH = (BAUD_RATE_DIVISOR >> 8);
UBRRL = BAUD_RATE_DIVISOR;
// Enable receiver and RX complete interrupt
UCSRB |= (1<<TXEN) | (1 << RXEN) | (1 << RXCIE);
// Set frame format: 8 data bits, 1 stop bit
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
usart::init(9600); // Initialize USART with baud rate 9600
// Enable global interrupts
sei();
static FILE mystdout;
fdev_setup_stream(&mystdout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
stdout = &mystdout;
printf("Started");
uint8_t n = 0;
uint8_t d = 0;
usart::printf("Started");
while (1) {
char c = usart::get();
if(c == '\r') {
usart::put('\n');
usart::put('\r');
} else {
usart::put(c);
}
if(c == 'n') {
n = 0;
d = 0;
for(int i = 0; i < 6; i++) {
digits[i] = 0;
decimalpoint[i] = false;
}
}
if(c == '.' && n > 0) {
decimalpoint[n-1] = true;
}
if(c >= '0' && c <= '9' && n < 6) {
digits[n++] = c - '0';
}
}
return 0;