打印
[方案相关]

HC32F460外部中断的使用

[复制链接]
1964|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

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

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


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

1.1 按键电路



   按键对应管脚 端口号:




使用特权

评论回复
沙发
家有两宝呀|  楼主 | 2021-11-30 12:44 | 只看该作者
   1.2 led对应电路图



1725261a5ac13e1600.png (25.15 KB )

1725261a5ac13e1600.png

使用特权

评论回复
板凳
家有两宝呀|  楼主 | 2021-11-30 19:43 | 只看该作者
LED对应管脚 端口号:

使用特权

评论回复
地板
家有两宝呀|  楼主 | 2021-11-30 19:47 | 只看该作者
2. 初始化GPIO

        2.1 led 按键管脚定义

使用特权

评论回复
5
家有两宝呀|  楼主 | 2021-11-30 19:48 | 只看该作者
 
#define LED1_PORT   PortD
#define LED1_Pin    Pin02

#define LED2_PORT   PortB
#define LED2_Pin    Pin03

#define LED3_PORT   PortB
#define LED3_Pin    Pin04

#define LED4_PORT   PortB
#define LED4_Pin    Pin05

#define KEY1_PORT   PortA
#define KEY1_Pin    Pin04

#define KEY2_PORT   PortA
#define KEY2_Pin    Pin05

#define KEY3_PORT   PortA
#define KEY3_Pin    Pin06

#define ReadPort                PortA
#define ReadPin                        Pin07

使用特权

评论回复
6
家有两宝呀|  楼主 | 2021-11-30 19:51 | 只看该作者
2.2 管脚配置

使用特权

评论回复
7
家有两宝呀|  楼主 | 2021-11-30 19:53 | 只看该作者
void User_Gpio_Init(void)
{
  stc_port_init_t Port_CFG;

        MEM_ZERO_STRUCT(Port_CFG);

        Port_CFG.enPinMode = Pin_Mode_Out;

        PORT_Init(LED1_PORT, LED1_Pin, &Port_CFG);
        PORT_Init(LED2_PORT, LED2_Pin, &Port_CFG);
        PORT_Init(LED3_PORT, LED3_Pin, &Port_CFG);
        PORT_Init(LED4_PORT, LED4_Pin, &Port_CFG);
   
}

使用特权

评论回复
8
家有两宝呀|  楼主 | 2021-11-30 19:54 | 只看该作者
2.3 点亮 熄灭方法(led1为例):        
PORT_SetBits(LED1_PORT,LED1_Pin);

PORT_ResetBits(LED1_PORT,LED1_Pin);

使用特权

评论回复
9
家有两宝呀|  楼主 | 2021-11-30 19:55 | 只看该作者
3. 中断配置

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

     /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcExtiConfig);
    MEM_ZERO_STRUCT(stcIrqRegiConf);
    MEM_ZERO_STRUCT(stcPortInit);

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

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

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

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

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

    /* 使能 NVIC */
    NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
   

使用特权

评论回复
10
家有两宝呀|  楼主 | 2021-11-30 19:55 | 只看该作者
3.2 回调函数配置

        自己定义了两个结构体测试用

使用特权

评论回复
11
家有两宝呀|  楼主 | 2021-11-30 19:56 | 只看该作者
typedef struct{
        uint8_t        key1:1;
        uint8_t        key2:1;
        uint8_t        key3:1;
        uint8_t        key4:1;
        uint8_t        key5:1;
        uint8_t        key6:1;
        uint8_t        key7:1;
        uint8_t        key8:1;
}Key_IO_bit_t;

typedef union{
        Key_IO_bit_t keys;
        uint8_t status;
}KEY_status_t;

使用特权

评论回复
12
家有两宝呀|  楼主 | 2021-11-30 20:05 | 只看该作者
  回调函数:
static void key_1_callback(void){
                static int i = 0;
                KEY_status_t keyGroup = {0};
                if(i == 65534){
                                i = 0;
                }
                if(Set == EXINT_Irq**Get(ExtiCh01)){

                               
                        do{
                                keyGroup.keys.key1 =  PORT_GetBit(KEY1_PORT, KEY1_Pin);
                                keyGroup.keys.key2 =  PORT_GetBit(KEY2_PORT, KEY2_Pin);
                                keyGroup.keys.key3 =  PORT_GetBit(KEY3_PORT, KEY3_Pin);
                                if(keyGroup.status != 7) break;
                        }while(1);
                       
                        Ddl_Delay1ms(40);
                       
                        if((keyGroup.keys.key1 == Reset) && (Reset == PORT_GetBit(KEY1_PORT, KEY1_Pin))){
                               
                                if(i%2== 1){
                                PORT_ResetBits(LED1_PORT,LED1_Pin);
                                }else{
                                        PORT_SetBits(LED1_PORT,LED1_Pin);
                                }
                                logDebug("key1Press! cnt:%d\n",i);
                                i++;
                        }else if((keyGroup.keys.key2 == Reset) && (Reset == PORT_GetBit(KEY2_PORT, KEY2_Pin))){
                                if(i%2== 1){
                                PORT_ResetBits(LED2_PORT,LED2_Pin);
                                }else{
                                        PORT_SetBits(LED2_PORT,LED2_Pin);
                                }
                                logDebug("key2Press! cnt:%d\n",i);
                                i++;
                        }else if((keyGroup.keys.key3 == Reset) && (Reset == PORT_GetBit(KEY3_PORT, KEY3_Pin))){
                               
                                if(i%2== 1){
                                PORT_ResetBits(LED3_PORT,LED3_Pin);
                                }else{
                                        PORT_SetBits(LED3_PORT,LED3_Pin);
                                }
                                logDebug("key3Press! cnt:%d\n",i);
                                i++;
                        }
                       
       /* clear int request flag */
      EXINT_Irq**Clr(ExtiCh01);
                }
}

使用特权

评论回复
13
家有两宝呀|  楼主 | 2021-11-30 20:06 | 只看该作者
剩下的就是编译,烧录测试!

使用特权

评论回复
14
家有两宝呀|  楼主 | 2021-11-30 20:11 | 只看该作者
备注:

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

                PORT_DebugPortSetting(TDO_SWO | TDI | TRST, Disable);

使用特权

评论回复
15
lqwuliang| | 2021-12-10 09:05 | 只看该作者
看看,学习学习

使用特权

评论回复
16
长江一道浪| | 2022-7-28 17:28 | 只看该作者
楼主我使用外部中断PC2成功了,但是PC3使用外部中断功能就是不行,我试了下PC3的读取外部输入功能也没错,那PC3是不是有什么特殊配置才能使用外部中断功能呀?

使用特权

评论回复
17
match007| | 2022-8-9 21:06 | 只看该作者
感谢分享,学习了~~

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

46

主题

535

帖子

0

粉丝