/******************************************************************************
* Nome do Arquivo : main.c
*
* Descrição : Sistema demonstrador de estados de chaves e nível de tensão
*
* Ambiente : WOKWI e RP2040 C/C++ SDK
*
* Responsável : Nathan Santana Soares
*
*****************************************************************************/
/******************************************************************************
* HEADER-FILES (Somente os arquivos necessários nesse arquivo)
******************************************************************************/
#include <stdio.h>
#include "pico/stdlib.h"
#include "display_lcd.h"
#include "hardware/timer.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
/*****************************************************************************/
/******************************************************************************
* Variaveis Globais
******************************************************************************/
#define OUT_H 1
#define OUT_L 0
#define OUT 1
#define IN 0
#define DELAY 1000
#define ALARM_NUM 0
#define ALARM_IRQ timer_hardware_alarm_get_irq_num(timer_hw, ALARM_NUM)
#define chave_1 16
#define chave_2 15
#define chave_3 14
#define chave_4 13
#define chave_5 12
#define chave_6 11
#define chave_7 10
#define chave_8 9
#define LED_1 22
#define LED_2 21
#define LED_3 20
#define LED_4 19
#define LED_5 18
#define LED_6 17
uint8_t chaves[8]={chave_1,chave_2,chave_3,chave_4,chave_5,chave_6,chave_7,chave_8};
uint8_t estados_chaves[8]={1,1,1,1,1,1,1,1};
uint8_t leds[6]={LED_1,LED_2,LED_3,LED_4,LED_5,LED_6};
uint8_t trava_muda_nomes=1;
uint8_t trava_muda_estados_chaves_1=1;
uint8_t trava_muda_estados_chaves_2=1;
uint8_t trava_muda_leds=1;
uint8_t trava_muda_adc=1;
uint16_t contagem=3000;
uint8_t sequencia=0;
uint16_t leitura_adc_at=0;
uint16_t leitura_adc_ant=0;
uint16_t porcentagem=0;
uint8_t timer_led_1=0;
uint8_t timer_led_2=0;
uint8_t libera_pisca_led1=0;
uint8_t libera_pisca_led2=0;
uint8_t texto1_chave[17]="CH1:OFF CH2:OFF ";
uint8_t texto2_chave[17]="CH3:OFF CH4:OFF ";
uint8_t texto3_chave[17]="CH5:OFF CH6:OFF ";
uint8_t texto4_chave[17]="CH7:OFF CH8:OFF ";
uint8_t texto_porcentagem[17]="Porcentagem Lida";
/*****************************************************************************/
/******************************************************************************
* Prototipos das funções
******************************************************************************/
void alarm_irq(void);
void button_callback(uint gpio);
void system_init(void);
void chaves_init(void);
void mostra_estados(void);
void mostra_leitura_adc(void);
void muda_leds(void);
uint8_t mostra_nomes(void);
void main(void)
{
stdio_init_all();
system_init();
sequencia = mostra_nomes();
chaves_init();
while(true)
{
leitura_adc_at=adc_read();
porcentagem=leitura_adc_at/41;
if((leitura_adc_at)!=(leitura_adc_ant))
{
if(sequencia==3)
{
trava_muda_adc=1;
}
}
mostra_estados();
mostra_leitura_adc();
muda_leds();
leitura_adc_ant=leitura_adc_at;
sleep_ms(1);
}
}
void system_init(void)
{
for(uint8_t posicao=0;posicao<6;posicao++)
{
gpio_init(leds[posicao]);
gpio_set_dir(leds[posicao],OUT);
gpio_put(leds[posicao],OUT_L);
}
init_lcd();
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq);
uint64_t target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
irq_set_enabled(ALARM_IRQ, true);
}
void chaves_init(void)
{
for(uint8_t posicao=0;posicao<8;posicao++)
{
gpio_init(chaves[posicao]);
gpio_set_dir(chaves[posicao],IN);
gpio_pull_up(chaves[posicao]);
gpio_set_irq_enabled_with_callback(chaves[posicao],GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
}
if(!(gpio_get(chave_1)))
{
texto1_chave[5]='N';
texto1_chave[6]=' ';
estados_chaves[0]=0;
}
else
{
texto1_chave[5]='F';
texto1_chave[6]='F';
estados_chaves[0]=1;
}
if(!(gpio_get(chave_2)))
{
texto1_chave[13]='N';
texto1_chave[14]=' ';
estados_chaves[1]=0;
}
else
{
texto1_chave[13]='F';
texto1_chave[14]='F';
estados_chaves[1]=1;
}
if(!(gpio_get(chave_3)))
{
texto2_chave[5]='N';
texto2_chave[6]=' ';
estados_chaves[2]=0;
}
else
{
texto2_chave[5]='F';
texto2_chave[6]='F';
estados_chaves[2]=1;
}
if(!(gpio_get(chave_4)))
{
texto2_chave[13]='N';
texto2_chave[14]=' ';
estados_chaves[3]=0;
}
else
{
texto2_chave[13]='F';
texto2_chave[14]='F';
estados_chaves[3]=1;
}
if(!(gpio_get(chave_5)))
{
texto3_chave[5]='N';
texto3_chave[6]=' ';
estados_chaves[4]=0;
}
else
{
texto3_chave[5]='F';
texto3_chave[6]='F';
estados_chaves[4]=1;
}
if(!(gpio_get(chave_6)))
{
texto3_chave[13]='N';
texto3_chave[14]=' ';
estados_chaves[5]=0;
}
else
{
texto3_chave[13]='F';
texto3_chave[14]='F';
estados_chaves[5]=1;
}
if(!(gpio_get(chave_7)))
{
texto4_chave[5]='N';
texto4_chave[6]=' ';
estados_chaves[6]=0;
}
else
{
texto4_chave[5]='F';
texto4_chave[6]='F';
estados_chaves[6]=1;
}
if(!(gpio_get(chave_8)))
{
texto4_chave[13]='N';
texto4_chave[14]=' ';
estados_chaves[7]=0;
}
else
{
texto4_chave[13]='F';
texto4_chave[14]='F';
estados_chaves[7]=1;
}
adc_init();
adc_gpio_init(26);
adc_select_input(0);
adc_read();
}
void mostra_estados(void)
{
if((trava_muda_estados_chaves_1)||(trava_muda_estados_chaves_2))
{
if((sequencia==1)&&(trava_muda_estados_chaves_1))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(texto1_chave);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(texto2_chave);
trava_muda_estados_chaves_1=0;
}
else if((sequencia==2)&&(trava_muda_estados_chaves_2))
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(texto3_chave);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(texto4_chave);
trava_muda_estados_chaves_2=0;
}
DesligaCursor();
}
}
void mostra_leitura_adc(void)
{
if(trava_muda_adc)
{
if(sequencia==3)
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(texto_porcentagem);
posicao_cursor_lcd(2,6);
escreve_inteiro_lcd(porcentagem);
}
DesligaCursor();
trava_muda_adc=0;
}
}
void muda_leds(void)
{
if(trava_muda_leds)
{
if((estados_chaves[0])==0)
{
libera_pisca_led1=1;
}
else
{
gpio_put(LED_1, OUT_L);
timer_led_1=0;
libera_pisca_led1=0;
}
if((estados_chaves[1])==0)
{
libera_pisca_led2=1;
}
else
{
gpio_put(LED_2, OUT_L);
timer_led_2=0;
libera_pisca_led2=0;
}
if(((estados_chaves[2])==0)&&((estados_chaves[3])==0))
{
gpio_put(LED_4, OUT_H);
}
else
{
gpio_put(LED_4, OUT_L);
}
if((estados_chaves[4])==1)
{
gpio_put(LED_5, OUT_H);
}
else
{
gpio_put(LED_5, OUT_L);
}
if(((estados_chaves[5])==1)&&((estados_chaves[6])==1)&&((estados_chaves[7])==1))
{
gpio_put(LED_6, OUT_H);
}
else if(((estados_chaves[5])==0)&&((estados_chaves[6])==0)&&((estados_chaves[7])==0))
{
gpio_put(LED_6, OUT_L);
}
trava_muda_leds=0;
}
}
void alarm_irq(void)
{
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
uint64_t target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
contagem--;
if(!sequencia)
{
if(!contagem)
{
trava_muda_nomes=1;
contagem=3000;
}
}
else
{
if(!contagem)
{
if(sequencia==1)
{
sequencia=2;
trava_muda_adc=0;
trava_muda_estados_chaves_1=0;
trava_muda_estados_chaves_2=1;
}
else if(sequencia==2)
{
sequencia=3;
trava_muda_estados_chaves_1=0;
trava_muda_estados_chaves_2=0;
trava_muda_adc=1;
}
else if(sequencia==3)
{
sequencia=1;
trava_muda_adc=0;
trava_muda_estados_chaves_2=0;
trava_muda_estados_chaves_1=1;
}
else
{
sequencia=1;
trava_muda_adc=0;
trava_muda_estados_chaves_2=0;
trava_muda_estados_chaves_1=1;
}
contagem=2000;
}
}
if(libera_pisca_led1)
{
if(timer_led_1)
{
timer_led_1--;
}
else
{
gpio_put(LED_1,!gpio_get(LED_1)); //Tive que colocar aqui porque a resposta do LED estava sendo influenciada pela velocidade de execução do while(true) do main().
timer_led_1=50;
printf("Pisca 1 %d \n",gpio_get(LED_1));
}
}
if(libera_pisca_led2)
{
if(timer_led_2)
{
timer_led_2--;
}
else
{
gpio_put(LED_2, !gpio_get(LED_2));
timer_led_2=50;
printf("Pisca 2 %d \n",gpio_get(LED_2));
}
}
}
void button_callback(uint gpio)
{
if(gpio==chave_1)
{
if(!(gpio_get(chave_1)))
{
texto1_chave[5]='N';
texto1_chave[6]=' ';
estados_chaves[0]=0;
}
else
{
texto1_chave[5]='F';
texto1_chave[6]='F';
estados_chaves[0]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_2)
{
if(!(gpio_get(chave_2)))
{
texto1_chave[13]='N';
texto1_chave[14]=' ';
estados_chaves[1]=0;
}
else
{
texto1_chave[13]='F';
texto1_chave[14]='F';
estados_chaves[1]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_3)
{
if(!(gpio_get(chave_3)))
{
texto2_chave[5]='N';
texto2_chave[6]=' ';
estados_chaves[2]=0;
}
else
{
texto2_chave[5]='F';
texto2_chave[6]='F';
estados_chaves[2]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_4)
{
if(!(gpio_get(chave_4)))
{
texto2_chave[13]='N';
texto2_chave[14]=' ';
estados_chaves[3]=0;
}
else
{
texto2_chave[13]='F';
texto2_chave[14]='F';
estados_chaves[3]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_5)
{
if(!(gpio_get(chave_5)))
{
texto3_chave[5]='N';
texto3_chave[6]=' ';
estados_chaves[4]=0;
}
else
{
texto3_chave[5]='F';
texto3_chave[6]='F';
estados_chaves[4]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_6)
{
if(!(gpio_get(chave_6)))
{
texto3_chave[13]='N';
texto3_chave[14]=' ';
estados_chaves[5]=0;
}
else
{
texto3_chave[13]='F';
texto3_chave[14]='F';
estados_chaves[5]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_7)
{
if(!(gpio_get(chave_7)))
{
texto4_chave[5]='N';
texto4_chave[6]=' ';
estados_chaves[6]=0;
}
else
{
texto4_chave[5]='F';
texto4_chave[6]='F';
estados_chaves[6]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_8)
{
if(!(gpio_get(chave_8)))
{
texto4_chave[13]='N';
texto4_chave[14]=' ';
estados_chaves[7]=0;
}
else
{
texto4_chave[13]='F';
texto4_chave[14]='F';
estados_chaves[7]=1;
}
trava_muda_estados_chaves_2=1;
}
trava_muda_leds=1;
}
uint8_t mostra_nomes(void)
{
uint8_t nome_1[17] = "Carlos E. S. ";
uint8_t nome_2[17] = "Carlos H. C. O. ";
uint8_t nome_3[17] = "Guilherme Akaki";
uint8_t nome_4[17] = "Julio Cesar ";
uint8_t nome_5[17] = "Nathan S. Soares";
uint8_t ra_1[17] = "1600732313022";
uint8_t ra_2[17] = "1600732213005";
uint8_t ra_3[17] = "1600732321042";
uint8_t ra_4[17] = "1600732311020";
uint8_t ra_5[17] = "1600732411020";
uint8_t nome_ra=1;
while(nome_ra<7)
{
if(trava_muda_nomes)
{
LIMPA_DISPLAY();
if (nome_ra==1)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_1);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_1);
}
else if (nome_ra==2)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_2);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_2);
}
else if (nome_ra==3)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_3);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_3);
}
else if (nome_ra==4)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_4);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_4);
}
else if (nome_ra==5)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_5);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_5);
}
trava_muda_nomes=0;
nome_ra++;
DesligaCursor();
}
sleep_ms(50);
}
LIMPA_DISPLAY();
return 1;
}