数码管流水灯

该文章展示了一个Linux内核模块,用于驱动SPI设备m74hc595。它包含了设备的打开、关闭和ioctl操作,用于从用户空间发送数据到SPI总线。测试代码显示了如何与设备交互,改变并发送SPI消息。
摘要由CSDN通过智能技术生成

源代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/spi/spi.h>
#include "spi.h"

#define CNAME "myspi"
int major;
struct class *cls;
struct device *dev;
struct spi_device *spi1;

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

long m74hc595_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
    int ret;
    char buf[2]={};
    switch(cmd)
    {
        case SEND_MSG:
        {
            ret=copy_from_user((void *)buf,(void *)arg,sizeof(buf));
            if(ret)
            {
                printk("给用户拷贝失败\n");
                return -EINVAL;
            }
            spi_write(spi1,buf,sizeof(buf));
        }
        break;
    }
    return 0;
}

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

struct file_operations fops={
    .open=m74hc595_open,
    .unlocked_ioctl=m74hc595_ioctl,
    .release=m74hc595_close,
};
int m74hc595_probe(struct spi_device *spi)
{
    spi1=spi;
    major=register_chrdev(0,"m74hc595",&fops);
    if(major<0)
    {
        printk("注册字符设备失败\n");
        return major;
    }
    printk("注册字符设备成功\n");
 
    //自动创建设备节点
    cls=class_create(THIS_MODULE,"si7006");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"m74hc595");
    if(IS_ERR(dev))
    {
        printk("向上提交节点信息失败\n");
        return PTR_ERR(dev);
    }  
    printk("向上提交节点信息成功\n");
    printk("%s:%d\n",__FILE__,__LINE__);
    return 0;
}
int m74hc595_remove(struct spi_device *spi)
{
    //销毁向上层提交设备信息
    device_destroy(cls,MKDEV(major,0));
    //销毁向上层提交目录信息
    class_destroy(cls);
    
    //1.注销字符设备驱动
    unregister_chrdev(major,CNAME);
     printk("%s:%d\n",__FILE__,__LINE__);
     return 0;
}

//设备树匹配表
struct of_device_id of_table[]={
    {.compatible="hqyj,m74hc595"},
    {},
};
//定义SPI对象并且初始化
struct spi_driver m74hc595 ={ 
    .probe=m74hc595_probe,
    .remove=m74hc595_remove,
    .driver={
        .name="m74hc595",
        .of_match_table=of_table,
    },
};

module_spi_driver(m74hc595); 
MODULE_LICENSE("GPL"); 

测试代码

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <sys/ioctl.h>  
#include <arpa/inet.h>
#include"spi.h"
int main(int argc, char const *argv[])
{
    char num[10] = {0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f,0x6f};
    char buf[2];
    int index=0;
    int fd=open("/dev/m74hc595",O_RDWR);
    if(fd<0)
    {
        printf("设备文件打开失败\n");
        exit(-1);
    }
    printf("设备文件打开成功\n");

    while(1)
    {
       for(int i=1;i<9;i=i*2)
        {                            
            buf[0]=i;
            if(index>9)
            {
                index=0;
            }
                buf[1]=num[index];
                ioctl(fd,SEND_MSG,buf);
                sleep(1);
                buf[1]=0;
                ioctl(fd,SEND_MSG,buf);
                index++;
                sleep(1);
        }
    }
    close(fd);
    return 0;
}

头文件

#ifndef __IIC_H__
#define __IIC_H__
#define SEND_MSG _IOW('c',0,short)
#endif

结果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一段代码示例,实现51单片机的数码管流水灯效果。请注意,以下代码示例仅供参考,具体实现方式可能因硬件环境、编程语言等因素而有所不同。 ``` #include <reg52.h> // 定义各个数码管对应的引脚 sbit DIG1 = P2^2; sbit DIG2 = P2^3; sbit DIG3 = P2^4; sbit DIG4 = P2^5; // 定义数字0-9的各个段的状态 unsigned char digitSegments[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x98}; void delay(unsigned int ms) { // 延时函数,延时时间为ms毫秒 unsigned int i, j; for (i = 0; i < ms; i++) { for (j = 0; j < 1141; j++); } } void main() { unsigned char i, j; while (1) { for (i = 0; i < 10; i++) { // 显示当前数字 DIG1 = 1; DIG2 = 0; DIG3 = 0; DIG4 = 0; P0 = digitSegments[i]; delay(20); // 显示下一个数字 DIG1 = 0; DIG2 = 1; DIG3 = 0; DIG4 = 0; P0 = digitSegments[(i + 1) % 10]; delay(20); // 显示下下一个数字 DIG1 = 0; DIG2 = 0; DIG3 = 1; DIG4 = 0; P0 = digitSegments[(i + 2) % 10]; delay(20); // 显示下下下一个数字 DIG1 = 0; DIG2 = 0; DIG3 = 0; DIG4 = 1; P0 = digitSegments[(i + 3) % 10]; delay(20); } } } ``` 这段代码使用51单片机实现了数码管流水灯效果。在`main()`函数中,通过循环依次显示0-9的数字,并且每次显示3个数字,形成流水灯效果。在循环中,先显示当前数字,然后通过设置不同的数码管引脚,依次显示下一个数字、下下一个数字和下下下一个数字。每次显示完一个数字后,会等待一段时间,再显示下一个数字,循环往复,实现了数码管流水灯效果。在这段代码中,`delay()`函数用于延时,确保每个数字的显示时间一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值