[技术问答] 求M451的灵敏按键的程序

[复制链接]
1130|14
 楼主| 一箩筐呆萌 发表于 2017-5-18 22:01 | 显示全部楼层 |阅读模式
我是小白
首先,需要使能时钟吗,如果需要怎么使能
然后,初始化GPIO
最后,想要实现按下去为低电平
yiy 发表于 2017-5-18 22:11 | 显示全部楼层
你是想用中断,还是普通输入模式?
yiy 发表于 2017-5-18 22:12 | 显示全部楼层
GPIO中断的例子
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 7 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the usage of GPIO interrupt function.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M451Series.h"


  12. #define PLLCTL_SETTING  CLK_PLLCTL_72MHz_HXT
  13. #define PLL_CLOCK       72000000


  14. /**
  15. * @brief       GPIO PB IRQ
  16. *
  17. * @param       None
  18. *
  19. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  20. *
  21. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The PB default IRQ, declared in startup_M451Series.s.
  22. */
  23. void GPB_IRQHandler(void)
  24. {
  25.     /* To check if PB.2 interrupt occurred */
  26.     if(GPIO_GET_INT_FLAG(PB, BIT2))
  27.     {
  28.         GPIO_CLR_INT_FLAG(PB, BIT2);
  29.         printf("PB.2 INT occurred.\n");
  30.     }
  31.     else
  32.     {
  33.         /* Un-expected interrupt. Just clear all PB interrupts */
  34.         PB->INTSRC = PB->INTSRC;
  35.         printf("Un-expected interrupts.\n");
  36.     }
  37. }

  38. /**
  39. * @brief       GPIO PC IRQ
  40. *
  41. * @param       None
  42. *
  43. * @return      None
  44. *
  45. * @details     The PC default IRQ, declared in startup_M451Series.s.
  46. */
  47. void GPC_IRQHandler(void)
  48. {
  49.     /* To check if PC.5 interrupt occurred */
  50.     if(GPIO_GET_INT_FLAG(PC, BIT5))
  51.     {
  52.         GPIO_CLR_INT_FLAG(PC, BIT5);
  53.         printf("PC.5 INT occurred.\n");
  54.     }
  55.     else
  56.     {
  57.         /* Un-expected interrupt. Just clear all PC interrupts */
  58.         PC->INTSRC = PC->INTSRC;
  59.         printf("Un-expected interrupts.\n");
  60.     }
  61. }

  62. void SYS_Init(void)
  63. {

  64.     /*---------------------------------------------------------------------------------------------------------*/
  65.     /* Init System Clock                                                                                       */
  66.     /*---------------------------------------------------------------------------------------------------------*/

  67.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  68.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  69.     /* Wait for HIRC clock ready */
  70.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  71.     /* Select HCLK clock source as HIRC and and HCLK source divider as 1 */
  72.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  73.     /* Enable HXT clock (external XTAL 12MHz) */
  74.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  75.     /* Wait for HXT clock ready */
  76.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  77.     /* Set core clock as PLL_CLOCK from PLL */
  78.     CLK_SetCoreClock(PLL_CLOCK);

  79.     /* Enable UART module clock */
  80.     CLK_EnableModuleClock(UART0_MODULE);

  81.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  82.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  83.     /*---------------------------------------------------------------------------------------------------------*/
  84.     /* Init I/O Multi-function                                                                                 */
  85.     /*---------------------------------------------------------------------------------------------------------*/

  86.     /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
  87.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  88.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  89. }

  90. void UART0_Init()
  91. {
  92.     /*---------------------------------------------------------------------------------------------------------*/
  93.     /* Init UART                                                                                               */
  94.     /*---------------------------------------------------------------------------------------------------------*/
  95.     /* Reset UART module */
  96.     SYS_ResetModule(UART0_RST);

  97.     /* Configure UART0 and set UART0 baud rate */
  98.     UART_Open(UART0, 115200);
  99. }

  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* MAIN function                                                                                           */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. int main(void)
  104. {
  105.     /* Unlock protected registers */
  106.     SYS_UnlockReg();

  107.     /* Init System, peripheral clock and multi-function I/O */
  108.     SYS_Init();

  109.     /* Lock protected registers */
  110.     SYS_LockReg();

  111.     /* Init UART0 for printf */
  112.     UART0_Init();

  113.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  114.     printf("+------------------------------------------------+\n");
  115.     printf("|    GPIO PB.2 and PC.5 Interrupt Sample Code    |\n");
  116.     printf("+------------------------------------------------+\n\n");

  117.     /*-----------------------------------------------------------------------------------------------------*/
  118.     /* GPIO Interrupt Function Test                                                                        */
  119.     /*-----------------------------------------------------------------------------------------------------*/
  120.     printf("PB.2 and PC.5 are used to test interrupt ......\n");

  121.     /* Configure PB.2 as Input mode and enable interrupt by rising edge trigger */
  122.     GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
  123.     GPIO_EnableInt(PB, 2, GPIO_INT_RISING);
  124.     NVIC_EnableIRQ(GPB_IRQn);

  125.     /* Configure PC.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  126.     GPIO_SetMode(PC, BIT5, GPIO_MODE_QUASI);
  127.     GPIO_EnableInt(PC, 5, GPIO_INT_FALLING);
  128.     NVIC_EnableIRQ(GPC_IRQn);

  129.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
  130.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
  131.     GPIO_ENABLE_DEBOUNCE(PB, BIT2);
  132.     GPIO_ENABLE_DEBOUNCE(PC, BIT5);

  133.     /* Waiting for interrupts */
  134.     while(1);
  135. }

  136. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/
yiy 发表于 2017-5-18 22:13 | 显示全部楼层
普通的输出输入模式
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 6 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief    Show how to set GPIO pin mode and use pin data input/output control.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include "stdio.h"
  12. #include "M451Series.h"


  13. #define PLL_CLOCK       72000000


  14. void SYS_Init(void)
  15. {

  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  21.     /* Wait for HIRC clock ready */
  22.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  23.     /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  25.     /* Enable HXT clock (external XTAL 12MHz) */
  26.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  27.     /* Wait for HXT clock ready */
  28.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);

  31.     /* Enable UART module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);

  33.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  34.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init I/O Multi-function                                                                                 */
  37.     /*---------------------------------------------------------------------------------------------------------*/

  38.     /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
  39.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  40.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  41. }

  42. void UART0_Init()
  43. {
  44.     /*---------------------------------------------------------------------------------------------------------*/
  45.     /* Init UART                                                                                               */
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Reset UART module */
  48.     SYS_ResetModule(UART0_RST);

  49.     /* Configure UART0 and set UART0 baud rate */
  50.     UART_Open(UART0, 115200);
  51. }

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /*  Main Function                                                                                          */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. int32_t main(void)
  56. {

  57.     int32_t i32Err, i32TimeOutCnt;

  58.     /* Unlock protected registers */
  59.     SYS_UnlockReg();

  60.     /* Init System, peripheral clock and multi-function I/O */
  61.     SYS_Init();

  62.     /* Lock protected registers */
  63.     SYS_LockReg();

  64.     /* Init UART0 for printf */
  65.     UART0_Init();

  66.     printf("\n\nCPU @ %dHz\n", SystemCoreClock);

  67.     printf("+-------------------------------------------------+\n");
  68.     printf("|    PB.3(Output) and PD.7(Input) Sample Code     |\n");
  69.     printf("+-------------------------------------------------+\n\n");

  70.     /*-----------------------------------------------------------------------------------------------------*/
  71.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  72.     /*-----------------------------------------------------------------------------------------------------*/
  73.     printf("  >> Please connect PB.3 and PD.7 first << \n");
  74.     printf("     Press any key to start test by using [Pin Data Input/Output Control] \n\n");
  75.     getchar();

  76.     /* Configure PB.3 as Output mode and PD.7 as Input mode then close it */
  77.     GPIO_SetMode(PB, BIT3, GPIO_MODE_OUTPUT);
  78.     GPIO_SetMode(PD, BIT7, GPIO_MODE_INPUT);

  79.     i32Err = 0;
  80.     printf("GPIO PB.3(output mode) connect to PD.7(input mode) ......");

  81.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  82.     /* Set PB.3 output pin value is low */
  83.     PB3 = 0;

  84.     /* Set time out counter */
  85.     i32TimeOutCnt = 100;

  86.     /* Wait for PD.7 input pin status is low for a while */
  87.     while(PD7 != 0)
  88.     {
  89.         if(i32TimeOutCnt > 0)
  90.         {
  91.             i32TimeOutCnt--;
  92.         }
  93.         else
  94.         {
  95.             i32Err = 1;
  96.             break;
  97.         }
  98.     }

  99.     /* Set PB.3 output pin value is high */
  100.     PB3 = 1;

  101.     /* Set time out counter */
  102.     i32TimeOutCnt = 100;

  103.     /* Wait for PD.7 input pin status is high for a while */
  104.     while(PD7 != 1)
  105.     {
  106.         if(i32TimeOutCnt > 0)
  107.         {
  108.             i32TimeOutCnt--;
  109.         }
  110.         else
  111.         {
  112.             i32Err = 1;
  113.             break;
  114.         }
  115.     }

  116.     /* Print test result */
  117.     if(i32Err)
  118.     {
  119.         printf("  [FAIL].\n");
  120.     }
  121.     else
  122.     {
  123.         printf("  [OK].\n");
  124.     }

  125.     /* Configure PB.3 and PD.7 to default Quasi-bidirectional mode */
  126.     GPIO_SetMode(PB, BIT3, GPIO_MODE_QUASI);
  127.     GPIO_SetMode(PD, BIT7, GPIO_MODE_QUASI);

  128.     while(1);

  129. }
