#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 led1 22
#define led2 21
uint8_t estado_btn1 = 0;
uint8_t estado_btn2 = 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 = 0;
uint8_t nome[]="teste";
uint8_t ra[]="RA:13027";
uint8_t btna[]="A";
uint8_t btnn2[]= "B";
uint8_t chave1_on[]="1";
uint8_t chave1_off[]="0";
uint8_t chave2_on[]="1";
uint8_t chave2_off[]="0";
stdio_init_all();
sleep_ms(250);
system_init();
posicao_cursor_lcd(1, 5);
escreve_frase_ram_lcd(nome);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra);
sleep_ms(1000);
DesligaCursor();
sleep_ms(500);
LIMPA_DISPLAY();
while (1)
{
estado_btn1 = gpio_get(btn1);
estado_btn2 = gpio_get(btn2);
if(estado_btn1 == 0)
{
posicao_cursor_lcd(1, 0);
escreve_frase_ram_lcd(btna);
posicao_cursor_lcd(2, 0);
escreve_frase_ram_lcd(chave1_on);
gpio_put(led1, 1);
}
else
{
posicao_cursor_lcd(1, 0);
escreve_frase_ram_lcd(btna);
posicao_cursor_lcd(2, 0);
escreve_frase_ram_lcd(chave1_off);
gpio_put(led1, 0);
}
if( estado_btn2 == 0)
{
posicao_cursor_lcd(1,3);
escreve_frase_ram_lcd( btnn2);
posicao_cursor_lcd(2,3);
escreve_frase_ram_lcd(chave2_on);
gpio_put(led2, 1);
}
else
{
posicao_cursor_lcd(1,3);
escreve_frase_ram_lcd(btnn2);
posicao_cursor_lcd(2,3);
escreve_frase_ram_lcd(chave2_off);
}
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(btn1);
gpio_set_dir(btn1, GPIO_IN);
gpio_pull_up(btn1); // Enable pull-up resistor
gpio_init(led2);
gpio_set_dir(led2, OUT);
gpio_init(btn2);
gpio_set_dir(btn2, GPIO_IN);
gpio_pull_up(btn2);
gpio_set_irq_enabled_with_callback(btn1, 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");
}
}