1310 views|3 replies

445

Posts

0

Resources
The OP
 

【CH32X035 Evaluation Board】--Onboard Button Function [Copy link]

 

This article describes the onboard buttons. There are two buttons on the development board, and the schematic diagram is as follows. S1 is used as a reset button, and S2 is used as a button to control the on and off of LED1.

Figure 1: Button schematic diagram

1. Button S1 is used as reset button

Button S1 is connected to PA21, and the MCU is not reset by hardware when button S1 is pressed. After checking the data, the main function of PA21 is a common IO port, and the default multiplexing function is RST, so it needs to be configured. However, the SDK and data do not clearly state the register configuration (if there are friends who can configure registers, please communicate with each other). Later, the WCH-LinkUtility host computer software is configured through WCH-LinkE as shown below:

Figure 2: Reset button configuration

2. Button S2 controls LED1, and switches on/off every time it is pressed.

The key S2 is connected to PC17. Here, the external INT interrupt is used to perform key debounce processing. Every time the key is pressed, the level of PA0 (connected to LED1, low level lights up) is reversed once to control the LED on/off. Part of the code is as follows:

<1>//Key IO PC17 external interrupt configuration


void EXTI17_INT_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    EXTI_InitTypeDef EXTI_InitStructure = {0};
    NVIC_InitTypeDef NVIC_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_17;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* GPIOA ----> EXTI_Line0 */
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource17);
    EXTI_InitStructure.EXTI_Line = EXTI_Line17;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI25_16_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    printf("EXTI17_INT_INIT\r\n");
}

<2>Key debounce and control function

	while(1)
    {
        if(key_flag)
        {
            key_flag=0;
            Delay_Ms(100);
            if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_17)==1)
            {
                led_status=GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0);
                GPIO_WriteBit(GPIOA, GPIO_Pin_0, (led_status == 0) ? (led_status=Bit_SET):(led_status=Bit_RESET));
                printf("Led Toggle!\r\n");
                if(led_status)
                {
                    printf("now led_status OFF\r\n");
                }
                else
                {
                    printf("now led_status ON\r\n");
                }

            }
        }
    }

3. Operation log

Test the button light control and reset button functions, the log is as follows:

Figure 3: Test log

4. Source code

See the attachment for all function codes

main.c

4.32 KB, downloads: 4

按键控灯程序

This post is from Domestic Chip Exchange

Latest reply

Thanks for sharing, looking forward to the follow-up!   Details Published on 2024-1-15 19:32
 
 

6827

Posts

11

Resources
2
 

I looked at the de-jitter processing of the big guys, the delay is 100ms, and ours is usually around 10Ms. I feel that it is OK to demonstrate a single function, but it is not advisable in actual projects. You can try to use a state machine to handle de-jitter.

This post is from Domestic Chip Exchange
 
 
 

445

Posts

0

Resources
3
 

This has something to do with the mechanical button debounce delay and schematic diagram. This button has no bypass capacitor. It is theoretically set to 20~50ms. If it is actually set to around 20ms, it will trigger multiple times when pressed once. It is still subject to actual debugging and testing. The parameters are determined through test experience. I have adjusted it and the effect is OK.

This post is from Domestic Chip Exchange
 
 
 

7422

Posts

2

Resources
4
 

Thanks for sharing, looking forward to the follow-up!

This post is from Domestic Chip Exchange
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list