三个按键实现按键中断,key1-> led1 key2->led2 key3->led3 按键按一下灯亮,再按一下灯灭

mykey.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>


#define CNAME "myled"


struct device_node *node;

int ret;

struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;

//接受软中断号
unsigned int irqno1,irqno2,irqno3;

//中断处理函数
irqreturn_t irq_handler1(int irqno,void *dev)
{
    gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
    return IRQ_HANDLED;
}

irqreturn_t irq_handler2(int irqno,void *dev)
{
    gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
    return IRQ_HANDLED;
}

irqreturn_t irq_handler3(int irqno,void *dev)
{
    gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
    return IRQ_HANDLED;
}

// open函数
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// close函数
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

//操作方法结构体初始化
const struct file_operations fops = {
    .open = mycdev_open,
//    .unlocked_ioctl = ioctl,
    .release = mycdev_close,
};

//入口函数
static int __init mycdev_init(void)
{  

    //通过名字获取设备树节点信息
    node = of_find_node_by_name(NULL, "myled");
    if (node == NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");

    //获取并申请gpio编号
    gpiono1 = gpiod_get_from_of_node(node, "myled1", 0, GPIOD_OUT_LOW, NULL);
    gpiono2 = gpiod_get_from_of_node(node, "myled2", 0, GPIOD_OUT_LOW, NULL);
    gpiono3 = gpiod_get_from_of_node(node, "myled3", 0, GPIOD_OUT_LOW, NULL);

    printk("获取gpio编号成功\n");

    //通过名字获取设备树节点信息
    node = of_find_node_by_name(NULL, "myirq");
    if (node == NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT; 
    }
    printk("成功解析到设备树节点\n");

    //获取软中断号1
    irqno1 = irq_of_parse_and_map(node,2);
    if(irqno1 == 0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }

    //终端注册进内核
    ret = request_irq(irqno1,irq_handler1,IRQF_TRIGGER_FALLING,"key1",NULL);
    if(ret)
    {
        printk("注册软中断号失败\n");
        return EINVAL;
    }

     //获取软中断号2
    irqno2 = irq_of_parse_and_map(node,0);
    if(irqno2 == 0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }

    //终端注册进内核
    ret = request_irq(irqno2,irq_handler2,IRQF_TRIGGER_FALLING,"key2",NULL);
    if(ret)
    {
        printk("注册软中断号失败\n");
        return EINVAL;
    }
    printk("注册软中断成功\n");

     //获取软中断号3
    irqno3 = irq_of_parse_and_map(node,1);
    if(irqno3 == 0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }
    printk("获取软中断成功\n");

    //终端注册进内核
    ret = request_irq(irqno3,irq_handler3,IRQF_TRIGGER_FALLING,"key3",NULL);
    if(ret)
    {
        printk("注册软中断号失败\n");
        return EINVAL;
    }
    printk("注册软中断成功\n");

    return 0;

}

//出口函数
static void __exit mycdev_exit(void)
{
    free_irq(irqno1,NULL);
    free_irq(irqno2,NULL);
    free_irq(irqno3,NULL);

    // 6.释放申请的gpio编号
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
}

module_init(mycdev_init);
module_exit(mycdev_exit);
// GPL协议
MODULE_LICENSE("GPL");  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题需要根据具体的硬件平台进行编程实现,以下是一个基于Arduino Uno开发板的示例代码: ```C++ const int LED_PIN[8] = {2, 3, 4, 5, 6, 7, 8, 9}; const int KEY1_PIN = 2; const int KEY2_PIN = 3; volatile bool isKey1Pressed = false; volatile bool isKey2Pressed = false; void setup() { // 初始化LED引脚为输出模式 for (int i = 0; i < 8; i++) { pinMode(LED_PIN[i], OUTPUT); } // 初始化按键引脚为输入模式 pinMode(KEY1_PIN, INPUT_PULLUP); pinMode(KEY2_PIN, INPUT_PULLUP); // 绑定中断服务函数 attachInterrupt(digitalPinToInterrupt(KEY1_PIN), key1Interrupt, FALLING); attachInterrupt(digitalPinToInterrupt(KEY2_PIN), key2Interrupt, FALLING); // 初始状态下所有LED起 allLedOn(); } void loop() { if (isKey1Pressed) { ledFromBottomToTop(); isKey1Pressed = false; } if (isKey2Pressed) { ledFromTopToBottom(); isKey2Pressed = false; } delay(10); } void allLedOn() { for (int i = 0; i < 8; i++) { digitalWrite(LED_PIN[i], HIGH); } } void ledFromBottomToTop() { for (int i = 0; i < 8; i++) { digitalWrite(LED_PIN[i], LOW); delay(100); } } void ledFromTopToBottom() { for (int i = 7; i >= 0; i--) { digitalWrite(LED_PIN[i], LOW); delay(100); } } void key1Interrupt() { isKey1Pressed = true; } void key2Interrupt() { isKey2Pressed = true; } ``` 解释一下代码的实现逻辑: 首先定义了8个LED的引脚号以及两个按键的引脚号。在`setup()`函数中,将LED引脚设置为输出模式,按键引脚设置为输入模式,并通过`attachInterrupt()`函数绑定了两个外部中断服务函数。 `loop()`函数中检测是否有按键按下,如果有则执行相应的LED操作,并将按键状态标记清除。 三个LED函数分别为:全部LED起、从下到上依次点、从上到下依次点。这里使用`digitalWrite()`函数控制LED的开关,`delay()`函数控制LED的时间间隔。 两个中断服务函数分别为按键Key1和Key2的中断服务函数,用于标记按键状态。这里使用了`volatile`关键字,以确保在中断服务函数中修改的变量能够在主程序中正确地读取。 总体来说,这个程序的逻辑比较简单,主要涉及到了Arduino的GPIO控制和中断服务函数的使用。如果使用其他硬件平台,可能需要相应地修改引脚号和GPIO控制函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值