当前位置:网站首页>Qiming cloud sharing: tips on esp32c3 simple IO and serial port

Qiming cloud sharing: tips on esp32c3 simple IO and serial port

2022-06-24 04:40:00 Qiming cloud

1. Specify chip

Pay attention to setting the target chip before compiling Set command :

idf.py set-target esp32c3

Be careful , This will clear and initialize the compilation and configuration before the project ( if there be )

Otherwise, an error will be reported as follows :

Default configuration is ESP32 If you are using ESP32 You don't need this sentence

idf.py set-target esp32c3

2. Burn in the generated file

compile , idf.py -p COM5 flash Burn in

3. Hardware Connect

connection : 18 Foot joint rgb Of a lamp G The segment is green 19 Pick up rgb Of a lamp B paragraph

Displayed in cyan , there 18 You can talk to 19 Exchange , Because the lighting time is consistent with the intensity

4. GPIO initialization

Then the simplest and most important IO Initialization and output

gpio_config_t io_conf; // establish io Configuration structure

io_conf.intr_type = GPIO_INTR_DISABLE; // Disable interrupt

io_conf.mode = GPIO_MODE_OUTPUT; // Set to output mode

io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;

//io Pin mask , Use bitwise operations Move left to the bit that needs to be changed

//#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0/*18*/) | \

//(1ULL<<GPIO_OUTPUT_IO_1/*19*/))

// The macro is to first set 1 The strong conversion type is unsigned long integer, and the left shift or operation makes 18,19 Configured together as output

io_conf.pull_down_en = 0; // Pull down mode enable close

io_conf.pull_up_en = 0; // Pull up mode enable open

gpio_config(&io_conf); //gpio To configure

5. Output control

The output is already configured

gpio_set_level(GPIO_OUTPUT_IO_0, 0); //18 The pin is set to low level

gpio_set_level(GPIO_OUTPUT_IO_1, 1); //19 The pin is set to high level

6. Receive initialization

At this time, the output is ready , Then there is reception

//interrupt of rising edge

io_conf.intr_type = GPIO_INTR_POSEDGE; // Set rising edge interrupt

//bit mask of the pins, use GPIO4/5 here

io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; //io Pin mask , Use bitwise operations Move left to the bit that needs to be changed

//set as input mode

io_conf.mode = GPIO_MODE_INPUT; // Set to input mode

//enable pull-up mode

io_conf.pull_up_en = 1; // Pull up mode enable open

gpio_config(&io_conf);

Receiving includes interrupt receiving and reading IO Voltage reception

Advantages of interrupt reception : Compared with real-time reading

Read IO advantage : Easy to operate

7. Get the input level

Read IO :

Int IO0_level = gpio_get_level( GPIO_INPUT_IO_0); /* The return value is read IO_0 High and low levels of */

Interrupt plus thread :

//change gpio intrrupt type for one pin

gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);

// Change one pin Of Interrupt type

//create a queue to handle gpio event from isr

gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t)); // Create a new queue

//start gpio task

xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL); // Open the thread and run the function gpio_task_example

//install gpio isr service

gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); // For the corresponding GPIO Pin add ISR The handler

//hook isr handler for specific gpio pin

gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0); // For the corresponding GPIO add to ISR The handler

//hook isr handler for specific gpio pin

gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1); // For the corresponding GPIO add to ISR The handler

//remove isr handler for gpio number.

gpio_isr_handler_remove(GPIO_INPUT_IO_0); // Delete the corresponding ISR Program Here, test the corresponding function

//hook isr handler for specific gpio pin again

gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0); // Add again

static void IRAM_ATTR gpio_isr_handler(void* arg)

{

uint32_t gpio_num = (uint32_t) arg;

xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);

}

static void gpio_task_example(void* arg)

{

uint32_t io_num;

for(;;)

{

if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY))

{

printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));

}

}

}

Concrete demo In the downloaded esp-idf\examples\peripherals\gpio\gpio\generic_gpio

原网站

版权声明
本文为[Qiming cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210906203045981v.html