网站建设策划书是有谁编写的,设计网站推荐视频,南京网站建设招聘,那个外贸网站做的好开发板#xff1a;STM32MP157A
温湿度传感器#xff1a;si7006
显示器#xff08;数码管#xff09;#xff1a;m74hc595
遇到的问题#xff1a;循环采集温湿度传感器数值#xff0c;并将数值发送给数码管的时候两者存在竞态关系#xff0c;导致数码管显示亮度很暗 …开发板STM32MP157A
温湿度传感器si7006
显示器数码管m74hc595
遇到的问题循环采集温湿度传感器数值并将数值发送给数码管的时候两者存在竞态关系导致数码管显示亮度很暗
解决办法采用多线程或者多进程解决内核竞态问题
驱动代码
#include linux/init.h
#include linux/module.h
#include linux/i2c.h
#include linux/fs.h
#include linux/device.h
#include head.hunsigned int major;
struct class *cls;
struct device *dev;
struct i2c_client *client1;//读取温湿度的函数
int i2c_read_hum_tem(char reg)
{short value;char r_buf[]{reg};int ret;//封装消息struct i2c_msg r_msg[]{[0]{.addrclient1-addr,.flags0,//先写.lensizeof(r_buf),.bufr_buf,},[1]{.addrclient1-addr,.flags1,.len2,.buf(char *)value,},};//将消息传送reti2c_transfer(client1-adapter,r_msg,2);if(ret!2){printk(消息传输失败\n);return -EIO;}return value;//将读取到的温湿度返回
}int si7006_open(struct inode *inode,struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);return 0;
}long si7006_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{int tem,hum,ret;switch(cmd){case GET_HUM://读取湿度//读取湿度的逻辑humi2c_read_hum_tem(0xE5);retcopy_to_user((void *)arg,hum,4);//int类型4个字节if(ret){printk(copy_to_user error\n);return ret;}break;case GET_TEM://读取温度//读取温度的逻辑temi2c_read_hum_tem(0xE3);retcopy_to_user((void *)arg,tem,4);if(ret){printk(copy_to_user error\n);return ret;}break;}return 0;
}int si7006_close(struct inode *inode,struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__); return 0;
}//操作方法结构体
struct file_operations fops{.opensi7006_open,.unlocked_ioctlsi7006_ioctl,.releasesi7006_close,
};//给对象分配空间并且初始化
int i2c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{client1client;//字符设备驱动的注册majorregister_chrdev(0,si7006,fops);if(major0){printk(register_chrdev failed\n); return major;}printk(register_chrdev success\n);//设备节点的创建//向上提交目录clsclass_create(THIS_MODULE,si7006);if(IS_ERR(cls)){printk(向上提交目录失败\n);return -PTR_ERR(cls);}printk(向上提交目录成功\n);//向上提交设备节点devdevice_create(cls,NULL,MKDEV(major,0),NULL,si7006);if(IS_ERR(dev)){printk(向上提交设备节点失败\n);return -PTR_ERR(dev); }printk(向上提交设备节点成功\n);return 0;
}int i2c_remove(struct i2c_client *client)
{//销毁节点device_destroy(cls,MKDEV(major,0));//销毁目录class_destroy(cls);//注销字符设备驱动unregister_chrdev(major,si7006);return 0;
}
//定义设备树匹配的表
struct of_device_id oftable[]{{.compatiblehqyj,si7006,},{},
};//分配IIC驱动信息对象
struct i2c_driver i2c_drv{.probei2c_probe,.removei2c_remove,.driver{.namesi7006,.of_match_tableoftable,},
};module_i2c_driver(i2c_drv);
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 head.h
#include pthread.hint number1000;
int *pnumber;
int flag 0;void *shuma_callback(void *arg)
{while(1){//将温湿度传给数码管ioctl显示,发送数据ioctl(*(int *)arg,GET_SHUMA,p);} pthread_exit(NULL);
}int main(int argc,char const *argv[])
{int tem,hum;float tem1,hum1;int fd_i2copen(/dev/si7006,O_RDWR);if(fd_i2c0){printf(设备文件打开失败\n);exit(-1);}//温湿度值采用数码管显示int fd_spiopen(/dev/m74hc595,O_RDWR);if(fd_spi0){printf(设备文件打开失败\n);exit(-1);}pthread_t tid_1;if(pthread_create(tid_1,NULL,shuma_callback,(void *)fd_spi)!0){fprintf(stderr,pthread_create failed__%d__\n,__LINE__);return -1;}pthread_detach(tid_1);//分离线程while(1){//获取数据ioctl(fd_i2c,GET_HUM,hum);ioctl(fd_i2c,GET_TEM,tem);printf(hum%d,tem%d\n,hum,tem);//大小端转换humntohs(hum);temntohs(tem);//计算数据hum1125.0*hum/65536-6;tem1175.72*tem/65536-46.85;printf(hum1%d,tem1%d\n,hum1,tem1);number(int)hum1 * 100 (int)tem1;sleep(1);}pthread_join(tid_1,NULL);//阻塞等待tid_1线程退出return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_H__#define GET_SHUMA _IOR(m,2,int)//获取数码管的功能码
#define GET_HUM _IOR(m,1,int)//获取湿度的功能码
#define GET_TEM _IOR(m,0,int)//获取温度的功能码#endif 文章转载自: http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.baguiwei.com.gov.cn.baguiwei.com http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.qhfdl.cn.gov.cn.qhfdl.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.pqqxc.cn.gov.cn.pqqxc.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.bntgy.cn.gov.cn.bntgy.cn http://www.morning.lpnpn.cn.gov.cn.lpnpn.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.hsxkq.cn.gov.cn.hsxkq.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.tkqzr.cn.gov.cn.tkqzr.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.tgyqq.cn.gov.cn.tgyqq.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.dwztj.cn.gov.cn.dwztj.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.nqrfd.cn.gov.cn.nqrfd.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.fglyb.cn.gov.cn.fglyb.cn http://www.morning.hsrch.cn.gov.cn.hsrch.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.tdmr.cn.gov.cn.tdmr.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn