/*Interrupções são geradas a cada 1,6 ms pelo temporizador/contador
TC0. Em cada interrupção, um LED em PB2 (terminal 51) será acesso se somente
uma entrada, ou PB0 (terminal 53) ou PB3 (terminal 50), for igual a “1”. Escreva
o programa em C. Faça a simulação com o programa em https://wokwi.com. Use
chaves tipo push button nas entradas PB0 e PB3.*/
//Bibliotecas:
#define __ATmega2560__
#define __AVR_ATmega2560__
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <stdint.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdbool.h>
#include <stdint.h>
#include <util/delay.h>
#include <stdint.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#define ch0 PB0
#define ch1 PB3
#define led PB2
volatile int b0, b1;
int main(void){
configuracao_TC0();
DDRB = DDRB | (1 << led); // Out led
DDRB = DDRB & (~(1 << ch0)); // In chave
PORTB = PORTB | (1 << ch0); //Pull-up na chave
DDRB = DDRB & (~(1 << ch1)); // In chave
PORTB = PORTB | (1 << ch1); //Pull-up na chave
while(1){
}
}
void configuracao_TC0() {
// Configura o timer0 para gerar interrupções a cada 1,6 ms
TCCR0A = 0x02; // Modo CTC
TCCR0B = 0x04; // CS0[2:0]=100 N=256
TIMSK0 = 0x02; // Ativa OCIE0A (interrupção de comparação A)
OCR0A = 99; // TOP (valor de comparação)
TCNT0 = 0x00; // Zera o contador
sei(); // Ativa as interrupções globais
}
ISR(TIMER0_COMPA_vect) {
b0 = !(PINB & (1 << ch0));
b1 = !(PINB & (1 << ch1));
if (b0 || b1){
PORTB |= (1 << led);
}
else {
PORTB &= ~(1 << led);
}
}