驱动 11/30

I2C实验,并设定温度值超过led灯亮

#ifndef __SI7006_H__
#define __SI7006_H__

#define GET_TEM  _IOR('m',0,int)
#define GET_HUM  _IOR('m',1,int)
//封装点灯命令码
#define LED_ON _IOW('l',1,int)
#define LED_OFF _IOW('l',0,int)

#endif
#include <linux/init.h>
#include <linux/module.h>
#include<linux/i2c.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include"si7006.h"
struct i2c_client *tclient;
//设备号
int major;
struct class *cls;
struct device *dev;
/***************led相关*****************/
//定义指针指向获取的设备树结点信息
struct device_node *node;
//用于接收gpio编号
struct gpio_desc * gpiono;
//获取温湿度的函数
int i2c_read_hum_tem(unsigned char reg)
{
    int ret;
    //读数据的封装
    char r_buf[]={reg};
    unsigned short val; //读出的数据
    struct i2c_msg r_msg[]={
        [0]={
            .addr=tclient->addr,
            .flags=0,
            .len=1,
            .buf=r_buf,
        },
        [1]={
            .addr=tclient->addr,
            .flags=1,
            .len=2,
            .buf=(char *)&val,
        },
    };
    //消息的传输函数
    ret=i2c_transfer(tclient->adapter,r_msg,ARRAY_SIZE(r_msg));
    if(ret!=ARRAY_SIZE(r_msg))
    {
        printk("获取温湿度数据失败\n");
        return EAGAIN;
    }
    return val;
}
int si7006_open(struct inode *inode, struct file *file)
{
    printk("this is open\n");
    return 0;
}
ssize_t si7006_read(struct file *file, char __user *ubuf, size_t size, loff_t *loffs)
{
    return size;
}
ssize_t si7006_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
{
    return size;
}
long si7006_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int tem,hum;
    int ret;
    switch (cmd)
    {
    case GET_HUM:   //湿度指令码(0xe5)
            hum=i2c_read_hum_tem(0xe5);
            ret=copy_to_user((void *)arg,(void *)&hum,sizeof(int));
            if(ret)
            {
                printk("传输湿度数据失败\n");
                return EINVAL;
            }

        break;
    case GET_TEM:   //温度指令码(0xe3)
            tem=i2c_read_hum_tem(0xe3);
            ret=copy_to_user((void *)arg,(void *)&tem,sizeof(int));
            if(ret)
            {
                printk("传输温度数据失败\n");
                return EINVAL;
            }
        break;
    case LED_ON:
            gpiod_set_value(gpiono,1);
        break;
    case LED_OFF:
            gpiod_set_value(gpiono,0);
        break;       
        
    default:
        break;
    }
    return 0;
}

int si7006_close(struct inode *inode, struct file *file)
{
     printk("this is close\n");
    return 0;
}
//定义fops
struct file_operations fops={
        .open=si7006_open,
        .read=si7006_read,
        .write=si7006_write,
        .unlocked_ioctl=si7006_ioctl,
        .release=si7006_close,
};
 //匹配成功后执行probe
int si7006_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    tclient=client;
   //注册字符设备驱动
    major=register_chrdev(0,"si7006",&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,"si7006");
    if(IS_ERR(dev))
    {
        printk("向上提交设备信息失败\n");
        return PTR_ERR(dev);
    }
    printk("向上提交设备信息成功\n");
    /*************************led相关*****************************/
    node=of_find_node_by_name(NULL,"myleds");
    if(node==NULL)
    {
        printk("通过名字解析设备树失败\n");
        return -EFAULT;
    }
    printk("通过名字解析设备树成功\n");
    //获取gpio编号并申请
    gpiono=gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpiono编号失败\n");
        return PTR_ERR(gpiono);
    }
    printk("获取gpio编号成功\n");
    //设置管脚为输出
    gpiod_direction_output(gpiono,0);
    return 0;
}
   //设备分离后执行remove
