MIKR/lectures/Timer/7-seg-display_timer+pin-debounce_interrupts.c

79 lines
1.9 KiB
C
Raw Permalink Normal View History

2024-04-16 20:18:18 +02:00
#include <avr/io.h>
#include <avr/interrupt.h>
volatile uint8_t button_reset_counter;
volatile uint8_t main_count;
volatile uint8_t current_segment = 0;
const uint8_t digits[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
const uint8_t segments[] = {0x0E, 0x0D, 0x0B, 0x07};
// decrease reset_counter until 0
ISR(TIMER2_OVF_vect)
{
PORTC &= 0xF0;
PORTC |= segments[current_segment];
int count_disp[4];
int count_copy = main_count;
for(int i = 0; i < 4; i++){
count_disp[i] = count_copy % 10;
count_copy /= 10;
}
PORTA = digits[count_disp[current_segment]];
current_segment = (current_segment + 1) % 4;
button_reset_counter -= button_reset_counter == 0 ? 0 : 1;
}
// update press_counter on interrupt if reset == 0
ISR(PCINT2_vect){
if (button_reset_counter == 0){
main_count++;
button_reset_counter = 255;
}
}
int main(void) {
DDRA = 0xFF;
// C7 as input for external interrupt
DDRC &= ~_BV(DDC7);
PORTC |= _BV(PORTC7);
// C0:3 as segment out
DDRC = 0x0F;
// TIMER 2 setup
TCCR2B |= _BV(CS22);// | _BV(CS21) | _BV(CS20);
TIMSK2 |= _BV(TOIE2);
cli();
// rising edge interrupt on INT2
EICRA |= _BV(ISC20);
EIMSK |= _BV(INT2);
// enable external interrupt for pin 23 - PC7
PCMSK2 |= _BV(PCINT23);
PCICR |= _BV(PCIE2);
sei();
while(1) {
// if(przyciskZablokowany1 && (PIND & (1<<PD2))) przyciskZablokowany1++;
// if(!przyciskZablokowany2 && !(PIND & (1<<PD1))) {
// if(dioda2 == 0)
// dioda2 = 1;
// else dioda2 = 0;
// przyciskZablokowany2 = 1;
// }
// else if(przyciskZablokowany2 && (PIND & (1<<PD1))) przyciskZablokowany2++;
// if(!przyciskZablokowany3 && !(PIND & (1<<PD0))) {
// liczba = (liczba + 1) % 10;
// przyciskZablokowany3 = 1;
// }
// else if(przyciskZablokowany3 && (PIND & (1<<PD0))) przyciskZablokowany3++;
}
return 0;
}