#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>
// fint = 1/1,8ms = 555,5
//TOP = 111,5 = 112
volatile uint32_t tick = 0, tick_2=0;
uint8_t chave1_atual, chave0_atual;
int ler_pinos = 0;
void configura_sistema(void)
{
// Configura o timer0 para gerar interrupções a cada 1,8 ms
TCCR0A = (1 << WGM01); // Modo CTC
TCCR0B = (1 << CS01) | (1 << CS00); // Prescaler de 64
OCR0A = 112; //
TIMSK0 = (1 << OCIE0A); // Habilita interrupção de comparação
TCNT0 = 0; // Zera o contador
DDRB |= (1 << DDB7); //saida LED
PORTB &= ~(1 << PORTB7); //desligado ao iniciar
DDRB &= ~(1 << DDB0); //entrada chave
PORTB |= (1 << PORTB0); //Pull-Up
DDRB &= ~(1 << DDB2); //entrada chave
PORTB |= (1 << PORTB2); //Pull-Up
sei(); // Habilita interrupções globalmente
}
ISR(TIMER0_COMPA_vect)
{
tick++;
tick_2++;
if (tick == 695) // 1,25/1,8ms = 694,44
{
PORTB = PORTB ^ (1 << PORTB7);
tick = 0;
}
if(tick_2 == 667) {
ler_pinos = 1;
}
}
int main(void)
{
USART0_configura();
configura_sistema(); // Configura o timer0 e as portas
while (1) // Laço infinito que será executado
{
if(ler_pinos){
chave1_atual = PINB & (1<<PINB0);
chave0_atual = PINB & (1<<PINB2);
USART0_transmite(chave1_atual);
USART0_transmite(chave0_atual);
USART0_transmite_int(chave1_atual);
USART0_transmite_int(chave0_atual);
}
}
}