单片机读取温度通过串口1在串口调试助手上显示温度。

头文件
#ifndef __MLX90614_H
#define __MLX90614_H
#include "sys.h"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);    //获取温度值
void SMBus_DisplayTemperature(void);    //在LCD第5,6页显示温度
#endif
源文件
#include "mlx90614.h"
#define ACK  0
#define NACK 1
#define SA              0x00 //从机地址,单个MLX90614时地址为0x00,多个时地址默认为0x5a
#define RAM_ACCESS      0x00 //RAM access command
#define EEPROM_ACCESS   0x20 //EEPROM access command
#define RAM_TOBJ1       0x07 //To1 address in the eeprom#define SMBUS_PORT      GPIOB
#define SMBUS_SCK       GPIO_Pin_6
#define SMBUS_SDA       GPIO_Pin_7#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 //读取引脚电平/*******************************************************************************
* 函数名: SMBus_StartBit
* 功能  : 产生起始位
* 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)SMBUS_SCK_L();       // Clear SCL lineSMBus_Delay(5);        // Wait a few microseconds
}/*******************************************************************************
* 函数名: SMBus_StopBit
* 功能: 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
}/*******************************************************************************
* 函数名: SMBus_SendByte
* 功能: 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;
}/*******************************************************************************
* 函数名: SMBus_SendBit
* 功能: 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;
}/*******************************************************************************
* 函数名: SMBus_ReceiveByte
* 功能: 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;
}/*******************************************************************************
* 函数名: SMBus_Delay
* 功能: 延时  一次循环约1us
* Input          : time
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Delay(u16 time)
{u16 i, j;for (i=0; i<4; i++){for (j=0; j<time; j++);}
}/*******************************************************************************
* 函数名: SMBus_Init
* 功能: SMBus初始化
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SMBus_Init()
{GPIO_InitTypeDef    GPIO_InitStructure;/* Enable SMBUS_PORT clocks */RCC_APB2PeriphClockCmd(RCC_APB2Periph_SMBUS_PORT, ENABLE);/*配置SMBUS_SCK、SMBUS_SDA为集电极开漏输出*/GPIO_InitStructure.GPIO_Pin = SMBUS_SCK | SMBUS_SDA;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(SMBUS_PORT, &GPIO_InitStructure);SMBUS_SCK_H();SMBUS_SDA_H();
}/******************************************************************************** 函数名: SMBus_ReadMemory* 功能: READ DATA FROM RAM/EEPROM* Input          : slaveAddress, command* 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;
}/*******************************************************************************
* 函数名: PEC_calculation
* 功能 : 数据校验
* 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];
}/******************************************************************************** 函数名: SMBus_ReadTemp* 功能: 计算并返回温度值* Return         : SMBus_ReadMemory(0x00, 0x07)*0.02-273.15
*******************************************************************************/
float SMBus_ReadTemp(void)
{   return SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15;
}/*********************************END OF FILE*********************************/
主函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "adc.h"
#include "mlx90614.h" int main(void){  float a;delay_init();           //延时函数初始化    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级uart_init(115200);      //串口初始化为115200LED_Init();               //LED端口初始化LCD_Init();  SMBus_Init();    while(1){a = SMBus_ReadTemp();printf("温度 = %f℃\r\n",a);LED0=!LED0;delay_ms(250);  }}

