/******************************************************************************
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)
/*****************************************************************************/
/******************************************************************************
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[]="Weslley";
uint8_t ra[]="RA:123456";
uint8_t nome_dois[]="Antonio";
uint8_t ra_dois[]="RA:987654";
stdio_init_all();
printf("Microcontroladores - Aula 15\n");
sleep_ms(250);
system_init();
while(1)
{
if((estado == 1) && (timer_display == 0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra);
timer_display = 30;
estado++;
}
else if((estado == 2) && (timer_display == 0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1, 1);
escreve_frase_ram_lcd(nome_dois);
posicao_cursor_lcd(2, 1);
escreve_frase_ram_lcd(ra_dois);
timer_display = 30;
estado++;
if(estado>2)
{
estado = 1;
}
}
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();
}
/******************************************************************************
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)
{
}