【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
|