Working countdown

This commit is contained in:
2024-07-07 08:38:57 +12:00
parent a137e81c98
commit 5cf0bd6626

View File

@@ -29,14 +29,19 @@ 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}; 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
// 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; if (decimalpoint[digit]) {
PORTB |= DP;
} else {
PORTB &= ~DP;
}
switch (digit) { switch (digit) {
case 0: case 0:
PORTA |= (1<<PA1); PORTA |= (1<<PA1);
@@ -71,12 +76,44 @@ ISR(TIMER1_COMPA_vect) {
currentDigit = (currentDigit + 1) % 6; // Move to the next digit currentDigit = (currentDigit + 1) % 6; // Move to the next digit
} }
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]--;
}
}
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);
@@ -92,31 +129,20 @@ int main() {
// Enable global interrupts // Enable global interrupts
sei(); sei();
uint8_t n = 0; bool pd6_high = false; // Track the state of PD6
uint8_t d = 0;
usart::printf("Started"); usart::printf("Started");
while (1) { while (1) {
char c = usart::get(); if (PIND & (1<<PD6)) {
if(c == '\r') { if (!pd6_high) { // If PD6 was previously low
usart::put('\n'); //usart::printf("PD6");
usart::put('\r'); updateDigits(); // Update digits every time PD6 goes high
} else { pd6_high = true; // Set the state to high
usart::put(c);
}
if(c == 'n') {
n = 0;
d = 0;
for(int i = 0; i < 6; i++) {
digits[i] = 0;
decimalpoint[i] = false;
} }
} else {
pd6_high = false; // Reset the state to low when PD6 is low
} }
if(c == '.' && n > 0) {
decimalpoint[n-1] = true; //_delay_us(10); // Add a delay to avoid rapid checking
}
if(c >= '0' && c <= '9' && n < 6) {
digits[n++] = c - '0';
}
} }
return 0; return 0;