7752 views|38 replies

6841

Posts

11

Resources
The OP
 

[National Technology N32WB031_STB Development Board Review] GPIO Buttons and LED Lights [Copy link]

 

[National Technology N32WB031_STB Development Board Evaluation] Create MDK Template

1. Based on this creation, copy this template and rename it to N32WB_KEY. Create two new folders, key and led, under the bsp folder, and add key.c, key.h, led.c, and led.h. The directory is as follows:

├─key
│ key.c
│ key.h
├─led
│ led.c
│ led.h

2. Open the project with mdk, create bsp/LED\bsp/key groups, and add .c\.h to the project:

3. The led.c program is as follows:

#include "led.h"


/**
 * [url=home.php?mod=space&uid=159083]@brief[/url] Configures LED GPIO.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_InitType GPIO_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
    }
    else
    {
        return;
    }

    /* Configure the GPIO pin */
    if (Pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin = Pin;
        GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }
}

/**
 * @brief  Turns selected Led on.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
void LedOn(GPIO_Module *GPIOx, uint16_t Pin)
{
    GPIO_SetBits(GPIOx, Pin);
}

/**
 * @brief  Turns selected Led Off.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_ResetBits(GPIOx, Pin);
}

/**
 * @brief  Toggles the selected Led.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_TogglePin(GPIOx, Pin);
}

led.h:

#ifndef __LED_H__
#define __LED_H__
#include "n32wb03x.h"
void LedInit(GPIO_Module* GPIOx, uint16_t Pin);
void LedOn(GPIO_Module *GPIOx, uint16_t Pin);
void LedOff(GPIO_Module* GPIOx, uint16_t Pin);
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin);

#endif

key.c:

#include "key.h"
#include "led.h"
#include "main.h"

/**
 * @brief  Configures key port.
 * @param GPIOx x can be A to G to select the GPIO port.
 * @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
 */
void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_InitType GPIO_InitStructure;
    EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
        RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else
    {
        return;
    }

    /*Configure the GPIO pin as input floating*/
    if (Pin <= GPIO_PIN_ALL)
    {
        GPIO_InitStruct(&GPIO_InitStructure);
        GPIO_InitStructure.Pin          = Pin;
        GPIO_InitStructure.GPIO_Pull    = GPIO_PULL_UP;
        GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
    }

    /*Configure key EXTI Line to key input Pin*/
    GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);

    /*Configure key EXTI line*/
    EXTI_InitStructure.EXTI_Line    = KEY_INPUT_EXTI_LINE;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&EXTI_InitStructure);

    /*Set key input interrupt priority*/
    NVIC_InitStructure.NVIC_IRQChannel                   = KEY_INPUT_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority           = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


ke.y:

#ifndef __KEY_H__
#define __KEY_H__
#include "n32wb03x.h"


void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin);

#endif

4. Add the key interrupt program in n32wb03_it.c:

void EXTI2_3_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(KEY_INPUT_EXTI_LINE))
    {
        if(GPIO_ReadInputDataBit(KEY_INPUT_PORT, KEY_INPUT_PIN) == RESET)
        {
            Delay(10);
            if(GPIO_ReadInputDataBit(KEY_INPUT_PORT, KEY_INPUT_PIN) == RESET)
            {
                LedBlink(LED2_PORT, LED2_PIN);
            }
        }
        EXTI_ClrITPendBit(KEY_INPUT_EXTI_LINE);
    }
}

[Note] This is just for demonstration and cannot be run in actual projects

Then initialize key\led in the main program

#include "led.h"
#include "key.h"

int main(void)
{

    LedInit(LED1_PORT, LED1_PIN);
    LedInit(LED2_PORT, LED2_PIN);
    

    LedOn(LED1_PORT, LED1_PIN);
    LedOn(LED2_PORT, LED2_PIN);
		KeyInputExtiInit(KEY_INPUT_PORT, KEY_INPUT_PIN);
    while (1)
    {
        LedBlink(LED1_PORT, LED1_PIN);


        Delay(0x28FFFF);


    }
}

In this way, you can achieve periodic flashing of LED1, and flip LED2 when Button1 is pressed. [Note] Since delay is used in the interrupt, the effect is not very good. It will be optimized after the timer is explained.

This post is from RF/Wirelessly

Latest reply

I suggest you look at the disassembly code. The delay function of Guomin will be optimized out by the regular gcc compiler under the c99 specification. I encountered this when I used their N32G45x with gcc.   Details Published on 2023-5-8 22:47
 

6841

Posts

11

Resources
2
 
Attached is the project, you can use it, if there are any deficiencies, please point them out. Thank you! N32WB031_KEY.zip (234.21 KB, downloads: 5)
This post is from RF/Wirelessly
 
 

6788

Posts

2

