GY906 温度的传感器
目录
- 外形
- 注意事项
- 代码
- main
- .c
- .h
- 实际效果
外形
注意事项
引脚设置为开漏输出
模块可以3.3v供电
代码
代码来自STM32 单片机 GY-906 MLX90614 红外温度读取
这位大哥的
main
temp=SMBus_ReadTemp();printf("ζÈֵΪ£º%.2f\r\n",temp);
HAL_Delay(100);
.c
#include "mlx90614.h"/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK 0
#define NACK 1
#define SA 0x00 //Slave address x
#define RAM_ACCESS 0x00 //RAM access command RAM´æÈ¡ÃüÁî
#define EEPROM_ACCESS 0x20 //EEPROM access command EEPROM´æÈ¡ÃüÁî
#define RAM_TOBJ1 0x07 //To1 address in the eeprom Ä¿±ê1ζÈ,¼ì²âµ½µÄºìÍâÎÂ¶È -70.01 ~ 382.19¶È#define SMBUS_PORT GPIOB //PB¶Ë¿Ú(¶Ë¿ÚºÍÏÂÃæµÄÁ½¸öÕë½Å¿É×Ô¶¨Òå)
#define SMBUS_SCK GPIO_PIN_6 //PB6£ºSCL
#define SMBUS_SDA GPIO_PIN_7 //PB7£ºSDA2#define RCC_APB2Periph_SMBUS_PORT RCC_APB2Periph_GPIOB#define SMBUS_SCK_H() SMBUS_PORT->BSRR = SMBUS_SCK //Öøߵçƽ
#define SMBUS_SCK_L() SMBUS_PORT->BRR = SMBUS_SCK //Öõ͵çƽ
#define SMBUS_SDA_H() SMBUS_PORT->BSRR = SMBUS_SDA
#define SMBUS_SDA_L() SMBUS_PORT->BRR = SMBUS_SDA#define SMBUS_SDA_PIN() SMBUS_PORT->IDR & SMBUS_SDA //¶ÁÈ¡Òý½Åµçƽ/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*//*******************************************************************************
* Function Name : SMBus_StartBit
* Description : Generate START condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StartBit(void)
{SMBUS_SDA_H(); // Set SDA lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SCK_H(); // Set SCL lineSMBus_Delay(5); // Generate bus free time between StopSMBUS_SDA_L(); // Clear SDA lineSMBus_Delay(5); // Hold time after (Repeated) Start// Condition. After this period, the first clock is generated.//(Thd:sta=4.0us min)ÔÚSCK=1ʱ£¬¼ì²âµ½SDAÓÉ1µ½0±íʾͨÐÅ¿ªÊ¼£¨Ï½µÑØ£©SMBUS_SCK_L(); // Clear SCL lineSMBus_Delay(5); // Wait a few microseconds
}/*******************************************************************************
* Function Name : SMBus_StopBit
* Description : Generate STOP condition on SMBus
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_StopBit(void)
{SMBUS_SCK_L(); // Clear SCL lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SDA_L(); // Clear SDA lineSMBus_Delay(5); // Wait a few microsecondsSMBUS_SCK_H(); // Set SCL lineSMBus_Delay(5); // Stop condition setup time(Tsu:sto=4.0us min)SMBUS_SDA_H(); // Set SDA lineÔÚSCK=1ʱ£¬¼ì²âµ½SDAÓÉ0µ½1±íʾͨÐŽáÊø£¨ÉÏÉýÑØ£©
}/*******************************************************************************
* Function Name : SMBus_SendByte
* Description : Send a byte on SMBus
* Input : Tx_buffer
* Output : None
* Return : None
*******************************************************************************/
u8 SMBus_SendByte(u8 Tx_buffer)
{u8 Bit_counter;u8 Ack_bit;u8 bit_out;for(Bit_counter=8; Bit_counter; Bit_counter--){if (Tx_buffer&0x80){bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out}else{bit_out=0; // else clear bit_out}SMBus_SendBit(bit_out); // Send the current bit on SDATx_buffer<<=1; // Get next bit for checking}Ack_bit=SMBus_ReceiveBit(); // Get acknowledgment bitreturn Ack_bit;
}/*******************************************************************************
* Function Name : SMBus_SendBit
* Description : Send a bit on SMBus 82.5kHz
* Input : bit_out
* Output : None
* Return : None
*******************************************************************************/
void SMBus_SendBit(u8 bit_out)
{if(bit_out==0){SMBUS_SDA_L();}else{SMBUS_SDA_H();}SMBus_Delay(2); // Tsu:dat = 250ns minimumSMBUS_SCK_H(); // Set SCL lineSMBus_Delay(6); // High Level of Clock PulseSMBUS_SCK_L(); // Clear SCL lineSMBus_Delay(3); // Low Level of Clock Pulse
// SMBUS_SDA_H(); // Master release SDA line ,return;
}/*******************************************************************************
* Function Name : SMBus_ReceiveBit
* Description : Receive a bit on SMBus
* Input : None
* Output : None
* Return : Ack_bit
*******************************************************************************/
u8 SMBus_ReceiveBit(void)
{u8 Ack_bit;SMBUS_SDA_H(); //Òý½Å¿¿Íⲿµç×èÉÏÀ£¬µ±×÷ÊäÈëSMBus_Delay(2); // High Level of Clock PulseSMBUS_SCK_H(); // Set SCL lineSMBus_Delay(5); // High Level of Clock Pulseif (SMBUS_SDA_PIN()){Ack_bit=1;}else{Ack_bit=0;}SMBUS_SCK_L(); // Clear SCL lineSMBus_Delay(3); // Low Level of Clock Pulsereturn Ack_bit;
}/*******************************************************************************
* Function Name : SMBus_ReceiveByte
* Description : Receive a byte on SMBus
* Input : ack_nack
* Output : None
* Return : RX_buffer
*******************************************************************************/
u8 SMBus_ReceiveByte(u8 ack_nack)
{u8 RX_buffer;u8 Bit_Counter;for(Bit_Counter=8; Bit_Counter; Bit_Counter--){if(SMBus_ReceiveBit()) // Get a bit from the SDA line{RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_bufferRX_buffer |=0x01;}else{RX_buffer <<= 1; // If the bit is LOW save 0 in RX_bufferRX_buffer &=0xfe;}}SMBus_SendBit(ack_nack); // Sends acknowledgment bitreturn RX_buffer;
}/*******************************************************************************
* Function Name : SMBus_Delay
* Description : ÑÓʱ Ò»´ÎÑ»·Ô¼1us
* Input : time
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{delay_us(time);
}/*******************************************************************************
* Function Name : SMBus_Init
* Description : SMBus³õʼ»¯
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SMBus_Init()
{SMBUS_SCK_H();SMBUS_SDA_H();
}/******************************************************************************** Function Name : SMBus_ReadMemory* Description : READ DATA FROM RAM/EEPROM* Input : slaveAddress, command* Output : None* Return : Data
*******************************************************************************/
u16 SMBus_ReadMemory(u8 slaveAddress, u8 command)
{u16 data; // Data storage (DataH:DataL)u8 Pec; // PEC byte storageu8 DataL=0; // Low data byte storageu8 DataH=0; // High data byte storageu8 arr[6]; // Buffer for the sent bytesu8 PecReg; // Calculated PEC byte storageu8 ErrorCounter; // Defines the number of the attempts for communication with MLX90614ErrorCounter=0x00; // Initialising of ErrorCounterslaveAddress <<= 1; //2-7λ±íʾ´Ó»úµØÖ·do{repeat:SMBus_StopBit(); //If slave send NACK stop comunication--ErrorCounter; //Pre-decrement ErrorCounterif(!ErrorCounter) //ErrorCounter=0?{break; //Yes,go out from do-while{}}SMBus_StartBit(); //Start conditionif(SMBus_SendByte(slaveAddress))//Send SlaveAddress ×îµÍλWr=0±íʾ½ÓÏÂÀ´Ð´ÃüÁî{goto repeat; //Repeat comunication again}if(SMBus_SendByte(command)) //Send command{goto repeat; //Repeat comunication again}SMBus_StartBit(); //Repeated Start conditionif(SMBus_SendByte(slaveAddress+1)) //Send SlaveAddress ×îµÍλRd=1±íʾ½ÓÏÂÀ´¶ÁÊý¾Ý{goto repeat; //Repeat comunication again}DataL = SMBus_ReceiveByte(ACK); //Read low data,master must send ACKDataH = SMBus_ReceiveByte(ACK); //Read high data,master must send ACKPec = SMBus_ReceiveByte(NACK); //Read PEC byte, master must send NACKSMBus_StopBit(); //Stop conditionarr[5] = slaveAddress; //arr[4] = command; //arr[3] = slaveAddress+1; //Load array arrarr[2] = DataL; //arr[1] = DataH; //arr[0] = 0; //PecReg=PEC_Calculation(arr);//Calculate CRC}while(PecReg != Pec); //If received and calculated CRC are equal go out from do-while{}data = (DataH<<8) | DataL; //data=DataH:DataLreturn data;
}/*******************************************************************************
* Function Name : PEC_calculation
* Description : Calculates the PEC of received bytes
* Input : pec[]
* Output : None
* Return : pec[0]-this byte contains calculated crc value
*******************************************************************************/
u8 PEC_Calculation(u8 pec[])
{u8 crc[6];u8 BitPosition=47;u8 shift;u8 i;u8 j;u8 temp;do{/*Load pattern value 0x000000000107*/crc[5]=0;crc[4]=0;crc[3]=0;crc[2]=0;crc[1]=0x01;crc[0]=0x07;/*Set maximum bit position at 47 ( six bytes byte5...byte0,MSbit=47)*/BitPosition=47;/*Set shift position at 0*/shift=0;/*Find first "1" in the transmited message beginning from the MSByte byte5*/i=5;j=0;while((pec[i]&(0x80>>j))==0 && i>0){BitPosition--;if(j<7){j++;}else{j=0x00;i--;}}/*End of while *//*Get shift value for pattern value*/shift=BitPosition-8;/*Shift pattern value */while(shift){for(i=5; i<0xFF; i--){if((crc[i-1]&0x80) && (i>0)){temp=1;}else{temp=0;}crc[i]<<=1;crc[i]+=temp;}/*End of for*/shift--;}/*End of while*//*Exclusive OR between pec and crc*/for(i=0; i<=5; i++){pec[i] ^=crc[i];}/*End of for*/}while(BitPosition>8); /*End of do-while*/return pec[0];
}/******************************************************************************** Function Name : SMBus_ReadTemp* Description : Calculate and return the temperature* Input : None* Output : None* Return : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{ float temp;temp = SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;return temp;
}
.h
#ifndef __MLX90614_H
#define __MLX90614_H/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "tim.h"
/* Exported types ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void SMBus_StartBit(void);
void SMBus_StopBit(void);
void SMBus_SendBit(u8);
u8 SMBus_SendByte(u8);
u8 SMBus_ReceiveBit(void);
u8 SMBus_ReceiveByte(u8);
void SMBus_Delay(u16);
void SMBus_Init(void);
u16 SMBus_ReadMemory(u8, u8);
u8 PEC_Calculation(u8*);
float SMBus_ReadTemp(void); //»ñȡζÈÖµ#endif
实际效果
非常灵敏
不愧是红外
GY906 温度的传感器相关推荐
- STM32 BMP280模块 获取气压温度高度传感器数据 TFT显示
STM32 BMP280模块 获取气压温度高度传感器数据 TFT显示 简介 BMP280是博世最新推出的数字气压传感器,具有卓越的性能和低廉的价格,相对精度为±0.12 hPa(相当于±1米),传感器 ...
- 树莓派+串口墨水电子屏幕+温度湿度传感器打造专属时钟
前言 前两天在http://shumeipai.nxez.com/上看到一篇文章<宅男必备:配备电子纸屏幕的抽纸盒>,发现作者使用树莓派和一些硬件设备打造了一款特别有趣的东西,于是我也特别 ...
- 多传感器检测系统设计 光纤传感器(测位移)+温度pt100传感器
多传感器检测系统设计 光纤传感器(测位移)+温度pt100传感器+电涡流传感器(测转速): multisim电路仿真+labview人机交互界面设计: ID:5489666438612407
- 基于 Arduino 的温度+湿度传感器
虽然根本不懂电路,但经过查阅资料,总算是实现了.在这里做一个记录,以免忘记. 开发环境 硬件 Arduino uno r3 开发板,点我看Arduino各版本的比较,uno的第3个版本,也是目前市面上 ...
- Qt温度湿度传感器采样上位机源代码C++数据记录功能Windows上位机
Qt温度湿度传感器采样上位机源代码C++数据记录功能Windows上位机 YID:765645958944466他也是来自江湖
- 温度湿度传感器流程图_温度传感器和湿度传感器在高铁轨道板智能养护中的应用...
导读:近日,国内首条高智能轨道板生产线产品在天津宝坻一次性通过验收.该生产线担负京唐高速铁路40991块轨道板的生产任务,年生产轨道板可达43800块,是目前国内规模最大的智能化轨道板生产线.其中,在 ...
- 温度湿度传感器流程图_为什么温湿度传感器用一段时间就会漂移?
温湿度是冷和热物体的物理量.温湿度作为衡量整体环境质量的重要标准,在生产和科学研究中,许多物理现象和化学过程在一定温度.湿度下进行.其中农业.工业.楼宇.办公室.机房等领域对温湿度传感器的需求量越来越 ...
- 红外温度枪传感器资料
推荐资料 http://m.elecfans.com/article/619986.html https://wenku.baidu.com/view/9efc60be690203d8ce2f0066 ...
- 温度湿度传感器用法笔记
芯科科技(Silicon labs)的Gecko系列芯片内部ADC集成有内部参考电压和温度传感器,温度精度很低,误差超过±3摄氏度,优点就是在精度要求不高的Z-Wave/Zigbee系统中提供了一个免 ...
最新文章
- python 使用 redis expire属性设置访问时间间隔
- 小学计算机课计划,小学信息技术教学工作计划
- 【Flocking、PPO无人机群控制算法】基于Flocking和PPO深度强化学习的无人机群控制算法的MATLAB仿真
- golang函数多值返回示例
- c++中声明和定义的区别(这个兄弟写的解决了我的疑惑)
- mysql和oracle 开源_MySQL和oracle比较
- Linux守护进程列表
- 可交换的四本书的封面
- linux send and recv详解
- latex 设置pdf的页边距
- 快来,这里不仅有帅哥,还有美女!!
- 对jquery val 获取input 文本框值进行扩展
- Linux基金会宣布行业进一步支持Akraino Edge Stack
- spring cloud 搭建问题记录
- linux下文件打包、压缩详解
- Typora突然开始收费?开源免费的MarkText了解一下
- 算法设计与分析知识点整理
- 【2021ACM-ICPC亚洲区域赛济南站】C、D、J、K四题超详细题解
- 提供风声无组件上传类V2.11下载 解决图片尺寸读取问题
- matlab如何编newton-raphson,Matlab中的Newton Raphsons方法?