int si7006_remove(struct i2c_client *client)
{
    //注销字符设备驱动
    unregister_chrdev(major,"si7006");
    //卸载前熄灭
    gpiod_set_value(gpiono,0);
    //释放gpiono
    gpiod_put(gpiono);
    return 0;
}
//定义设备树匹配表
struct of_device_id oftable[]=
{
    {.compatible="hqyj,si7006",},
    {}
};
MODULE_DEVICE_TABLE(of,oftable);
//定义对象并初始化
struct i2c_driver si7006={
    .probe=si7006_probe,
    .remove=si7006_remove,
    .driver={
        .name="tem_hum_driver",
        .of_match_table=oftable,
    },
};
//一键注册宏
module_i2c_driver(si7006);
MODULE_LICENSE("GPL");
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include<stdlib.h>
#include <arpa/inet.h>
#include "si7006.h" 

int main(int argc, char const *argv[])
{
    
    int tem,hum;
   
    int fd;
    fd=open("/dev/si7006",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        int led1=1;
        ioctl(fd,GET_TEM,&tem);
        ioctl(fd,GET_HUM,&hum);
        //字节序的转换
        tem=ntohs(tem);
        hum=ntohs(hum);
        //温湿度计算
        hum=125.0*hum/65536-6;
        tem=175.72*tem/65536-46.85;
        printf("温度:%d  湿度:%d\n",tem,hum);
        if(tem>18)
        {
            printf("温度超出,警告!!!!\n");
            ioctl(fd,LED_ON,&led1);
        }
        else
        {
            printf("温度正常\n");
            ioctl(fd,LED_OFF,&led1);
        }
        sleep(1);
    }
    
 
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用STM32驱动DHT11的示例代码: ``` #include "stm32f10x.h" #define DHT11_PIN GPIO_Pin_0 #define DHT11_PORT GPIOA #define DHT11_TIMEOUT 10000 void delay_us(uint32_t us) { uint32_t count = us * (SystemCoreClock / 1000000) / 5; while (count--); } void DHT11_Start(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = DHT11_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(DHT11_PORT, &GPIO_InitStructure); GPIO_SetBits(DHT11_PORT, DHT11_PIN); delay_us(1000); GPIO_ResetBits(DHT11_PORT, DHT11_PIN); delay_us(18000); GPIO_SetBits(DHT11_PORT, DHT11_PIN); delay_us(30); } uint8_t DHT11_Check_Response(void) { uint8_t count = 0; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = DHT11_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(DHT11_PORT, &GPIO_InitStructure); while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET) { count++; if (count > DHT11_TIMEOUT) return 1; delay_us(1); } count = 0; while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == RESET) { count++; if (count > DHT11_TIMEOUT) return 1; delay_us(1); } count = 0; while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET) { count++; if (count > DHT11_TIMEOUT) return 1; delay_us(1); } return 0; } uint8_t DHT11_Read_Bit(void) { uint8_t count = 0; while (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == RESET) { count++; if (count > DHT11_TIMEOUT) return 1; delay_us(1); } delay_us(40); if (GPIO_ReadInputDataBit(DHT11_PORT, DHT11_PIN) == SET) return 1; else return 0; } uint8_t DHT11_Read_Byte(void) { uint8_t i, data = 0; for (i = 0; i < 8; i++) { data <<= 1; if (DHT11_Read_Bit()) data |= 1; } return data; } uint8_t DHT11_Read_Data(uint8_t *humidity, uint8_t *temperature) { uint8_t i, data[5]; DHT11_Start(); if (DHT11_Check_Response()) { return 1; } for (i = 0; i < 5; i++) { data[i] = DHT11_Read_Byte(); } *humidity = data[0]; *temperature = data[2]; return 0; } int main(void) { uint8_t humidity, temperature; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); while (1) { if (DHT11_Read_Data(&humidity, &temperature) == 0) { //读取到温湿度数据,进行处理 } delay_us(2000000); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值