Resources
3
 

Are you going to test Bluetooth later?

This post is from RF/Wirelessly

Comments

Bluetooth is one of the assignments, and I still want to know what kind of power-saving thing I can make.  Details Published on 2023-4-29 17:03
 
 
 

6841

Posts

11

Resources
4
 
wangerxian posted on 2023-4-29 14:33 Are you going to test Bluetooth later?

Bluetooth is one of the assignments, and I still want to know what kind of power-saving thing I can make.

This post is from RF/Wirelessly
 
 
 

4817

Posts

4

Resources
5
 

Looking forward to some basic homework, and it would be even better if there are some small experiments in the time.

This post is from RF/Wirelessly

Comments

The main reason is that my level is limited and I need to continue practicing.  Details Published on 2023-4-29 20:58
 
 
 

6841

Posts

11

Resources
6
 
led2015 posted on 2023-4-29 20:01 Looking forward to some basic homework, and a little experiment when I have time would be even better

The main reason is that my level is limited and I need to continue practicing.

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
7
 

Hello, I would like to ask why I use the national LED example to add a button to control the LED. The button does not respond and can only detect whether the button is pressed at the moment of power on.

This post is from RF/Wirelessly

Comments

You can post the code below and I'll help you figure out what's going on.  Details Published on 2023-5-5 13:34
 
 
 

18

Posts

0

Resources
8
 

Can you add me on WeChat to discuss and help me take a look? I'm new

This post is from RF/Wirelessly
 
 
 

6841

Posts

11

Resources
9
 
J1anbean posted on 2023-5-5 11:56 Hello, I would like to ask why I use the national LED example to add a button to control the LED. The button has no response and can only be monitored at the moment of power-on...

You can post the code below and I'll help you figure out what's going on.

This post is from RF/Wirelessly
 
 
 

6841

Posts

11

Resources
10
 
J1anbean posted on 2023-5-5 11:56 Can you add WeChat to discuss and help me take a look? I'm a newbie

Sure, you can send me a private message on WeChat.

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
11
 

#ifndef __MAIN_H__
#define __MAIN_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "N32G031.H"

/*LED1-PB1, Led2-PB6, Led3-PB7*/
#define PORT_GROUP GPIOB
#define LED1_PORT PORT_GROUP
#define LED2_PORT PORT_GROUP
#define LED3_PORT PORT_GROUP #define LED1_PIN GPIO_PIN_1

#define LED2_PIN GPIO_PIN_6 #define LED3_PIN GPIO_PIN_7

#define KEY_INPUT_PORT GPIOA
#define KEY_INPUT_PIN GPIO_PIN_5
#define KEY_INPUT_EXTI_LINE EXTI_LINE5 #define KEY_INPUT_PORT_SOURCE GPIOA_PORT_SOURCE

#define KEY_INPUT_PIN_SOURCE GPIO_PIN_SOURCE5 #define KEY_INPUT_IRQn EXTI4_15_IRQn

void Delay(uint32_t count);
void LedInit(GPIO_Module* GPIOx, uint16_t Pin);
void LedOn(GPIO_Module *GPIOx, uint16_t Pin);
void LedOff(GPIO_Module* GPIOx, uint16_t Pin);
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin) ;
void LedBreath(GPIO_Module* GPIOx, uint16_t Pin);
void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin);
//uint8_t Key_Scan(GPIO_Module *GPIOx, uint16_t pin);
#ifdef __cplusplus
}#endif

#endif /* __MAIN_H__ */

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
12
 

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
13
 

image.png (216.05 KB, downloads: )

image.png
This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
14
 

void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin)
{
GPIO_InitType GPIO_InitStructure;
EXTI_InitType EXTI_InitStructure;
NVIC_InitType NVIC_InitStructure;

/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

/* Enable the GPIO Clock */
if (GPIOx == GPIOA)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
}
else if (GPIOx == GPIOB)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PER IPH_AFIO, ENABLE);
}
else if (GPIOx = = GPIOC)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);
}
else if (GPIOx == GPIOF)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF | RCC_APB2_PERIPH_AFIO, ENABLE);
}
else
{
return;
}

/*Configure the GPIO pin as input floating*/
if (Pin <= GPIO_PIN_ALL)
{
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = Pin;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
}

/*Configure key EXTI Line to key input Pin*/
GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);

/*Configure key EXTI line*/
EXTI_InitStructure.EXTI_Line = KEY_INPUT_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
Peripheral(&EXTI_InitStructure);

/*Set key input interrupt priority*/
NVIC_InitStructure.NVIC_IRQChannel = KEY_INPUT_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/**
* @brief Inserts a delay time.
* @param count specifies the delay time length.
*/
void Delay(uint32_t count)
{
for (; count > 0; count--)
;
}