huangcunxiake 发表于 2017-5-19 10:30 | 显示全部楼层
先学会简单的IO输入输出的操作吧
wahahaheihei 发表于 2017-5-20 16:08 | 显示全部楼层
上面的例子就可以,如果你是一个按键,可以设成输入模式,比如上拉模式,或者下拉模式
yiyigirl2014 发表于 2017-5-20 22:10 | 显示全部楼层
先随便找个单片机的按键教程,看看按键原理吧。
yiyigirl2014 发表于 2017-5-20 22:10 | 显示全部楼层
楼主是学生吗,先去借几本单片机相关的书籍吧,了解一下几本的概念,所有的单片机不管谁家的,都是大差不差的,就是寄存器名字不同而已。
稳稳の幸福 发表于 2017-5-21 09:41 | 显示全部楼层
QQ截图20170521094100.png
一般是这种接法。
稳稳の幸福 发表于 2017-5-21 09:42 | 显示全部楼层
KEY1 与 KEY2 都外接 10K 欧姆的上拉电阻。 这里我们必须要了解为什么要加上拉电阻。
当设置好 KEY1 和 KEY2 引脚为输入模式的时候,I/O 状态表现为高阻状态用于检测外部的电平的变化,如果是高电平的话,必须
要检测到 3.3V 的电压,如果是低电平的话,必须检测到 0V 的电压。倘若如果当前 KEY1 和 KEY2 只是悬空引脚的话,高电平就不
复存在,同时,当按键没有按下的时候,KEY1 与 KEY2 引脚表现为悬空状态,引脚状态是不确定的,容易接受外界的电磁干扰。在
制作工艺为 CMOS 的芯片上,为了防止静电造成损坏,不用的管脚不能悬空,一般接上拉电阻令输入阻抗降低,提供泄荷通路。通
过以上分析,就知道为什么按键硬件电路的设计必须接上上拉电阻的原因了。  

稳稳の幸福 发表于 2017-5-21 09:42 | 显示全部楼层
为了检测引脚的电平,可以将 I/O 引脚设置为输入模式或准双向模式,当前实验,这里选择为输入模式,需要调用库函数
GPIO_SetMode(需要包含 gpio.c),代码如下:
设置 PB0、PE8 引脚为输入模式


/* PB0 引脚初始化为输入模式 */

GPIO_SetMode(PB,BIT0,GPIO_MODE_INPUT);
/* PE8 引脚初始化为输入模式 */
GPIO_SetMode(PE,BIT8,GPIO_MODE_INPUT);

稳稳の幸福 发表于 2017-5-21 09:43 | 显示全部楼层
在单片机的应用中,利用按键实现与用户的交互功能是相当常见的,同时按键的检测也是很讲究的,众所周知,在有键按下后,
数据线上的信号出现一段时间的抖动,然后为低,当按键释放时,信号抖动一段时间后变高,然而这段抖动时间要维持 10ms~50ms,
这个与按键本身的材质有一定的关系,在这个范围内基本上都可以确定的。当前实验,只是验证输入模式,那么按键扫描采用简单的
按键延时消抖去实现。主体代码如下  int32_t main(void)

{
PROTECT_REG
(
/* 系统时钟初始化 */
SYS_Init(PLL_CLOCK);
/* 串口 0 初始化,波特率 115200bps */
UART0_Init(115200);
)
/* PB0 引脚初始化为输入模式 */
GPIO_SetMode(PB,BIT0,GPIO_MODE_INPUT);
/* PE8 引脚初始化为输入模式 */
GPIO_SetMode(PE,BIT8,GPIO_MODE_INPUT);
while(1)
{
/* 检查 KEY1 是否按下 */
if(PB0 ==0)
{
/* 延时 20ms */
Delayms(20);
/* 等待 KEY1 释放 */
while(PB0 == 0);
/* 打印 KEY1 输出信息 */
printf("KEY1 is pressed\r\n");
}
/* 检查 KEY2 是否按下 */
if(PE8 ==0)
{
/* 延时 20ms */
Delayms(20);
/* 等待 KEY2 释放 */
while(PE8 == 0);
/* 打印 KEY2 输出信息 */
printf("KEY2 is pressed\r\n");
}
}
}


稳稳の幸福 发表于 2017-5-21 09:44 | 显示全部楼层
上面的代码就是M451的代码。
你可以在论坛搜索:ARM Cortex-M4微控制器原理与实践【网络版】V1.20
找到这个PDF资料下载。
稳稳の幸福 发表于 2017-5-21 09:45 | 显示全部楼层
643757107 发表于 2017-5-21 10:08 | 显示全部楼层
上面这个朋友给的图就是按下去是低电平,按下去低电平的关键就是平时是高电平,高电平的方法就是上拉电阻。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

3

帖子

0

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