Compare commits
4 Commits
8bffc896a8
...
36b4410cfb
Author | SHA1 | Date |
---|---|---|
jimmy | 36b4410cfb | |
jimmy | ddbeac535d | |
jimmy | 234001628a | |
jimmy | 81d6df812d |
|
@ -0,0 +1 @@
|
||||||
|
bin
|
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "lib/usart"]
|
||||||
|
path = lib/usart
|
||||||
|
url = git@git.technical.kiwi:avr/usart.git
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 05034f925671f06abe15097326b05eceeef0bc1e
|
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:
|
||||||
|
|
98
src/main.cpp
98
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,14 @@ const uint8_t numbers[] = {
|
||||||
(A | B | C | F | G) // 9
|
(A | B | C | F | G) // 9
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool decimalpoint[6] = {false, false, false, false, false, false};
|
||||||
|
|
||||||
// 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, 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
|
// Function to turn on a specific digit
|
||||||
void turnOnDigit(uint8_t digit) {
|
void turnOnDigit(uint8_t digit) {
|
||||||
|
decimalpoint[digit] ? PORTB |= DP : PORTB &= ~DP;
|
||||||
switch (digit) {
|
switch (digit) {
|
||||||
case 0:
|
case 0:
|
||||||
PORTA |= (1<<PA1);
|
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
|
// 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
|
|
||||||
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() {
|
int main() {
|
||||||
// Set PORTB as output for segments
|
// Set PORTB as output for segments
|
||||||
DDRB = 0xFF;
|
DDRB = 0xFF;
|
||||||
|
@ -128,23 +87,36 @@ 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();
|
||||||
|
|
||||||
|
uint8_t n = 0;
|
||||||
static FILE mystdout;
|
uint8_t d = 0;
|
||||||
fdev_setup_stream(&mystdout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
usart::printf("Started");
|
||||||
stdout = &mystdout;
|
|
||||||
printf("Started");
|
|
||||||
while (1) {
|
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;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue