985 views|3 replies

444

Posts

0

Resources
The OP
 

【All-round Small Gateway|CH32V208】--3.Key Interrupt [Copy link]

 

This article describes the key IO external interrupt.

1. Button principle and preparation

From the schematic diagram below, we know that the user key is triggered by the falling edge of the low level. Here we use PA0 as the key detection IO port, and connect the pin header PA0 and the KEY pin with the DuPont line.

Figure 1: Button principle

2. Code Preparation

1. Initialize the configuration of PA0 external interrupt, falling edge triggered.

void EXTI0_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_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* GPIOA ----> EXTI_Line0 */
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

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

2. Interrupt function. Here we simply print the log

/*********************************************************************
 * @fn      EXTI0_IRQHandler
 *
 * [url=home.php?mod=space&uid=159083]@brief[/url] This function handles EXTI0 Handler.
 *
 * [url=home.php?mod=space&uid=784970]@return[/url] none
 */
void EXTI0_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
  {
    printf("Run at EXTI\r\n");
    EXTI_ClearITPendingBit(EXTI_Line0);     /* Clear Flag */
  }
}

3. Main function

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    printf("Uart Initr\n");
    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    printf("EXTI0 Test\r\n");
    EXTI0_INT_INIT();
    printf("INT Init\n");

    while(1)
    {
        Delay_Ms(1000);
        //printf("Run at main\r\n");
    }
}

3. Test

Compile and burn, power on and run, check the log to see that it is initialized. When the user button S2 is pressed, you can see that the IO interrupt is triggered.

Figure 2: Key interrupt log

At this point, the key IO external interrupt function is realized.

This post is from Domestic Chip Exchange

Latest reply

This depends on whether the printf redirection you use blocks the output. If it does, or there is a problem with the peripheral, then the entire program will be affected.   Details Published on 2024-6-12 14:25
 
 

6818

Posts

11

Resources
2
 

Will printing logs in interrupt callback slow down the interrupt response?

This post is from Domestic Chip Exchange

Comments

This is not bad. If you are not sure, you can put the print after clearing the flag in the interrupt. It is not advisable to put applications that block for a long time in the interrupt. You can also set the flag for the main function to handle it.  Details Published on 2024-6-12 09:16
 
 
 

444

Posts

0

Resources
3
 
lugl4313820 posted on 2024-6-10 18:58 Will printing logs in interrupt callbacks slow down the response of the interrupt?

This is not bad. If you are not sure, you can put the print after clearing the flag in the interrupt. It is not advisable to put applications that block for a long time in the interrupt. You can also set the flag for the main function to handle it.

This post is from Domestic Chip Exchange

Comments

This depends on whether the printf redirection you use blocks the output. If it does, or there is a problem with the peripheral, then the entire program will be affected.  Details Published on 2024-6-12 14:25
 
 
 

6818

Posts

11

Resources
4
 
dirty posted on 2024-6-12 09:16 This is not bad. If you are worried, you can put the print after the clear flag in the interrupt. It is not advisable to put applications that block for a long time in the interrupt. You can also set the flag ma ...

This depends on whether the printf redirection you use blocks the output. If it does, or there is a problem with the peripheral, then the entire program will be affected.

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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