[方案相关] HC32F460外部中断的使用

[复制链接]
3186|16
 楼主| 家有两宝呀 发表于 2021-11-30 12:42 | 显示全部楼层 |阅读模式

本次主要是使用按键触发外部中断,打印按下的按键,同时操作led灯。

外部中断操作流程参考《HC32F460系列的中断控制器INTC Rev1.1.pdf》中的使用方法。


1. 首先是介绍硬件电路的接法

1.1 按键电路


280561a5ab8414339.png


2433761a5ab8aba002.png

   按键对应管脚 端口号:

1163061a5ab9d4e68d.png




 楼主| 家有两宝呀 发表于 2021-11-30 12:44 | 显示全部楼层
   1.2 led对应电路图

2685261a5ac2beb3c7.png

6609861a5ac34c11f1.png
1725261a5ac13e1600.png
 楼主| 家有两宝呀 发表于 2021-11-30 19:43 | 显示全部楼层
LED对应管脚 端口号:
4455761a5ac53ea08b.png
 楼主| 家有两宝呀 发表于 2021-11-30 19:47 | 显示全部楼层
2. 初始化GPIO

        2.1 led 按键管脚定义
 楼主| 家有两宝呀 发表于 2021-11-30 19:48 | 显示全部楼层

  1. #define LED1_PORT   PortD
  2. #define LED1_Pin    Pin02

  3. #define LED2_PORT   PortB
  4. #define LED2_Pin    Pin03

  5. #define LED3_PORT   PortB
  6. #define LED3_Pin    Pin04

  7. #define LED4_PORT   PortB
  8. #define LED4_Pin    Pin05

  9. #define KEY1_PORT   PortA
  10. #define KEY1_Pin    Pin04

  11. #define KEY2_PORT   PortA
  12. #define KEY2_Pin    Pin05

  13. #define KEY3_PORT   PortA
  14. #define KEY3_Pin    Pin06

  15. #define ReadPort                PortA
  16. #define ReadPin                        Pin07
 楼主| 家有两宝呀 发表于 2021-11-30 19:51 | 显示全部楼层
2.2 管脚配置
 楼主| 家有两宝呀 发表于 2021-11-30 19:53 | 显示全部楼层
  1. void User_Gpio_Init(void)
  2. {
  3.   stc_port_init_t Port_CFG;

  4.         MEM_ZERO_STRUCT(Port_CFG);

  5.         Port_CFG.enPinMode = Pin_Mode_Out;

  6.         PORT_Init(LED1_PORT, LED1_Pin, &Port_CFG);
  7.         PORT_Init(LED2_PORT, LED2_Pin, &Port_CFG);
  8.         PORT_Init(LED3_PORT, LED3_Pin, &Port_CFG);
  9.         PORT_Init(LED4_PORT, LED4_Pin, &Port_CFG);
  10.    
  11. }
 楼主| 家有两宝呀 发表于 2021-11-30 19:54 | 显示全部楼层
2.3 点亮 熄灭方法(led1为例):        
  1. PORT_SetBits(LED1_PORT,LED1_Pin);

  2. PORT_ResetBits(LED1_PORT,LED1_Pin);
 楼主| 家有两宝呀 发表于 2021-11-30 19:55 | 显示全部楼层
3. 中断配置

      3.1 中断函数配置
  1.     stc_exint_config_t stcExtiConfig;        /* 外部中断配置结构体 */
  2.     stc_irq_regi_conf_t stcIrqRegiConf; /*irq配置结构体 */
  3.     stc_port_init_t stcPortInit;        /*管脚配置结构体 */

  4.      /* configuration structure initialization */
  5.     MEM_ZERO_STRUCT(stcExtiConfig);
  6.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  7.     MEM_ZERO_STRUCT(stcPortInit);

  8.     /* Set PD06 as External Int Ch.6 input */
  9.     stcPortInit.enExInt = Enable;
  10.         PORT_Init(KEY1_PORT, KEY1_Pin, &stcPortInit);
  11.         PORT_Init(KEY2_PORT, KEY2_Pin, &stcPortInit);
  12.         PORT_Init(KEY3_PORT, KEY3_Pin, &stcPortInit);

  13.     stcExtiConfig.enExitCh = ExtiCh01;
  14.         /* 过滤器配置 */
  15.     stcExtiConfig.enFilterEn = Enable;
  16.     stcExtiConfig.enFltClk = Pclk3Div8;
  17.     /* 低电平触发(触发模式) */
  18.     stcExtiConfig.enExtiLvl = ExIntLowLevel;
  19.     EXINT_Init(&stcExtiConfig);

  20.     /* 选择外部中断通道 Ch.1 */
  21.     stcIrqRegiConf.enIntSrc = INT_PORT_EIRQ1;
  22.     /*注册外部中断 to Vect.No.000 */
  23.     stcIrqRegiConf.enIRQn = Int000_IRQn;
  24.     /* 绑定回调函数 */
  25.     stcIrqRegiConf.pfnCallback = &key_1_callback;
  26.     enIrqRegistration(&stcIrqRegiConf);

  27.     /* Clear Pending */
  28.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);

  29.     /* 配置优先级 */
  30.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_15);

  31.     /* 使能 NVIC */
  32.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
  33.    
 楼主| 家有两宝呀 发表于 2021-11-30 19:55 | 显示全部楼层
3.2 回调函数配置

        自己定义了两个结构体测试用
 楼主| 家有两宝呀 发表于 2021-11-30 19:56 | 显示全部楼层
  1. typedef struct{
  2.         uint8_t        key1:1;
  3.         uint8_t        key2:1;
  4.         uint8_t        key3:1;
  5.         uint8_t        key4:1;
  6.         uint8_t        key5:1;
  7.         uint8_t        key6:1;
  8.         uint8_t        key7:1;
  9.         uint8_t        key8:1;
  10. }Key_IO_bit_t;

  11. typedef union{
  12.         Key_IO_bit_t keys;
  13.         uint8_t status;
  14. }KEY_status_t;
 楼主| 家有两宝呀 发表于 2021-11-30 20:05 | 显示全部楼层
  回调函数:
  1. static void key_1_callback(void){
  2.                 static int i = 0;
  3.                 KEY_status_t keyGroup = {0};
  4.                 if(i == 65534){
  5.                                 i = 0;
  6.                 }
  7.                 if(Set == EXINT_Irq**Get(ExtiCh01)){

  8.                                
  9.                         do{
  10.                                 keyGroup.keys.key1 =  PORT_GetBit(KEY1_PORT, KEY1_Pin);
  11.                                 keyGroup.keys.key2 =  PORT_GetBit(KEY2_PORT, KEY2_Pin);
  12.                                 keyGroup.keys.key3 =  PORT_GetBit(KEY3_PORT, KEY3_Pin);
  13.                                 if(keyGroup.status != 7) break;
  14.                         }while(1);
  15.                        
  16.                         Ddl_Delay1ms(40);
  17.                        
  18.                         if((keyGroup.keys.key1 == Reset) && (Reset == PORT_GetBit(KEY1_PORT, KEY1_Pin))){
  19.                                
  20.                                 if(i%2== 1){
  21.                                 PORT_ResetBits(LED1_PORT,LED1_Pin);
  22.                                 }else{
  23.                                         PORT_SetBits(LED1_PORT,LED1_Pin);
  24.                                 }
  25.                                 logDebug("key1Press! cnt:%d\n",i);
  26.                                 i++;
  27.                         }else if((keyGroup.keys.key2 == Reset) && (Reset == PORT_GetBit(KEY2_PORT, KEY2_Pin))){
  28.                                 if(i%2== 1){
  29.                                 PORT_ResetBits(LED2_PORT,LED2_Pin);
  30.                                 }else{
  31.                                         PORT_SetBits(LED2_PORT,LED2_Pin);
  32.                                 }
  33.                                 logDebug("key2Press! cnt:%d\n",i);
  34.                                 i++;
  35.                         }else if((keyGroup.keys.key3 == Reset) && (Reset == PORT_GetBit(KEY3_PORT, KEY3_Pin))){
  36.                                
  37.                                 if(i%2== 1){
  38.                                 PORT_ResetBits(LED3_PORT,LED3_Pin);
  39.                                 }else{
  40.                                         PORT_SetBits(LED3_PORT,LED3_Pin);
  41.                                 }
  42.                                 logDebug("key3Press! cnt:%d\n",i);
  43.                                 i++;
  44.                         }
  45.                        
  46.        /* clear int request flag */
  47.       EXINT_Irq**Clr(ExtiCh01);
  48.                 }
  49. }
 楼主| 家有两宝呀 发表于 2021-11-30 20:06 | 显示全部楼层
剩下的就是编译,烧录测试!
 楼主| 家有两宝呀 发表于 2021-11-30 20:11 | 显示全部楼层
备注:

关于部分io 比如swd下载的部分管脚默认上电以后是swd功能如果需要使用io功能则需要屏蔽对应的swd功能, 参考之前的帖子或者使用代码:

  1.                 PORT_DebugPortSetting(TDO_SWO | TDI | TRST, Disable);
lqwuliang 发表于 2021-12-10 09:05 | 显示全部楼层
看看,学习学习
长江一道浪 发表于 2022-7-28 17:28 | 显示全部楼层
楼主我使用外部中断PC2成功了,但是PC3使用外部中断功能就是不行,我试了下PC3的读取外部输入功能也没错,那PC3是不是有什么特殊配置才能使用外部中断功能呀?
match007 发表于 2022-8-9 21:06 | 显示全部楼层
感谢分享,学习了~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

62

主题

585

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部