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

相关文章

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr