打印

【CPKCOR-RA8D1B核心板】7、 加入新的交互——按键

[复制链接]
1475|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
#申请原创# @21小跑堂 @21小跑堂 @21小跑堂
往期回顾
【CPKCOR-RA8D1B核心板】1、板卡基本信息
【CPKCOR-RA8D1B核心板】2、搭建开发环境
【CPKCOR-RA8D1B核心板】3、离不开的点灯——GPIO输出操作
【CPKCOR-RA8D1B核心板】4、另一种闪烁——非阻塞式闪烁
【CPKCOR-RA8D1B核心板】5、不一样的点灯系列——点呼吸灯
【CPKCOR-RA8D1B核心板】6、指示明灯——串口打印

       按键作为最直接的交互方式在很多的场景中都被广泛应用,咱们得核心板上也配备了一个用户按键,接下来我们进行一下对外部中断按键状态采集。
       首先了解一下外部中断:
       上图是ICU框图,根据ICU的功能框图可以知道,首先需要配置IRQCR寄存器(IRQ Control Register,IRQ英文全称:Interrupt ReQuest,中文名:中断请求);之后我们要配置IELSR寄存器(英文全称:ICU Event Link Setting Register,中文名:中断控制单元事件链接设置寄存器);之后使能IR寄存器; 最后我们需要配置NVIC。
当中断来临的时候会最先经过IRQ寄存器,IRQ寄存器检测到中断的时候,会向中央处理嵌套向量中断控制器NVIC发送中断信号, 当NVIC检测到中断请求的时候,会将相应的中断服务函数进行挂起。之后将运行的八个寄存器进行压栈,堆栈完成之后将中断服务程序进行激活。 此时当另一个优先级更高的中断源发生请求的时候,会将相应的中断服务函数进行挂起。之后将当前运行的八个寄存器进行压栈,压栈完成之后将中断服务程序进行激活。 当中断服务程序运行完之后会将原先压栈的寄存器取出,之后继续运行之前中断服务的程序。中断服务程序运行完成之后之后将原先压栈的寄存器取出,继续运行之前的程序。
       中断在嵌入式应用中的地位不言而喻,之前的定时器和串口都有使用中断服务,中断对事件得到第一时间处理是非常重要的。 我们设计使用按键来作为触发源,使得控制器产生中断,并在中断服务函数中修改LED以及呼吸灯速率。
       硬件按键电路:
       另一个按键为复位按键,用于复位MCU芯片,上面这个按键是用户按键,可以用于本次的外部中断实现,对应的引脚是P008,按键按下时P008会出现下降沿和低电平。
软件需要哪些处理呢?
       首先配置中断,先去引脚看一下对应的中断(P008-IRQ12):
       然后配置引脚状态:
       创建外部中断外设:
       属性中配置:
       外部中断模块(r_icu)的属性介绍如下表所示。

External IRQ 属性
描述
Name
名称
Channel
通道(IRQ中断号)
Trigger
触发方式:上升沿、下降沿、双边沿或低电平触发
Digital Filtering
数字滤波使能
Digital Filtering Sample Clock
(Only valid when Digital Filtering is Enabled)
数字滤波采样时钟
Callback
中断回调函数
2个按键外部中断的中断回调函数可以设置为相同,
只需要在函数中判断触发中断的中断号即可互相区分
Pin Interrupt Priority
中断优先级
       配置完成之后可以按下快捷键“Ctrl + S”保存, 最后点右上角的 “Generate Project Content” 按钮,让软件自动生成配置代码即可。
       软件中添加初始化程序:
<article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;qJuV-1729007702184&quot;,&quot;name&quot;:&quot;code&quot;,&quot;data&quot;:{&quot;wrap&quot;:false,&quot;tokensPerLine&quot;:[],&quot;language&quot;:&quot;&quot;,&quot;theme&quot;:&quot;default&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;FRbM-1729007702183&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;zZOv-1729007702182&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;void Key_IRQ_Init(void){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;P0ul-1729007704223&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;LMEk-1729007704222&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   fsp_err_t err = FSP_SUCCESS;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Kscz-1729007704227&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Qby6-1729007704226&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   /* Open ICU module */&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;8WcO-1729007704229&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;SHw3-1729007704228&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   err = R_ICU_ExternalIrqOpen(&g_external_irq12_ctrl, &g_external_irq12_cfg);&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;gUfk-1729007704233&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;G8oN-1729007704232&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   err = R_ICU_ExternalIrqEnable(&g_external_irq12_ctrl);&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;9L3j-1729007704237&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;KjTu-1729007704236&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;}&quot;,&quot;marks&quot;:[]}]}]}],&quot;state&quot;:{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">void Key_IRQ_Init(void){
   fsp_err_t err = FSP_SUCCESS;
   /* Open ICU module */
   err = R_ICU_ExternalIrqOpen(&g_external_irq12_ctrl, &g_external_irq12_cfg);
   err = R_ICU_ExternalIrqEnable(&g_external_irq12_ctrl);
}</div></article>
      中断回调函数处理:
<article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;YRbC-1729007732439&quot;,&quot;name&quot;:&quot;code&quot;,&quot;data&quot;:{&quot;wrap&quot;:false,&quot;tokensPerLine&quot;:[],&quot;language&quot;:&quot;&quot;,&quot;theme&quot;:&quot;default&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;LW3W-1729007732438&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;DGQi-1729007732437&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;void g_external_irq12_Callback(external_irq_callback_args_t *p_args){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;uwZW-1729007762925&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;W47j-1729007762924&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   /* 判断中断通道 */&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;UvGQ-1729007762927&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;frvP-1729007762926&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   if (12 == p_args->channel){// 用户按键按下&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;UtNW-1729007762929&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;19S0-1729007762928&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;       LED.speed++;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;5J3v-1729007762931&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Kaqr-1729007762930&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;       LED.speed %= 3;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;4WIo-1729007762933&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;RGgQ-1729007762932&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;   }&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Unpx-1729007762935&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;ImqI-1729007762934&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;}&quot;,&quot;marks&quot;:[]}]}]}],&quot;state&quot;:{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">void g_external_irq12_Callback(external_irq_callback_args_t *p_args){
   /* 判断中断通道 */
   if (12 == p_args->channel){// 用户按键按下
       LED.speed++;
       LED.speed %= 3;
   }
}</div></article>
      在定时器中断对LED处理处加入速率控制:
<article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;sR1t-1729007798556&quot;,&quot;name&quot;:&quot;code&quot;,&quot;data&quot;:{&quot;wrap&quot;:false,&quot;tokensPerLine&quot;:[],&quot;language&quot;:&quot;&quot;,&quot;theme&quot;:&quot;default&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;w4qk-1729007798555&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;CLdL-1729007798554&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;void g_timer0_Callback(timer_callback_args_t * p_args){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;u76G-1729007813673&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;KAj2-1729007813672&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;    if (TIMER_EVENT_CYCLE_END == p_args->event){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;W33T-1729007813675&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;TlyD-1729007813674&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        LED.timercnt++;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;BesV-1729007813677&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;11Ro-1729007813676&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        if(LED.timercnt >= 50*(LED.speed+1)){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;FAo4-1729007813679&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;G730-1729007813678&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            LED.timercnt = 0;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;3Bm6-1729007813682&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;GfNz-1729007813681&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            LED.flag = 1;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;lpOC-1729007813684&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;5zTv-1729007813683&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            LED.cnt++;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Zlv8-1729007813686&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;NrSQ-1729007813685&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        }&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Pzjd-1729007813688&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;5Na7-1729007813687&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;HzqE-1729007813690&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;hzeo-1729007813689&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        BreathLED.cnt++;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;2Vxc-1729007813692&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;dZen-1729007813691&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        if(BreathLED.cnt>5 && BreathLED.cnt<=95){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;NQSs-1729007813694&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;JZrK-1729007813693&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            GPT4_PWM_SetDuty(BreathLED.cnt);&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;eawf-1729007813696&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;B5d1-1729007813695&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        }&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;jqxj-1729007813698&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;ltIh-1729007813697&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        else if(BreathLED.cnt>105 && BreathLED.cnt<=195){&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;u6Tt-1729007813700&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;lO9U-1729007813699&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            GPT4_PWM_SetDuty(200-BreathLED.cnt);&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;RYAA-1729007813702&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;dIGX-1729007813701&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        }&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;oQIs-1729007813704&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;UO02-1729007813703&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;        else if(BreathLED.cnt >= 200)&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;vcTL-1729007813706&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;MxrG-1729007813705&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;            BreathLED.cnt = 0;&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;1l9q-1729007813708&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;oCPg-1729007813707&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;    }&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;3Aqc-1729007813710&quot;,&quot;name&quot;:&quot;code-line&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;dXiX-1729007813709&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;}&quot;,&quot;marks&quot;:[]}]}]}],&quot;state&quot;:{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">void g_timer0_Callback(timer_callback_args_t * p_args){
    if (TIMER_EVENT_CYCLE_END == p_args->event){
        LED.timercnt++;
        if(LED.timercnt >= 50*(LED.speed+1)){
            LED.timercnt = 0;
            LED.flag = 1;
            LED.cnt++;
        }

        BreathLED.cnt++;
        if(BreathLED.cnt>5 && BreathLED.cnt<=95){
            GPT4_PWM_SetDuty(BreathLED.cnt);
        }
        else if(BreathLED.cnt>105 && BreathLED.cnt<=195){
            GPT4_PWM_SetDuty(200-BreathLED.cnt);
        }
        else if(BreathLED.cnt >= 200)
            BreathLED.cnt = 0;
    }
}</div></article>
      下载验证就可以看到我们可以通过按键控制LED闪烁的速率了:

使用特权

评论回复

相关帖子

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

本版积分规则

认证:硬件工程师
简介:有着多年硬件开发经验的专业人员,专注于医疗电子领域,热衷于对新鲜事物的探索,喜欢DIY!

270

主题

1621

帖子

8

粉丝