Compare commits
6 Commits
8bffc896a8
...
main
Author | SHA1 | Date | |
---|---|---|---|
5cf0bd6626 | |||
a137e81c98 | |||
36b4410cfb | |||
ddbeac535d | |||
234001628a | |||
81d6df812d |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
bin
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "lib/usart"]
|
||||||
|
path = lib/usart
|
||||||
|
url = git@git.technical.kiwi:avr/usart.git
|
1
lib/usart
Submodule
1
lib/usart
Submodule
Submodule lib/usart added at a20e3d9836
5
makefile
5
makefile
@@ -7,13 +7,14 @@ OBJCOPY=avr-objcopy
|
|||||||
CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11
|
CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11
|
||||||
|
|
||||||
SRC=$(wildcard src/*.cpp)
|
SRC=$(wildcard src/*.cpp)
|
||||||
|
LIB=$(wildcard lib/*/src/*.cpp)
|
||||||
|
|
||||||
default: clean build flash
|
default: clean build flash
|
||||||
|
|
||||||
build:
|
build:
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
${CC} ${CFLAGS} -o bin/main.bin ${SRC} -w
|
${CC} ${CFLAGS} -o bin/main.bin ${SRC} ${LIB} -w
|
||||||
${CC} ${CFLAGS} -o bin/main.elf ${SRC} -w
|
${CC} ${CFLAGS} -o bin/main.elf ${SRC} ${LIB} -w
|
||||||
${OBJCOPY} -j .text -j .data -O ihex bin/main.bin bin/main.hex
|
${OBJCOPY} -j .text -j .data -O ihex bin/main.bin bin/main.hex
|
||||||
|
|
||||||
flash:
|
flash:
|
||||||
|
120
src/main.cpp
120
src/main.cpp
@@ -1,12 +1,10 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.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 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 segments for each digit
|
||||||
#define A (1<<PB5)
|
#define A (1<<PB5)
|
||||||
#define B (1<<PB7)
|
#define B (1<<PB7)
|
||||||
@@ -17,8 +15,6 @@
|
|||||||
#define G (1<<PB0)
|
#define G (1<<PB0)
|
||||||
#define DP (1<<PB2)
|
#define DP (1<<PB2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Segment patterns for digits 0 to 9
|
// Segment patterns for digits 0 to 9
|
||||||
const uint8_t numbers[] = {
|
const uint8_t numbers[] = {
|
||||||
(A | B | C | D | E | F), // 0
|
(A | B | C | D | E | F), // 0
|
||||||
@@ -33,19 +29,19 @@ const uint8_t numbers[] = {
|
|||||||
(A | B | C | F | G) // 9
|
(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
|
// Array to store digits to be displayed
|
||||||
volatile uint8_t digits[] = {0, 0, 0, 0, 0, 0};
|
volatile uint8_t digits[] = {0, 5, 9, 5, 9, 9}; // Initial countdown value: 59:59.9
|
||||||
|
|
||||||
// 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
|
// Function to turn on a specific digit
|
||||||
void turnOnDigit(uint8_t digit) {
|
void turnOnDigit(uint8_t digit) {
|
||||||
|
if (decimalpoint[digit]) {
|
||||||
|
PORTB |= DP;
|
||||||
|
} else {
|
||||||
|
PORTB &= ~DP;
|
||||||
|
}
|
||||||
|
|
||||||
switch (digit) {
|
switch (digit) {
|
||||||
case 0:
|
case 0:
|
||||||
PORTA |= (1<<PA1);
|
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
|
// Timer/Counter1 Compare Match A interrupt
|
||||||
ISR(TIMER1_COMPA_vect) {
|
ISR(TIMER1_COMPA_vect) {
|
||||||
turnOffAllDigits(); // Turn off all digits
|
static uint8_t currentDigit = 0;
|
||||||
displayDigit(digits[currentDigit]); // Display the current digit
|
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
|
turnOnDigit(currentDigit); // Turn on the corresponding digit
|
||||||
currentDigit = (currentDigit + 1) % 6; // Move to the next digit
|
currentDigit = (currentDigit + 1) % 6; // Move to the next digit
|
||||||
}
|
}
|
||||||
|
|
||||||
// USART RX Complete interrupt
|
void updateDigits() {
|
||||||
ISR(USART_RXC_vect) {
|
if (digits[5] == 0) { // If milliseconds digit reaches 0
|
||||||
static uint8_t d = 0;
|
digits[5] = 9; // Reset milliseconds digit
|
||||||
uint8_t data = UDR; // Read the received data
|
if (digits[4] == 0) {
|
||||||
|
digits[4] = 9;
|
||||||
if(data == 'X') {
|
if (digits[3] == 0) {
|
||||||
d = 0; // Reset currentDigit to start receiving digits
|
digits[3] = 5;
|
||||||
} else if (data >= 0 && data <= 9 && currentDigit < 6) {
|
if (digits[2] == 0) {
|
||||||
digits[d] = data;
|
digits[2] = 9;
|
||||||
d++;
|
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() {
|
int main() {
|
||||||
// Set PORTB as output for segments
|
// Set PORTB as output for segments
|
||||||
DDRB = 0xFF;
|
DDRB = 0xFF;
|
||||||
// Set PORTA and PORTD as output for digit control
|
// Set PORTA and PORTD as output for digit control
|
||||||
DDRA |= (1<<PA0) | (1<<PA1);
|
DDRA |= (1<<PA0) | (1<<PA1);
|
||||||
DDRD |= (1<<PD2) | (1<<PD3) | (1<<PD4) | (1<<PD5);
|
DDRD |= (1<<PD2) | (1<<PD3) | (1<<PD4) | (1<<PD5);
|
||||||
|
DDRD &= ~(1<<PD6);
|
||||||
|
|
||||||
// Initialize Timer/Counter1 in CTC mode
|
// Initialize Timer/Counter1 in CTC mode
|
||||||
TCCR1B |= (1 << WGM12);
|
TCCR1B |= (1 << WGM12);
|
||||||
@@ -128,23 +124,25 @@ int main() {
|
|||||||
// Enable Timer/Counter1 Compare Match A interrupt
|
// Enable Timer/Counter1 Compare Match A interrupt
|
||||||
TIMSK |= (1 << OCIE1A);
|
TIMSK |= (1 << OCIE1A);
|
||||||
|
|
||||||
// Initialize USART
|
usart::init(9600); // Initialize USART with baud rate 9600
|
||||||
// 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);
|
|
||||||
// Enable global interrupts
|
// Enable global interrupts
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
|
bool pd6_high = false; // Track the state of PD6
|
||||||
static FILE mystdout;
|
usart::printf("Started");
|
||||||
fdev_setup_stream(&mystdout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
|
||||||
stdout = &mystdout;
|
|
||||||
printf("Started");
|
|
||||||
while (1) {
|
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;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user