/**
FREQ 500Hz - 2ms
TOP=124
N=256
*/
#include <stdint.h> /* usamos os tipos uint8_t e uint16_t */
#define __AVR_ATmega2560__ /* programa para */
#include <avr/interrupt.h> /* usamos interrupções */
#include <avr/io.h> /* definições das entradas e saídas */
volatile uint16_t tick; /**< incrementada a cada interrupção do timer0. */
ISR(TIMER0_COMPA_vect)
{
tick++; // variável tick é incrementada a cada 2 ms
if((PINB&(1<<PINB0)==0x01)&&(PINB&(1<<PINB3)==0x08)){
PORTB = PORTB &(~(1 << PORTB2));
}else{
PORTB = PORTB |(1 << PORTB2);//LED DESLIGADO
}
if (tick >= 125) {
PORTB = PORTB ^ (1 << PORTB7); //inverte o led a cada 1s
tick = 0;
}
}
void configura_sistema(void)
{
TCCR0A = 0x02; // saídas OC0A e OC0B como portas normais. COM0A[1:0]=00 COM0B[1:0]=00 WGM0[1:0]=010 (modo 2-CTC)
TCCR0B = 0x04; // N=256. FOC0A = 0 FOC0B = 0 00 WGM0[2:2]=0 CS0[2:0] = 100
TIMSK0 = 0x02; // interrupções ocorrem no overflow do contador. OCIE0B = 0 OCIE0A = 1 TOIE0 = 0
OCR0A = 0x7C; // TOP = 124
TCNT0 = 0x00; // zeramos o contador
sei(); // habilita as interrupções globalmente
DDRB = DDRB | (1 << DDB7); // OUT
DDRB = DDRB | (1 << DDB2); // OUT
DDRB = DDRB & (~(1 << DDB0)); // IN
DDRB = DDRB & (~(1 << DDB3)); // IN
PORTB = PORTB |(1 << PORTB3);//pull-up
PORTB = PORTB |(1 << PORTB0);//pull-up
PORTB = PORTB & (~(1 << PORTB7));//LED DESLIGADO
PORTB = PORTB |(1 << PORTB2);//LED DESLIGADO
}
int main(void)
{
configura_sistema(); // configuramos o timer0 e o terminal PB7
while (1) // laço infinito
;
return 0;
}