1914 views|1 replies

2870

Posts

4

Resources
The OP
 

[RISC-V MCU CH32V103 Review] Using EXIT Interrupt [Copy link]

 

RISC-V MCU CH32V103, external interrupt test.

If you want to use external interrupts, you must make necessary settings. How to set it up? This is a very troublesome problem, because there is no detailed explanation in the manual. There is no good way but to analyze the routine. The manual only says

"The chip's external interrupt/event controller (EXTI) has 20 edge detectors for generating interrupt/event requests. Each interrupt line can be independently configured with its trigger event (rising edge or falling edge or both edges) and can be individually masked; the pending register maintains all interrupt request states. EXTI can detect pulse widths less than the internal APB2 clock cycle. Up to 51 general-purpose I/O ports can be optionally connected to 16 external interrupt lines."

My understanding is that there are 20 event sources and 51 general GPIOs can be linked to these interrupt sources.

void EXTI0_INT_INIT(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
 	EXTI_InitTypeDef EXTI_InitStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;

  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);
}

First, define three structures.

  GPIO_InitTypeDef  GPIO_InitStructure;//GPIO数据结构
  EXTI_InitTypeDef EXTI_InitStructure;//外部事件中断结构
  NVIC_InitTypeDef NVIC_InitStructure;//中断管理器结构

Initialization steps for this experiment

1. GPIO is defined as input mode

2. Define the EXTI_Line0 interrupt detector of the EXIT manager as falling edge trigger, turn on this function, and enable interrupts. Link EXTI_Line0 and GPIO_PinSource0.

Don't worry, we can't interrupt yet! This is just connecting the line to the interrupt detector ( the vague thing here is the parameter GPIO_PinSource0, is this a specific pin? )

3. Initialize the interrupt priority manager NVIC, start the interrupt source EXTI0_IRQn, set the main priority to 1, and the secondary priority to 2.

Now we can start the interrupt. The next step is to write the interrupt service program.

4. Interrupt service routine. This needs to be written in the file ch32v10x_it.c. Although it is not necessary to do so here, it is easier to manage.

void EXTI0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));


/*******************************************************************************
* Function Name  : EXTI0_IRQHandler
* Description    : This function handles EXTI0 Handler.
* Input          : None
* Return         : None
*******************************************************************************/
void EXTI0_IRQHandler(void)
{
  if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
  {
    printf("Run at EXTI\r\n");
    EXTI_ClearITPendingBit(EXTI_Line0);     /* Clear Flag */
  }
}

Note that you should not use the name EXTI0_IRQHandler casually, because the linker will not be able to find it.

The interruption is now complete.

Just call the initialization program in the main function to interrupt.

This post is from Domestic Chip Exchange
 
 

2870

Posts

4

Resources
2
 
This post was last edited by bigbat on 2021-1-23 17:32

The tests so far are normal.

image.png (27.6 KB, downloads: 0)

image.png

image.png (27.73 KB, downloads: 0)

image.png
This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
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