Working countdown

This commit is contained in:
Jimmy 2024-07-07 08:38:57 +12:00
parent a137e81c98
commit 5cf0bd6626
1 changed files with 50 additions and 24 deletions

View File

@ -29,14 +29,19 @@ const uint8_t numbers[] = {
(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
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
void turnOnDigit(uint8_t digit) {
decimalpoint[digit] ? PORTB |= DP : PORTB &= ~DP;
if (decimalpoint[digit]) {
PORTB |= DP;
} else {
PORTB &= ~DP;
}
switch (digit) {
case 0:
PORTA |= (1<<PA1);
@ -71,12 +76,44 @@ ISR(TIMER1_COMPA_vect) {
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() {
// 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);
@ -92,31 +129,20 @@ int main() {
// Enable global interrupts
sei();
uint8_t n = 0;
uint8_t d = 0;
bool pd6_high = false; // Track the state of PD6
usart::printf("Started");
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 (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
}
if(c == '.' && n > 0) {
decimalpoint[n-1] = true;
}
if(c >= '0' && c <= '9' && n < 6) {
digits[n++] = c - '0';
}
//_delay_us(10); // Add a delay to avoid rapid checking
}
return 0;