Compare commits

...

6 Commits

Author SHA1 Message Date
5cf0bd6626 Working countdown 2024-07-07 08:38:57 +12:00
a137e81c98 Update usart lib 2024-04-20 09:41:03 +12:00
36b4410cfb Ignore bin folder 2024-04-20 09:39:27 +12:00
ddbeac535d Working version 2024-04-20 09:36:44 +12:00
234001628a Add lib folder 2024-04-20 09:36:16 +12:00
81d6df812d Add usart lib 2024-04-20 09:34:50 +12:00
5 changed files with 67 additions and 63 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

Submodule lib/usart added at a20e3d9836

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,19 @@ const uint8_t numbers[] = {
(A | B | C | F | G) // 9
};
bool decimalpoint[6] = {true, false, true, false, true, false}; // Decimal points for each digit
// 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];
}
volatile uint8_t digits[] = {0, 5, 9, 5, 9, 9}; // Initial countdown value: 59:59.9
// Function to turn on a specific digit
void turnOnDigit(uint8_t digit) {
if (decimalpoint[digit]) {
PORTB |= DP;
} else {
PORTB &= ~DP;
}
switch (digit) {
case 0:
PORTA |= (1<<PA1);
@@ -70,54 +66,54 @@ 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++;
void updateDigits() {
if (digits[5] == 0) { // If milliseconds digit reaches 0
digits[5] = 9; // Reset milliseconds digit
if (digits[4] == 0) {
digits[4] = 9;
if (digits[3] == 0) {
digits[3] = 5;
if (digits[2] == 0) {
digits[2] = 9;
if (digits[1] == 0) {
digits[1] = 5;
if (digits[0] > 0) {
digits[0]--;
}
} else {
digits[1]--;
}
} else {
digits[2]--;
}
} else {
digits[3]--;
}
} else {
digits[4]--;
}
} else {
digits[5]--;
}
// 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<<PA0) | (1<<PA1);
DDRD |= (1<<PD2) | (1<<PD3) | (1<<PD4) | (1<<PD5);
DDRD &= ~(1<<PD6);
// Initialize Timer/Counter1 in CTC mode
TCCR1B |= (1 << WGM12);
@@ -128,23 +124,25 @@ 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");
bool pd6_high = false; // Track the state of PD6
usart::printf("Started");
while (1) {
if (PIND & (1<<PD6)) {
if (!pd6_high) { // If PD6 was previously low
//usart::printf("PD6");
updateDigits(); // Update digits every time PD6 goes high
pd6_high = true; // Set the state to high
}
} else {
pd6_high = false; // Reset the state to low when PD6 is low
}
//_delay_us(10); // Add a delay to avoid rapid checking
}
return 0;