pin_beep_sample.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-15 misonyo first implementation.
  9. */
  10. /*
  11. * 程序清单:这是一个 PIN 设备使用例程
  12. * 例程导出了 pin_beep_sample 命令到控制终端
  13. * 命令调用格式:pin_beep_sample
  14. * 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器
  15. */
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. /* 引脚编号,通过查看驱动文件drv_gpio.c确定 */
  19. #ifndef BEEP_PIN_NUM
  20. #define BEEP_PIN_NUM 37 /* PB2 */
  21. #endif
  22. #ifndef KEY0_PIN_NUM
  23. #define KEY0_PIN_NUM 57 /* PD10 */
  24. #endif
  25. #ifndef KEY1_PIN_NUM
  26. #define KEY1_PIN_NUM 56 /* PD9 */
  27. #endif
  28. void beep_on(void *args)
  29. {
  30. rt_kprintf("turn on beep!\n");
  31. rt_pin_write(BEEP_PIN_NUM, PIN_HIGH);
  32. }
  33. void beep_off(void *args)
  34. {
  35. rt_kprintf("turn off beep!\n");
  36. rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
  37. }
  38. static void pin_beep_sample(void)
  39. {
  40. /* 蜂鸣器引脚为输出模式 */
  41. rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT);
  42. /* 默认低电平 */
  43. rt_pin_write(BEEP_PIN_NUM, PIN_LOW);
  44. /* 按键0引脚为输入模式 */
  45. rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
  46. /* 绑定中断,上升沿模式,回调函数名为beep_on */
  47. rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL);
  48. /* 使能中断 */
  49. rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE);
  50. /* 按键1引脚为输入模式 */
  51. rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
  52. /* 绑定中断,上升沿模式,回调函数名为beep_off */
  53. rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL);
  54. /* 使能中断 */
  55. rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
  56. }
  57. /* 导出到 msh 命令列表中 */
  58. MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);