/**
* @brief Configures LED GPIO.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
{
GPIO_InitType GPIO_InitStructure;

/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

/* Enable the GPIO Clock */
if (GPIOx == GPIOA)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
}
else if (GPIOx == GPIOB)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
}
else if (GPIOx == GPIOC)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
}
else if (GPIOx == GPIOF)
{
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
}
else
{
return;
}

/* Configure the GPIO pin */
if (Pin <= GPIO_PIN_ALL)
{
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
}
}

/**
* @brief Turns selected Led on.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedOn(GPIO_Module *GPIOx, uint16_t Pin )
{
GPIO_SetBits(GPIOx, Pin);
}

/**
* @brief Turns selected Led Off.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedOff(GPIO_Module* GPIOx, uint16_t Pin )
{
GPIO_ResetBits(GPIOx, Pin);
}

/**
* @brief Toggles the selected Led.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin )
{
GPIO_TogglePin(GPIOx, Pin);
}

void LedBreath(GPIO_Module* GPIOx, uint16_t Pin)
{
unsigned char i = 0;
unsigned int t = 1;
unsigned char flag = 1;
while(1)
{
if(flag == 1) //LED??
{
for(i =0;i<10;i++)
{
GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN);
Delay(t);
GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN); //LED??
Delay(1001-t);
}
t++;
if(t == 1000)
{
flag = 0;
}
}
if(flag == 0) //LED????
{
for(i=0;i<10;i++)
{
GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN); //LED??
Delay(t);
GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN); //LED??
Delay(1001-t);
}
t--;
if(t == 1)
{
flag = 1;
}
}
}
}

/**
* @brief User assertion function that failed.
* @param file The name of the call that failed.
* @param line The source line number of the call that failed.
*/
#ifdef USE_FULL_ASSERT
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line) {
while (1)

{
}
}

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
15
 

Please help me. Below is the function of LED KEY initialization and LED lighting and extinguishing. The top is the header file and macro definition. The middle is the main function.

This post is from RF/Wirelessly
 
 
 

6841

Posts

11

Resources
16
 

If you want to detect key presses, you can't write a blocking delay like this, you have to use a state machine to write it.

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
17
 

Even if I don't write the delay button like this, there is no response when I press it. I don't know what's wrong.

This post is from RF/Wirelessly
 
 
 

18

Posts

0

Resources
18
 

You can take a look at the code package and send it to you

This post is from RF/Wirelessly

Comments

OK, I'll revise it and send it to you tonight. You need to learn some knowledge about state machines, so that it will work.  Details Published on 2023-5-5 16:17
 
 
 

18

Posts

0

Resources
19
 
Project.rar (3.95 MB, downloads: 1)

This post is from RF/Wirelessly
 
 
 

6841

Posts

11

Resources
20
 
J1anbean posted on 2023-5-5 16:10 You can take a look at the code package and send it to you

OK, I'll revise it and send it to you tonight. You need to learn some knowledge about state machines, so that it will work.

This post is from RF/Wirelessly
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
Analog Circuit Basics Tutorial! E-book

Recommended ★★★★★ Data Type E-books Document language Simplified Chinese

Wireless LAN and Network Security Technology

Comprehensive Analysis of Wireless LAN and Network Security Technology

Contradictions and tradeoffs in switching power supply design

Overview Designing a switching power supply is a process full of contradictions. You can't have your cake and eat it ...

Lora parameter calculation and RF transmission distance calculation formula and tools

This post was last edited by xulikai on 2019-10-20 10:36 This content is originally created by EEWORLD forum user xuli ...

How to deal with the prompt "Transient time point calculation did not converge" during Multisim simulation

How to deal with the error "Transient time point calculation did not converge" when simulating with Multisim under high- ...

6. DMA implementation of USART1 sending and receiving

The following GD32L233C-START special review: 1. Unboxing review https://bbs.eeworld.com.cn/thread-1192788-1-1.html 2. G ...

How to configure ST-LINK/V2 in the STM32CUBEIDE environment?

There is always an error in configuring ST-LINK/V2 in the STM32CUBEIDE environment. Which master knows how to configure ...

全志V853 NPU 转换部署 YOLO V5 模型

# NPU conversion and deployment of YOLO V5 model This article takes the YOLO v5s model as an example to detail the conve ...

[DigiKey "Smart Manufacturing, Non-stop Happiness" Creative Competition] - Current Conversion Circuit Simulation

This post was last edited by lansebuluo on 2023-11-25 16:41 This chapter is used to explain the circuit for converting ...

"Machine Learning Algorithms and Implementations - Python Programming and Application Examples" Deep Learning AlexNet

The network architecture of AlexNet is shown in the figure below: 831557 It can be seen that the Alexnet model consist ...

快速回复 返回顶部 Return list