STM32读取MLX90614模块温度,并在OLED屏幕上显示

2023-10-11 10:30

本文主要是介绍STM32读取MLX90614模块温度,并在OLED屏幕上显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

mlx90614驱动代码来源于网络,OLED驱动代码来源于江协科技,侵权删

本文使用硬件:STM32F103C8T6最小系统板、IIC协议0.96寸OLED屏幕显示、mlx90614红外测温模块。

 实现功能:在OLED上显示出mlx90614采集到的温度,精确到小数点后两位。

mlx.c

#include "stm32f10x.h"                  // Device header
#include "Delay.h"#include "mlx.h"
#define ACK         0
#define NACK        1
#define SA                        0x00 
#define RAM_ACCESS                0x00 
#define EEPROM_ACCESS             0x20 
#define RAM_TOBJ1                 0x07 #define SMBUS_PORT               GPIOB
#define SMBUS_SCK                GPIO_Pin_15
#define SMBUS_SDA                GPIO_Pin_14#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 void SMBus_StartBit(void)
{SMBUS_SDA_H();                // Set SDA lineSMBus_Delay(8);            // Wait a few microsecondsSMBUS_SCK_H();                // Set SCL lineSMBus_Delay(8);            // Generate bus free time between StopSMBUS_SDA_L();                // Clear SDA lineSMBus_Delay(8);            // 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(8);            // Wait a few microseconds
}void SMBus_StopBit(void)
{SMBUS_SCK_L();                // Clear SCL lineSMBus_Delay(8);            // Wait a few microsecondsSMBUS_SDA_L();                // Clear SDA lineSMBus_Delay(8);            // Wait a few microsecondsSMBUS_SCK_H();                // Set SCL lineSMBus_Delay(8);            // Stop condition setup time(Tsu:sto=4.0us min)SMBUS_SDA_H();                // Set SDA line
}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;
}void SMBus_SendBit(u8 bit_out)
{if(bit_out==0){SMBUS_SDA_L();}else{SMBUS_SDA_H();}SMBus_Delay(6);                                        // Tsu:dat = 250ns minimumSMBUS_SCK_H();                                        // Set SCL lineSMBus_Delay(8);                                        // High Level of Clock PulseSMBUS_SCK_L();                                        // Clear SCL lineSMBus_Delay(8);                                        // Low Level of Clock Pulsereturn;
}u8 SMBus_ReceiveBit(void)
{u8 Ack_bit;SMBUS_SDA_H();          SMBus_Delay(6);                        // High Level of Clock PulseSMBUS_SCK_H();                        // Set SCL lineSMBus_Delay(8);                        // High Level of Clock Pulseif (SMBUS_SDA_PIN()){Ack_bit=1;}else{Ack_bit=0;}SMBUS_SCK_L();                        // Clear SCL lineSMBus_Delay(8);                        // Low Level of Clock Pulsereturn        Ack_bit;
}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;
}void SMBus_Delay(u16 time)
{u16 i, j;for (i=0; i<5; i++){for (j=0; j<time; j++);}
}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();
}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;
}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];
}float SMBus_ReadTemp(void)
{   return (SMBus_ReadMemory(SA, RAM_ACCESS|RAM_TOBJ1)*0.02-273.15);
}

mlx.h

#ifndef __MLX_H
#define __MLX_H#include "stm32f10x.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); //获取温度值
#endif

main.c

#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "mlx.h"int main(void)
{float temper;OLED_Init();SMBus_Init();OLED_ShowString(1, 1, "T:");OLED_ShowString(1, 5, ".");while(1){temper = SMBus_ReadTemp();OLED_ShowNum(1, 3, temper, 2);  //显示温度整数OLED_ShowNum(1, 6, (unsigned long)(temper*100)%100, 2); //显示小数点后两位的温度Delay_ms(500);}}

实物效果图:

工程文件百度网盘链接:链接:https://pan.baidu.com/s/18gQPZKej-pdFgj-ZpxWswg 
提取码:1234

这篇关于STM32读取MLX90614模块温度,并在OLED屏幕上显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/187433

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

多模块的springboot项目发布指定模块的脚本方式

《多模块的springboot项目发布指定模块的脚本方式》该文章主要介绍了如何在多模块的SpringBoot项目中发布指定模块的脚本,作者原先的脚本会清理并编译所有模块,导致发布时间过长,通过简化脚本... 目录多模块的springboot项目发布指定模块的脚本1、不计成本地全部发布2、指定模块发布总结多模

Java读取InfluxDB数据库的方法详解

《Java读取InfluxDB数据库的方法详解》本文介绍基于Java语言,读取InfluxDB数据库的方法,包括读取InfluxDB的所有数据库,以及指定数据库中的measurement、field、... 首先,创建一个Java项目,用于撰写代码。接下来,配置所需要的依赖;这里我们就选择可用于与Infl

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

SpringBoot使用Apache POI库读取Excel文件的操作详解

《SpringBoot使用ApachePOI库读取Excel文件的操作详解》在日常开发中,我们经常需要处理Excel文件中的数据,无论是从数据库导入数据、处理数据报表,还是批量生成数据,都可能会遇到... 目录项目背景依赖导入读取Excel模板的实现代码实现代码解析ExcelDemoInfoDTO 数据传输