aboutsummaryrefslogtreecommitdiff
path: root/src/display.c
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2026-02-05 13:05:52 +0530
committerAkshay Nair <phenax5@gmail.com>2026-02-06 15:35:15 +0530
commitd544925cf75a05b681cadfc11b7d8a2d98de6679 (patch)
tree5f563d5c3ac368ff7893cfd90c256afbf4b2d5e2 /src/display.c
parenta40d8f331ba5b7ea239b35714eaa1cff504a086b (diff)
downloaddaft-watch-d544925cf75a05b681cadfc11b7d8a2d98de6679.tar.gz
daft-watch-d544925cf75a05b681cadfc11b7d8a2d98de6679.zip
Use timer interrupt for writing to display
Diffstat (limited to 'src/display.c')
-rw-r--r--src/display.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/src/display.c b/src/display.c
index 5f2d7f3..5fccd50 100644
--- a/src/display.c
+++ b/src/display.c
@@ -1,3 +1,4 @@
+#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>
@@ -12,30 +13,63 @@ const uint32_t digit_masks[] = {
0b00010010, 0b00000010, 0b01111000, 0b00000000, 0b00010000,
};
+void setup_display() {
+ TIMSK1 = 0;
+
+ // Set prescaler (Timer clock = F_CPU / 64)
+ // For 32768Hz -> 32768/64 = 512Hz
+ TCCR1B = (1 << CS11) | (1 << CS10) | (1 << WGM12) | (1 << WGM13);
+ TCCR1A = (1 << WGM11) | (1 << WGM10);
+
+ TCNT1 = 0; // Start counting from 0
+ OCR1A = 1; // 0-1 = 2 counts before timer interrupt (512/2 = 256Hz)
+ OCR1B = 0;
+
+ TIFR1 = (1 << OCF1A) | (1 << OCF1B) | (1 << TOV1); // Clear interrupt flags
+ TIMSK1 = (1 << OCIE1A); // Enable timer overflow interrupt
+
+ // Draw startup state
+ DDRA |= 0b11111111;
+ DDRB |= 0b11111111;
+ PORTA = (1 << PA7);
+ PORTB = (1 << PB2);
+}
+
void write_digit(uint8_t digit) {
if (digit > 9 || digit < 0)
return;
PORTA = digit_masks[digit];
}
-void write_time(uint16_t hour, uint16_t minute) {
+void write_time(uint16_t hour, uint16_t minute, uint8_t mask) {
uint8_t minute_digit1 = minute % 10;
- uint8_t minute_digit2 = minute / 10;
+ uint8_t minute_digit2 = (minute / 10) % 10;
// unsigned short hour_digit1 = hour % 10;
- // unsigned short hour_digit2 = hour / 10;
+ // unsigned short hour_digit2 = (hour / 10) % 10;
+
+ // _delay_ms(4);
+ // Reset digit control pins
+ PORTA &= ~(1 << PA7); // minute digit 1
+ PORTB &= ~(1 << PB2); // minute digit 2
+ // _delay_ms(4);
+ // cli();
+ if (mask & 0b0001 || mask & 0b0100) {
#ifdef STUBBED
- printf("\r:: %d %d", minute_digit2, minute_digit1);
- fflush(stdout);
+ printf(":: %d\n", minute_digit1);
+ fflush(stdout);
#endif
+ write_digit(minute_digit1);
+ PORTA |= (1 << PA7);
+ }
- write_digit(minute_digit1);
- PORTA |= (1 << PA7);
- _delay_ms(10);
- PORTA &= ~(1 << PA7);
-
- write_digit(minute_digit2);
- PORTB |= (1 << PB2);
- _delay_ms(10);
- PORTB &= ~(1 << PB2);
+ if (mask & 0b0010 || mask & 0b1000) {
+#ifdef STUBBED
+ printf(":: %d\n", minute_digit2);
+ fflush(stdout);
+#endif
+ write_digit(minute_digit2);
+ PORTB |= (1 << PB2);
+ }
+ // sei();
}