WiFive wrote:
I tried the example in gpio.h with gpio0 and get the interrupt messages over uart when the button on devkitc is pressed.
where is the code?
@Neil
example you connect a PIR to the GPIO17
you build an alarm
https://www.mpja.com/download/31227sc.pdf
The PIR i linked has 3.3 / 0 TTL
with 5 V supply
because Nano32 has 5.0 V output too,
i connect 5 V to Power
ground to ground
TTL to GPIO 17
( bevore i test with push button )
here is a example with PIR and the Nano32 form MakerAsia & GravitechThai
- IMG_5453.JPG (1019.33 KiB) Viewed 20996 times
an here the code for you.
if you use in your book
please named it
credit: MakerAsia
thank you
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
//
#define INPUT_GPIO 17
// #define INPUT_GPIO_DOORBUTTON 18
// #define INPUT_GPIO_ALARMBUTTON 19
#define TAG 3
void gpioCallback (void* arg);
/******************************************************************************
* FunctionName : hello_task
* Description :
* Parameters : void* pvParameter
* Returns :
*******************************************************************************/
void hello_task(void* pvParameter)
{
/* Calc */
printf("Hello, how are you?!\n");
for (int i = 10; i >= 0; i--) {
printf("Restarting in %d seconds...\n", i);
vTaskDelay(1000 / portTICK_RATE_MS);
}
printf("Restarting now.\n");
fflush(stdout);
system_restart();
}
/******************************************************************************
* FunctionName : main_task
* Description :
* Parameters : void* pvParameter
* Returns : void
*******************************************************************************/
void main_task(void* pvParameter)
{
/* Calc */
/* Configure the IOMUX register for pad BLINK_GPIO (some pads are
muxed to GPIO on reset already, but some default to other
functions and need to be switched to GPIO. Consult the
Technical Reference for a list of pads and their default
functions.)
*/
gpio_pad_select_gpio(INPUT_GPIO);
/* Set the GPIO as a input */
gpio_set_direction(INPUT_GPIO, GPIO_MODE_INPUT);
/* Set the GPIO pull */
gpio_set_pull_mode(INPUT_GPIO, GPIO_PULLUP_ONLY);
// gpio_set_intr_type(INPUT_GPIO, GPIO_INTR_NEGEDGE);
// gpio_set_intr_type(INPUT_GPIO, GPIO_INTR_ANYEDGE);
gpio_set_intr_type(INPUT_GPIO, GPIO_INTR_ANYEDGE);
gpio_intr_enable(INPUT_GPIO);
// Intterrupt number see below
gpio_isr_register(INPUT_GPIO, gpioCallback, (void *)TAG); // 17
while(1) {
// if you need..
// printf( "Loop...\n" );
// vTaskDelay(1000 / portTICK_RATE_MS);
}
}
/******************************************************************************
* FunctionName : gpioCallback
* Description :
* Parameters : void* arg
* Returns :
*******************************************************************************/
void gpioCallback(void* arg)
{
/* Calc */
uint32_t gpio_num = 0;
uint32_t gpio_intr_status = READ_PERI_REG(GPIO_STATUS_REG); //read status to get interrupt status for GPIO0-31
uint32_t gpio_intr_status_h = READ_PERI_REG(GPIO_STATUS1_REG);//read status1 to get interrupt status for GPIO32-39
SET_PERI_REG_MASK(GPIO_STATUS_W1TC_REG, gpio_intr_status); //Clear intr for gpio0-gpio31
SET_PERI_REG_MASK(GPIO_STATUS1_W1TC_REG, gpio_intr_status_h); //Clear intr for gpio32-39
do {
if(gpio_num < 32) {
if(gpio_intr_status & BIT(gpio_num)) { //gpio0-gpio31
ets_printf("1 Intr GPIO%d ,val: %d\n",gpio_num,gpio_get_level(gpio_num));
//This is an isr handler, you should post an event to process it in RTOS queue.
/ * test your self......
switch (gpio_intr_status & BIT(gpio_num)) {
case 17: ets_print("GPIO 17 request\n"); break;
case 18: ets_print("GPIO 18 request : task2 call\n");
xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
break;
}
* /
}
// }
} else {
if(gpio_intr_status_h & BIT(gpio_num - 32)) {
ets_printf("2 Intr GPIO%d, val : %d\n",gpio_num,gpio_get_level(gpio_num));
//This is an isr handler, you should post an event to process it in RTOS queue.
}
}
} while(++gpio_num < GPIO_PIN_COUNT);
/* push_status = ; */
}
/******************************************************************************
* * * * * * * *: main of ESP32
* FunctionName : app_main
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void app_main()
{
/* Calc */
nvs_flash_init();
system_init();
// xTaskCreate(&hello_task, "hello_task", 2048, NULL, 5, NULL);
xTaskCreate(&main_task, "main_task", 2048, NULL, 5, NULL);
}
- output.jpg (67.67 KiB) Viewed 20996 times