GD32 actual LED lamp lighting 3__

Knowledge Point

1. GPIO control

2. LED lamp driving principle

3. Check the key principles

Hardware parts

Here Insert Picture Description

FIG above, LED lights negative ground, the positive electrode through the 470 ohm resistor, to the pin on the MCU. As can be seen, when the output of the GPIO port of MCU

LED light is high,

LED off low.

Here Insert Picture Description

Above,

When the key-release, GPIO connected to the 3.3V, a high level

When depressed, the GPIO connected to ground, a low level

Here Insert Picture Description

note:

  1. Schematic of K1, K2, K3 K2 respectively on the board, K3, K4, according to the principles described herein are described with FIG.
  2. 0.1uF capacitor to shake effect

feature design

Design of a small feature, press KEY3 four LED lights, LED 4 when bouncing off. Including sub-function design,

1. Functional state design

Here Insert Picture Description

As shown above, this function altogether to go through several states above, in order to understand what features will function we need to provide details read the code below.

static VOID LED_Init(VOID)
{
    DRV_LED_Init();
    DRV_KEY_Init();
}

static VOID LED_SetLedStatus(IN U8 status)
{
    if (DRV_KEY_DOWN == status)
    {
        DRV_LED_On(DRV_LED1);
        DRV_LED_On(DRV_LED2);
        DRV_LED_On(DRV_LED3);
        DRV_LED_On(DRV_LED4);
    }
    else if (DRV_KEY_UP == status)
    {
        DRV_LED_Off(DRV_LED1);
        DRV_LED_Off(DRV_LED2);
        DRV_LED_Off(DRV_LED3);
        DRV_LED_Off(DRV_LED4);
    }
    else
    {
        ;
    }
}

static VOID LED_CheckKeyStatus(VOID)
{
    U8 keyStatus = 0;
    
    keyStatus = DRV_KEY_GetStatus(DRV_KEY3);
    if (DRV_KEY_DOWN == keyStatus)
    {
        APP_Delay(50); /* 50ms去抖动 */
        keyStatus = DRV_KEY_GetStatus(DRV_KEY3);
        if (DRV_KEY_DOWN == keyStatus)
        {
            LED_SetLedStatus(DRV_KEY_DOWN);
        }
    }
    else
    {
        LED_SetLedStatus(DRV_KEY_UP);
    }
}

VOID APP_LED_Test(VOID)
{
    LED_Init();
    LED_SetLedStatus(DRV_KEY_UP);
    
    while (1)
    {
        LED_CheckKeyStatus();
    }
}

2. Timer Design

1. 采用systick作为功能定时器,初始配置成1ms一次中断
2. 提供delay延时函数
static U32 gDrvSystickDelayCount = 0;

S32 DRV_SYSTICK_Init(VOID)
{
    /* 1000Hz,1ms中断一次 */
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        return OS_ERROR;
    }
    
    NVIC_SetPriority(SysTick_IRQn, 0x00);

    return OS_OK;
}

/* 1ms中断一次 */
VOID SysTick_Handler(VOID)
{
    if (gDrvSystickDelayCount > 0)
    {
        gDrvSystickDelayCount--;
    }
}

VOID DRV_SYSTICK_Delay(IN U32 ms)
{
    gDrvSystickDelayCount = ms;
    while (1)
    {
        if (gDrvSystickDelayCount <= 0)
        {
            break;
        }
    }

    return;
}

3. LED driver

  1. Provides initialization function interface
  2. Providing LED on / off Interface
#define DRV_LED1 GPIOC,GPIO_PIN_0
#define DRV_LED2 GPIOC,GPIO_PIN_2
#define DRV_LED3 GPIOE,GPIO_PIN_0
#define DRV_LED4 GPIOE,GPIO_PIN_1
#define DRV_LED_On(led) GPIO_SetBits(led);
#define DRV_LED_Off(led) GPIO_ResetBits(led);

extern void DRV_LED_Init(void);

4. The key activation

  1. It provides an interface to initialize
  2. It provides an interface for the key state
#define DRV_KEY3 GPIOB, GPIO_PIN_14
#define DRV_KEY_GetStatus(key) GPIO_ReadInputBit(key)
#define DRV_KEY_DOWN 0
#define DRV_KEY_UP 1

extern VOID DRV_KEY_Init(VOID);

to sum up

IO configurations are summarized (Configuration Clock (required) -> select multiplexing (optional) -> Mode Select (required) -> configurable rate (required)):

  1. Analyzing a GPIO (General Purpose IO) and AFIO (multiplexed IO), can be found from the datasheet PIN definition section, as Here Insert Picture Descriptiondefault is configured as a normal GPIO port, remap (multiplexed) as TM1_BKIN

  2. IO GPIO port as a general use, the configuration process is as follows:

    1. GPIO clock configuration, can be seen from the FIG., Hung on GPIO APB2 and, therefore, the following configuration codeRCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_GPIOC |RCC_APB2PERIPH_GPIOE,ENABLE);Here Insert Picture Description

    2. GPIO arranged direction, modes and rates Here Insert Picture DescriptionHere Insert Picture Descriptionshown above in several ways, as the configuration code, note that the input rate when the hardware has been configured, the software does not need to configure

      GPIO_InitPara GPIO_InitStructure;
      
      /* 上拉输出,50MHz */
      GPIO_InitStructure.GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_2;
      GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ;
      GPIO_Init(GPIOC,&GPIO_InitStructure);
      
      /* 浮空输入 */
      GPIO_InitStructure.GPIO_Pin = GPIO_PIN_14;
      GPIO_InitStructure.GPIO_Mode = GPIO_MODE_IN_FLOATING;
      GPIO_Init(GPIOB,&GPIO_InitStructure);
      
    3. Configurable GPIO port interrupt, the interrupt chapter reference

  3. AFIO remap process, to enable AF clock, then call pinRemap function can be remapped

    RCC_APB2PeriphClock_Enable(RCC_APB2PERIPH_AF, ENABLE);
    GPIO_PinRemapConfig(GPIO_REMAP_SWJ_DISABLE, ENABLE);
    

Reference material

《GD32F10xCH_V1.1.pdf》

《GD32103C-EVAL-V1.1.pdf》

《GD32F103xxDatasheetRev2.2.pdf》

Code path

https://github.com/YaFood/GD32F103/tree/master/TestLED

Guess you like

Origin blog.csdn.net/qq_17854661/article/details/91876312