STM32F103ZET6+红外温度传感器mlx90614芯片相关推荐

  1. 红外温度传感器MLX90614添加修改地址

    https://blog.csdn.net/lsjackson13/article/details/90256571 上面的博文给了我很大帮助,红外温度传感器的基础知识可以参考上面的. 下面主要记录的 ...

  2. 张高兴的 Windows 10 IoT 开发笔记:红外温度传感器 MLX90614

    GitHub : https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/MLX90614 转载于:https://www.cnblo ...

  3. STM32和MLx90614非接触红外温度传感器

    MLx90614介绍: MLx90614非接触红外温度传感器是一款性能和精度都较高的温度测量产品,在医疗,工业等领域应用较多:有四个引脚,使用SMBus协议,通过控制SCL引脚进行数据读取.现在给出一 ...

  4. ARDUINO使用MLX90614红外温度传感器研究笔记

    (温馨提醒:玩之前,建议把电脑上的计算器模式调成"程序员"模式,因为后面可能会用到大量二进制和十六进制的转换.) 相关资料:下载 由于最近由于公司项目,需要一款红外温度传感器,于是 ...

  5. 咚咚咚————【封装驱动】MLX90614医用级红外温度传感器,分享交流自己编写的程序。

    咚咚咚----[封装驱动]MLX90614医用级红外温度传感器,自己编写封装的驱动程序. /******************************************** 主控芯片:STM3 ...

  6. STM32F1读取MLX90615非接触式红外温度传感器

    MLX90615 简介 MLX90615是一种红外温度计,用于非接触式温度测量.红外敏感热电堆探测器芯片和信号调节芯片集成在同一个TO-46 CAN封装中.由于采用了低噪声放大器.16位模数转换器和强 ...

  7. 简单无脑,上手即用 - 手把手教你使用 智能红外温度传感器代码以及依赖的 gitee 库

    简单无脑,上手即用-智能温度传感器代码以及依赖的库! 前言 购买硬件 获取代码 使用代码和库 控制传感器 总结 前言 之前分享了一个大家自己在家就能制作的智能红外温度传感器,可以通过手机和电脑控制的 ...

  8. 使用红外温度传感器制作非接触式红外测温仪

    在调试电子电路或测试新硬件设计时,我常会通过触摸来检查电路板上的器件是否异常发热.如果某些东西搞砸了(通常是在第一次尝试中),这些器件可能会达到80°C或更高温度,不仅会烧毁器件,还会烫伤手指.我都不 ...

  9. STM32F1读取MLX90632非接触式红外温度传感器

    MLX90632 简介 MLX90632是一个小型SMD SFN封装中的非接触式红外温度传感器,可实现高精度非接触式温度测量,.该装置在出厂时使用存储在EEPROM存储器中的校准常数进行校准.环境温度 ...

  10. STM32模拟I2C协议获取MLX90615红外温度传感器测温数据(Open Drain管脚配置)

    STM32模拟I2C协议获取MLX90615红外温度传感器测温数据(Open Drain管脚配置) STM32的GPIO管脚可以配置为Open Drain输出模式,并且有两个功能: 可以设置内部上拉, ...

最新文章

  1. 设计模式实践-策略模式
  2. 前端vue框架的跨域处理方法
  3. Windows安装.net Framework时安装不上,提示已处理证书链,但是在不受信任提供程序信任的根证书中终止
  4. 用 Mathematica 获取图片的 RGB 三基色
  5. 怎么查找电脑中的流氓软件_玻璃丝网印刷过程中油墨出现问题怎么查找原因解决问题?...
  6. scala写入mysql_spark rdd转dataframe 写入mysql的实例讲解
  7. 搭建企业级Docker Registry -- Harbor
  8. Android OpenGL加入光照和材料属性
  9. java一卡通实例代码_java写的简单校园卡管理系统 校园一卡通 - 下载 - 搜珍网
  10. 计算机三角符号,word标尺倒三角 word倒三角符号
  11. 【系统架构】业务架构图
  12. 特殊符号大全(建议收藏_复制着用_数学符号最下面)
  13. 智能手机企业现状 行业发展趋势
  14. 湖南张家界夫妻自助旅游攻略
  15. awk 在指定字符后面插入新字符
  16. python中文社区-python
  17. ui设计一般用什么软件(ui学哪些软件)
  18. 【简陋Web应用3】实现人脸比对
  19. 如何查看计算机是否新装了硬盘,如何查看笔记本电脑换固态硬盘 判断笔记本电脑是否可换固态硬盘的技巧...
  20. 前端面试八股文(详细版)—上

热门文章

  1. 他捧红了王菲、张学友等近百个巨星,却甘心成为最普通的学佛人…
  2. 【自然语言处理】【多模态】Product1M:基于跨模态预训练的弱监督实例级产品检索
  3. Vmware报错“该虚拟机似乎正在使用中”获取该虚拟机所有权失败的解决
  4. 高通骁龙410e/APQ8016E嵌入式物联网应用处理器解决方案
  5. c语言实现 三角函数,关于数学:快速实现C ++三角函数
  6. 零基础学习软件测试必看的python之基础语法
  7. linux中怎么生成hwaddr,linux 下 hwaddr 和 macaddr的区别
  8. Go语言-复合数据结构(map)
  9. 利用Android Studio手动创建活动——笔记(超多图)
  10. iOS 多语言本地化 完美解决方案【自动+手动】