pin_beep_sample.c
/* * Copyright (c) 2006-2018, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2018-08-15 misonyo first implementation. *//* * 程序清单:这是一个 PIN 设备使用例程 * 例程导出了 pin_beep_sample 命令到控制终端 * 命令调用格式:pin_beep_sample * 程序功能:通过按键控制蜂鸣器对应引脚的电平状态控制蜂鸣器*/#include <rtthread.h>#include <rtdevice.h>/* 引脚编号,通过查看驱动文件drv_gpio.c确定 */#ifndef BEEP_PIN_NUM #define BEEP_PIN_NUM 35 /* PB0 */#endif#ifndef KEY0_PIN_NUM #define KEY0_PIN_NUM 55 /* PD8 */#endif#ifndef KEY1_PIN_NUM #define KEY1_PIN_NUM 56 /* PD9 */#endifvoid beep_on(void *args){ rt_kprintf("turn on beep!\n"); rt_pin_write(BEEP_PIN_NUM, PIN_HIGH);}void beep_off(void *args){ rt_kprintf("turn off beep!\n"); rt_pin_write(BEEP_PIN_NUM, PIN_LOW);}static void pin_beep_sample(void){ /* 蜂鸣器引脚为输出模式 */ rt_pin_mode(BEEP_PIN_NUM, PIN_MODE_OUTPUT); /* 默认低电平 */ rt_pin_write(BEEP_PIN_NUM, PIN_LOW); /* 按键0引脚为输入模式 */ rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP); /* 绑定中断,上升沿模式,回调函数名为beep_on */ rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_on, RT_NULL); /* 使能中断 */ rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE); /* 按键1引脚为输入模式 */ rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP); /* 绑定中断,上升沿模式,回调函数名为beep_off */ rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, beep_off, RT_NULL); /* 使能中断 */ rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);}/* 导出到 msh 命令列表中 */MSH_CMD_EXPORT(pin_beep_sample, pin beep sample);