/******************************************************************************
Nome do Arquivo : main.c
Descrição : Arquivo principal aula 5, exemplo 1
Ambiente : WOKWI e RP2040 C/C++ SDK
Responsável : Weslley M. Torres
Versão/Data : 1.0.0 - 25/03/2024 - Initial version
*****************************************************************************/
/******************************************************************************
HEADER-FILES (Somente os arquivos necessários nesse arquivo)
******************************************************************************/
#include <stdio.h>
#include "pico/stdlib.h"
#include "display_lcd.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
/*****************************************************************************/
/******************************************************************************
Variaveis Globais
******************************************************************************/
uint16_t timer_display = 0;
// Low-level alarm infrastructure we'll be using
#define ALARM_NUM 0
#define ALARM_IRQ TIMER_IRQ_0
#define DELAY 100000 // 1/Fs (in microseconds)
#define btn1 16
#define btn2 15
#define btn3 14
#define btn4 13
#define btn5 12
#define btn6 11
#define btn7 10
#define btn8 9
#define led1 22
#define led2 21
#define led3 20
#define led4 19
#define led5 18
#define led6 17
#define led7 27
#define led8 28
uint8_t estado_btn1 = 0;
uint8_t estado_btn2 = 0;
uint8_t estado_btn3 = 0;
uint8_t estado_btn4 = 0;
uint8_t estado_btn5 = 0;
uint8_t estado_btn6 = 0;
uint8_t estado_btn7 = 0;
uint8_t estado_btn8 = 0;
/*****************************************************************************/
/******************************************************************************
Prototipos das funções
******************************************************************************/
void system_init(void);
void alarm_irq(void);
void button_callback(uint gpio);
/*****************************************************************************/
/******************************************************************************
Funcao: int main(void)
Entrada: Nenhuma (void)
Saída: Nenhuma (void)
Descrição: Função principal
*****************************************************************************/
int main()
{
uint8_t estado = 1;
uint8_t nome[]="RODRIGO";
uint8_t ra[]="RA:13027";
uint8_t nome_dois[]="DENNER ";
uint8_t ra_dois[]="RA:13039";
uint8_t nome_tres[]="LEANDRO";
uint8_t ra_tres[]="RA:13032";
uint8_t nome_quatro[]="MOHAMED";
uint8_t ra_quatro[]="RA:00000";
uint8_t nome_cinco[]="PAULO ARAGAO";
uint8_t ra_cinco[]="RA:00000";
uint8_t btnn[]="1";
uint8_t btnn2[]="2";
uint8_t btnn3[]="3";
uint8_t btnn4[]="4";
uint8_t btnn5[]="5";
uint8_t btnn6[]="6";
uint8_t btnn7[]="7";
uint8_t btnn8[]="8";
uint8_t btn1_off[]="1";
uint8_t btn1_on[]="0";
uint8_t btn2_off[]="1";
uint8_t btn2_on[]="0";
uint8_t btn3_off[]="1";
uint8_t btn3_on[]="0";
uint8_t btn4_off[]="1";
uint8_t btn4_on[]="0";
uint8_t btn5_off[]="1";
uint8_t btn5_on[]="0";
uint8_t btn6_off[]="1";
uint8_t btn6_on[]="0";
uint8_t btn7_off[]="1";
uint8_t btn7_on[]="0";
uint8_t btn8_off[]="1";
uint8_t btn8_on[]="0";
stdio_init_all();
printf("Microcontroladores - Aula 15\n");
sleep_ms(250);
system_init();
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra);
sleep_ms(1000);
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome_dois);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra_dois);
sleep_ms(1000);
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome_tres);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra_tres);
sleep_ms(1000);
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome_quatro);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra_quatro);
sleep_ms(1000);
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome_cinco);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra_cinco);
sleep_ms(1000);
DesligaCursor();
sleep_ms(500);
LIMPA_DISPLAY();
while (1)
{
estado_btn1 = gpio_get(btn1);
estado_btn2 = gpio_get(btn2);
estado_btn3 = gpio_get(btn3);
estado_btn4 = gpio_get(btn4);
estado_btn5 = gpio_get(btn5);
estado_btn6 = gpio_get(btn6);
estado_btn7 = gpio_get(btn7);
estado_btn8 = gpio_get(btn8);
if(estado_btn1 == 0)
{
posicao_cursor_lcd(1, 0);
escreve_frase_ram_lcd(btnn);
posicao_cursor_lcd(2, 0);
escreve_frase_ram_lcd(btn1_off);
gpio_put(led1, 1);
}
else
{
posicao_cursor_lcd(1, 0);
escreve_frase_ram_lcd(btnn);
posicao_cursor_lcd(2, 0);
escreve_frase_ram_lcd(btn1_on);
gpio_put(led1, 0);
}
if(estado_btn2 == 0)
{
posicao_cursor_lcd(1, 2);
escreve_frase_ram_lcd(btnn2);
posicao_cursor_lcd(2, 2);
escreve_frase_ram_lcd(btn2_off);
gpio_put(led2, 1);
}
else
{
posicao_cursor_lcd(1, 2);
escreve_frase_ram_lcd(btnn2);
posicao_cursor_lcd(2, 2);
escreve_frase_ram_lcd(btn2_on);
gpio_put(led2, 0);
}
if(estado_btn3 == 0)
{
posicao_cursor_lcd(1, 4);
escreve_frase_ram_lcd(btnn3);
posicao_cursor_lcd(2, 4);
escreve_frase_ram_lcd(btn3_off);
gpio_put(led3, 1);
}
else
{
posicao_cursor_lcd(1, 4);
escreve_frase_ram_lcd(btnn3);
posicao_cursor_lcd(2, 4);
escreve_frase_ram_lcd(btn3_on);
gpio_put(led3, 0);
}
if(estado_btn4 == 0)
{
posicao_cursor_lcd(1,6);
escreve_frase_ram_lcd(btnn4);
posicao_cursor_lcd(2,6);
escreve_frase_ram_lcd(btn4_off);
gpio_put(led4, 1);
}
else
{
posicao_cursor_lcd(1, 6);
escreve_frase_ram_lcd(btnn4);
posicao_cursor_lcd(2, 6);
escreve_frase_ram_lcd(btn4_on);
gpio_put(led4, 0);
}
if(estado_btn5 == 0)
{
posicao_cursor_lcd(1, 8);
escreve_frase_ram_lcd(btnn5);
posicao_cursor_lcd(2, 8);
escreve_frase_ram_lcd(btn5_off);
gpio_put(led5, 1);
}
else
{
posicao_cursor_lcd(1, 8);
escreve_frase_ram_lcd(btnn5);
posicao_cursor_lcd(2, 8);
escreve_frase_ram_lcd(btn5_on);
gpio_put(led5, 0);
}
if(estado_btn6 == 0)
{
posicao_cursor_lcd(1, 10);
escreve_frase_ram_lcd(btnn6);
posicao_cursor_lcd(2, 10);
escreve_frase_ram_lcd(btn6_off);
gpio_put(led6, 1);
}
else
{
posicao_cursor_lcd(1, 10);
escreve_frase_ram_lcd(btnn6);
posicao_cursor_lcd(2, 10);
escreve_frase_ram_lcd(btn6_on);
gpio_put(led6, 0);
}
if(estado_btn7 == 0)
{
posicao_cursor_lcd(1, 12);
escreve_frase_ram_lcd(btnn7);
posicao_cursor_lcd(2, 12);
escreve_frase_ram_lcd(btn7_off);
gpio_put(led7, 1);
}
else
{
posicao_cursor_lcd(1, 12);
escreve_frase_ram_lcd(btnn7);
posicao_cursor_lcd(2, 12);
escreve_frase_ram_lcd(btn7_on);
gpio_put(led7, 0);
}
if(estado_btn8 == 0)
{
posicao_cursor_lcd(1, 14);
escreve_frase_ram_lcd(btnn8);
posicao_cursor_lcd(2, 14);
escreve_frase_ram_lcd(btn8_off);
gpio_put(led8, 1);
}
else
{
posicao_cursor_lcd(1, 14);
escreve_frase_ram_lcd(btnn8);
posicao_cursor_lcd(2, 14);
escreve_frase_ram_lcd(btn8_on);
gpio_put(led8, 0);
}
sleep_ms(5);
}
}
/******************************************************************************
Funcao: void system_init(void)
Entrada: Nenhuma (void)
Saída: Nenhuma (void)
Descrição: Inicializa sistema (microcontrolador e periféricos)
*****************************************************************************/
void system_init(void)
{
uint64_t target = 0;
printf("Init Hardware ....\n");
sleep_ms(10);
/* o trecho de codigo abaixo realiza a inicializacao da interrupt de timer */
// Enable the interrupt for the alarm (we're using Alarm 0)
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM) ;
// Associate an interrupt handler with the ALARM_IRQ
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq) ;
// Enable the alarm interrupt
irq_set_enabled(ALARM_IRQ, true) ;
target = timer_hw->timerawl + DELAY;
// Write the lower 32 bits of the target time to the alarm register, arming it.
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
/* Fim da inicialização do interrupcao de timer */
init_lcd();
printf("Init Hardware ....\n");
sleep_ms(10);
gpio_init(led1);
gpio_set_dir(led1, OUT);
gpio_init(led2);
gpio_set_dir(led2, OUT);
gpio_init(led3);
gpio_set_dir(led3, OUT);
gpio_init(led4);
gpio_set_dir(led4, OUT);
gpio_init(led5);
gpio_set_dir(led5, OUT);
gpio_init(led6);
gpio_set_dir(led6, OUT);
gpio_init(led7);
gpio_set_dir(led7, OUT);
gpio_init(led8);
gpio_set_dir(led8, OUT);
gpio_init(btn1);
gpio_set_dir(btn1, GPIO_IN);
gpio_pull_up(btn1); // Enable pull-up resistor
gpio_init(btn2);
gpio_set_dir(btn2, GPIO_IN);
gpio_pull_up(btn2); // Enable pull-up resistor
gpio_init(btn3);
gpio_set_dir(btn3, GPIO_IN);
gpio_pull_up(btn3); // Enable pull-up resistor
gpio_init(btn4);
gpio_set_dir(btn4, GPIO_IN);
gpio_pull_up(btn4); // Enable pull-up resistor
gpio_init(btn5);
gpio_set_dir(btn5, GPIO_IN);
gpio_pull_up(btn5); // Enable pull-up resistor
gpio_init(btn6);
gpio_set_dir(btn6, GPIO_IN);
gpio_pull_up(btn6); // Enable pull-up resistor
gpio_init(btn7);
gpio_set_dir(btn7, GPIO_IN);
gpio_pull_up(btn7); // Enable pull-up resistor
gpio_init(btn8);
gpio_set_dir(btn8, GPIO_IN);
gpio_pull_up(btn8); // Enable pull-up resistor
gpio_set_irq_enabled_with_callback(btn1, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn2, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn3, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn4, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn5, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn6, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn7, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(btn8, GPIO_IRQ_EDGE_FALL, true, (gpio_irq_callback_t)&button_callback);
}
/******************************************************************************
Funcao: void alarm_irq(void)
Entrada: Nenhuma (void)
Saída: Nenhuma (void)
Descrição: Função tratamento interrupcao do timer 0
*****************************************************************************/
void alarm_irq(void)
{
static bool toggle = false;
printf("Timer interrupt occurred!\n");
// Clear the alarm irq
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
uint64_t target = timer_hw->timerawl + DELAY;
// Write the lower 32 bits of the target time to the alarm register, arming it.
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
if(timer_display) timer_display--;
}
/******************************************************************************
Funcao: void button_callback(uint gpio)
Entrada: uint gpio - number of gpio
Saída: Nenhuma (void)
Descrição: Função tratamento interrupcao do botao
*****************************************************************************/
void button_callback(uint gpio)
{
if(gpio == btn1)
{
gpio_put(btn1, !gpio_get(btn1)); // Toggle the LED
printf("Button pressed!\n");
}
else if(gpio == btn2)
{
gpio_put(btn2, !gpio_get(btn2)); // Toggle the LED
printf("Button pressed!\n");
}
else if(gpio == btn3)
{
gpio_put(btn3, !gpio_get(btn3));
printf("Button pressed!\n");
}
else if(gpio == btn4)
{
gpio_put(btn4, !gpio_get(btn4));
printf("Button pressed!\n");
}
else if(gpio == btn5)
{
gpio_put(btn5, !gpio_get(btn5)); // Toggle the LED
printf("Button pressed!\n");
}
else if(gpio == btn6)
{
gpio_put(btn6, !gpio_get(btn6)); // Toggle the LED
printf("Button pressed!\n");
}
else if(gpio == btn7)
{
gpio_put(btn7, !gpio_get(btn7));
printf("Button pressed!\n");
}
else if(gpio == btn8)
{
gpio_put(btn8, !gpio_get(btn8));
printf("Button pressed!\n");
}
}