10.24-驱动-作业

数码管应用层控制

循环依次显示0~9

 驱动代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
int major;
struct class *cls;
struct device *dev;
struct spi_device *spi;
int m74hc595_open(struct inode *inode, struct file *file)
{

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t m74hc595_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    char buf[2] = {};
    copy_from_user(buf, ubuf, sizeof(buf));
    spi_write(spi, buf, sizeof(buf));
    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,
    .write = m74hc595_write,
    .release = m74hc595_close,
};
int m74hc595_probe(struct spi_device *spi_d)
{
    spi = spi_d;
    //注册字符设备
    major = register_chrdev(0, "m74hc595", &fops);
    if (major < 0)
    {
        printk("注册字符设备驱动失败\n");
        return major;
    }
    printk("注册字符设备驱动成功\n");
    //自动创建设备节点
    //向上提交目录
    cls = class_create(THIS_MODULE, "m74hc595");
    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");
    return 0;
}
int m74hc595_remove(struct spi_device *spi)
{
    char buf[] = {0xf, 0x0};
    spi_write(spi, buf, sizeof(buf));
    //销毁设备节点
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    //注销字符设备
    unregister_chrdev(major, "m74hc595");
    return 0;
}

//设备树匹配表
struct of_device_id of_table[] = {
    {.compatible = "hqyj,m74hc595"},
    {},
};
MODULE_DEVICE_TABLE(of, of_table);
//定义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 <sys/ioctl.h>
#include <stdlib.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char const *argv[])
{
    int i, j;
    char msg[2] = {};
    char buf[4] = {0x1, 0x2, 0x4, 0x8};
    char num[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7f, 0x6F};
    int fd = open("/dev/m74hc595", O_RDWR);
    if (fd < 0)
    {
        printf("open err\n");
        exit(-1);
    }
    printf("打开设备文件成功\n");
    for (i = 0, j = 0;; i = (i + 1) % 10, j = (j + 1) % 4)
    {
        msg[0] = buf[j];
        msg[1] = num[i];
        write(fd, msg, sizeof(msg));
        sleep(1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Matlab中的FFT函数进行谐波分析,基于提供的数据,可以按照以下步骤编写程序: 1. 定义给定数据的采样频率 `fs`,根据数据中的时间间隔计算得出,即 `fs = 1 / (时间间隔)`; 2. 对输入数据进行FFT变换,使用fft函数,得到频域数据 `Y`; 3. 计算基波幅值 `fundamental`,即第一个频率分量的幅值,即 `abs(Y(1))`; 4. 计算2到11次谐波幅值 `harmonics`,即第2到11个频率分量的幅值,即 `abs(Y(2:11))`; 5. 输出结果。 下面是一个可能的Matlab程序: ``` % 定义时间间隔和数据 t = [0 0.000208333 0.000416667 0.000625 0.000833333 0.001041667 0.00125 0.001458333 0.001666667 0.001875 0.002083333 0.002291667 0.0025 0.002708333 0.002916667 0.003125 0.003333333 0.003541667 0.00375 0.003958333 0.004166667 0.004375 0.004583333 0.004791667 0.005 0.005208333 0.005416667 0.005625 0.005833333 0.006041667 0.00625 0.006458333 0.006666667 0.006875 0.007083333 0.007291667 0.0075 0.007708333 0.007916667 0.008125 0.008333333 0.008541667 0.00875 0.008958333 0.009166667 0.009375 0.009583333 0.009791667 0.01 0.010208333 0.010416667 0.010625 0.010833333 0.011041667 0.01125 0.011458333 0.011666667 0.011875 0.012083333 0.012291667 0.0125]; y = [15.92 15.68 15.52 15.04 14.6 14.08 12.96 12.24 10.96 9.76 8.28 6.8 5.56 3.24 1.72 -0.76 -3 -5.24 -6.76 -8.4 -10.24 -11.32 -12.72 -14.4 -15.2 -16.28 -16.6 -17.28 -17.44 -17.24 -17.32 -17.04 -16.84 -16.04 -15.88 -15.16 -14.08 -13.32 -12.04 -10.88 -10.16 -7.96 -6.4 -4.72 -2.36 -0.48 2.08 3.8 5.92 7.32 8.88 10.36 11.64 12.88 13.72 14.84 15.44 15.76 15.96 16.04 15.52]; % 计算采样频率 fs = 1 / (t(2) - t(1)); % 计算FFT变换 Y = fft(y); % 计算基波幅值 fundamental = abs(Y(1)); % 计算2到11次谐波幅值 harmonics = abs(Y(2:11)); % 输出结果 fprintf('基波幅值: %.2f\n', fundamental); fprintf('2到11次谐波幅值: %.2f ', harmonics); fprintf('\n'); ``` 注意:这个程序假设您提供的数据是一个完整的周期数据,如果不是一个完整的周期数据需要进行预处理,例如使用窗函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值