//Nome: Kauana Quintana Fort
//Matrícula: 202110738
#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>
/*Use o circuito anterior, com o dip switch conectado as portas
PB2 a PB0. O temporizador/contador TC0 gera interrupções a cada 1,8 ms. Usando
as interrupções, faça o LED em PB7 piscar na frequência de 0,8 Hz. A cada 1,2 s os
terminais PB[2:0] são lidos e transmitidos pela interface serial USART0 no formato
binário e decimal.*/
// fint = 1/1,8ms = 555,5
//TOP = 111,5 = 112
uint32_t tick = 0, tick_2 = 0;
uint8_t leitura = 0, led = 0;
void configuracao_portas(void) {
DDRB |= (1 << DDB7); // PB7 é saída
PORTB &= ~(1 << PORTB7); // LED está apagado
DDRB &= ~((1 << DDB0) | (1 << DDB1) | (1 << DDB2)); // PB0, PB1 e PB2 são entradas
PORTB |= (1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2); // Resistores de pull-up internos
}
void configuracao_sistema(void) {
// Configura o timer0 para gerar interrupções a cada 1,8 ms
TCCR0A = 0x02; // WGM[1:0]=10 modo CTC
TCCR0B = 0x04; // CS0[2:0]=100 N=256
TIMSK0 = 0x02; // Ativa OCIE0A
OCR0A = 112; // TOP
TCNT0 = 0x00; // Zera o contador
sei(); // Ativa as interrupções
}
ISR(TIMER0_COMPA_vect) {
tick++;
tick_2++;
if (tick == 348) { // 1,25/1,8ms = 694,44/2 = 347.2
led = 1;
tick = 0;
}
if (tick_2 == 667) { //1,2/1,8ms = 666,66
leitura = 1;
tick_2 = 0;
}
}
void transmitir_valores(uint8_t valor) {
USART0_transmite_string_FLASH(PSTR("Valor int: "));
USART0_transmite_int(valor);
USART0_transmite_string_FLASH(PSTR("\nValor binário: "));
for (int i = 2; i >= 0; i--) {
if (valor & (1 << i)) {
USART0_transmite_string_FLASH(PSTR("1"));
} else {
USART0_transmite_string_FLASH(PSTR("0"));
}
}
USART0_transmite_string_FLASH(PSTR("\n\n"));
}
int main(void) {
configuracao_portas();
configuracao_sistema();
USART0_configura();
while (1) {
if (leitura) {
uint8_t leitura_pinos = PINB & 0x07; // Leitura dos pinos PB0, PB1 e PB2
uint8_t valor_int = 0;
switch (leitura_pinos) {
case 0x00: valor_int = 0x07; break;
case 0x01: valor_int = 0x06; break;
case 0x02: valor_int = 0x05; break;
case 0x03: valor_int = 0x04; break;
case 0x04: valor_int = 0x03; break;
case 0x05: valor_int = 0x02; break;
case 0x06: valor_int = 0x01; break;
case 0x07: valor_int = 0x00; break;
}
transmitir_valores(valor_int);
leitura = 0;
}
if (led) {
PORTB ^= (1 << PORTB7); // Toggle do LED
led = 0;
}
}
return 0;
}