/******************************************************************************
Nome do Arquivo : main.c
Descrição : Arquivo principal aula 11, exemplo 1
Ambiente : WOKWI e RP2040 C/C++ SDK
Responsável : Weslley M. Torres
Versão/Data : 1.0.0 - 07/05/2024 - Initial version
*****************************************************************************/
/******************************************************************************
HEADER-FILES (Somente os arquivos necessários nesse arquivo)
******************************************************************************/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
/*****************************************************************************/
/******************************************************************************
Variaveis Globais
******************************************************************************/
#define OUT_H 1
#define OUT_L 0
#define LED_BLUE 28
#define LED_RED 27
#define OUT 1
#define IN 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() ;
/*****************************************************************************/
/******************************************************************************
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;
static bool toggle_RED = true;
static uint8_t contador = 0;
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;
// Toggle an LED or do some other processing
// For example, toggle an LED on GPIO 28
contador ++;
printf("Valor do contador =%d \n", contador);
if(contador == 2);
{
printf("Altera estado LED ESQUERDA \n");
contador = 0;
gpio_put(LED_BLUE, toggle);
gpio_put(LED_RED, toggle_RED);
toggle = !toggle;
toggle_RED = !toggle_RED;
}
}
/******************************************************************************
Funcao: int main(void)
Entrada: Nenhuma (void)
Saída: Nenhuma (void)
Descrição: Função principal
*****************************************************************************/
int main()
{
stdio_init_all();
system_init();
printf("Hello, Timer!\n");
sleep_ms(250);
while(true)
{
}
}
/******************************************************************************
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;
gpio_init(LED_BLUE);
gpio_init(LED_RED);
gpio_set_dir(LED_BLUE, OUT);
gpio_set_dir(LED_RED,OUT);
// 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;
}