1.软件准备

(1)编程平台:Keil5

(2)CubeMX

(3)XCOM(串口调试助手)

2.硬件准备

(1)GY-906-BCC红外测温模块

(2)F1的板子,本例使用经典F103C8T6

(3)ST-link 下载器

(4)USB-TTL模块

(5)杜邦线若干

3.模块资料

(1)型号差异

(2)部分参数

供电电压:3-5V (内部低压差稳压)

通信方式:标准IIC通信协议

测量温度范围: -70℃~382.2℃

使用环境温度: -40~125℃

温度测量误差:±0.5℃ (室温下)分辨率0.02℃

4.CubeMX配置

(1)芯片选择

(2)配置RCC、SYS、时钟树

配置RCC

配置SYS

配置时钟树

(3)配置GPIO

(4)配置串口1

(5)设置路径、生成代码工程

5、Keil5代码

(1)勾选Use MicroLIB

(2)创建GY906.c和GY906.h文件

(3)添加上述的GY906.c文件进工程

上述两步参考之前系列教学步骤,在此不再赘述(点击跳转)

(4)GY906.c和GY906.h代码

GY906.c

#include "GY906.h"/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ACK  0
#define NACK 1
#define SA              0x00 //设备地址
#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  -70.01 ~ 382.19#define SMBUS_SCK_H()        HAL_GPIO_WritePin(SCK_GPIO_Port,SCK_Pin,GPIO_PIN_SET)
#define SMBUS_SCK_L()        HAL_GPIO_WritePin(SCK_GPIO_Port,SCK_Pin,GPIO_PIN_RESET)
#define SMBUS_SDA_H()        HAL_GPIO_WritePin(SDA_GPIO_Port,SDA_Pin,GPIO_PIN_SET)
#define SMBUS_SDA_L()        HAL_GPIO_WritePin(SDA_GPIO_Port,SDA_Pin,GPIO_PIN_RESET)
#define SMBUS_SDA_PIN()      HAL_GPIO_ReadPin(SDA_GPIO_Port,SDA_Pin) /* 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) StartSMBUS_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ê±£??ì2aμ?SDAóé0μ?1±íê?í¨D??áê?£¨é?éy??£?
}/*******************************************************************************
* 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();          //òy????ía2?μ?×èé?à-£?μ±×÷ê?è?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(u32 time)
{Coarse_delay_us(time);
}/*******************************************************************************
* Function Name  : SMBus_Init
* Description    : SMBus3?ê??ˉ
* 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±íê??ó??à′D′?üá?{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±íê??ó??à′?áêy?Y{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;
}/*** @brief  程序延时 us , 必须在 72M 主频下使用* @param  us: <= 4294967295* @retval None*/
void Coarse_delay_us(uint32_t us)
{uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);while (delay--){;}
}

 GY906.h

#ifndef __GY906_H
#define __GY906_H/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_tvoid 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(u32);
void SMBus_Init(void);
u16 SMBus_ReadMemory(u8, u8);
u8 PEC_Calculation(u8*);
float SMBus_ReadTemp(void);
void Coarse_delay_us(uint32_t us);
#endif

(5)usrat.c代码添加

/* USER CODE BEGIN 0 */
#include "stdio.h"
/* USER CODE END 0 *//* USER CODE BEGIN 1 */
/*********************************************************
*
*重定义 fputc 函数
*
*********************************************************/
int fputc(int ch,FILE *f)
{HAL_UART_Transmit (&huart1 ,(uint8_t *)&ch,1,HAL_MAX_DELAY );return ch;
}
/* USER CODE END 1 */

(6)main函数

int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */float temp;/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){temp=SMBus_ReadTemp();printf("温度值为:%.2f\r\n",temp);HAL_Delay(1000);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}

6.接线图及效果

本例程源码下载:点击跳转

STM32系列(HAL库)——F103C8T6 通过GY906/MLX90614红外测温模块实现温度测量相关推荐

  1. STM32系列(HAL库)——F103C8T6通过MFRC522、RFID射频卡、门禁卡模块读取卡片ID(二)

    本文继上一篇:STM32系列(HAL库)--F103C8T6通过MFRC522.RFID射频卡.门禁卡模块读取卡片ID 本文介绍在运用RC522模块时,运用链表结构存储数据的操作 Let's go! ...

  2. STM32驱动MLX90614红外测温模块

    简介:STM32F103C8T6驱动MLX90614红外测温模块源码介绍. 开发平台:KEIL ARM MCU型号:STM32F103C8T6 传感器型号:MLX90614 特别提示:驱动内可能使用了 ...

  3. MLX90614红外测温模块使用

    一.MLX90614介绍(官方介绍,很官方) MLX90614 是一款红外非接触温度计.TO-39 金属封装里同 时集成了红外感应热电堆探测器芯片和信号处理专用集成芯 片. 由于集成了低噪声放大器.1 ...

  4. STM32系列(HAL库)——F103C8T6通过NRF24L01(2.4G)无线模块进行双机通信

    本文参考:STM32L051C8T6 HAL库 + nRF24L01 收发案例(硬件SPI通讯) 1.软件准备 (1)编程平台:Keil5 (2)CubeMX (3)XCOM(串口调试助手) 2.硬件 ...

  5. STM32系列(HAL库)——F103C8T6通过MFRC522、RFID射频卡、门禁卡模块读取卡片ID

    1.软件准备 (1)编程平台:Keil5 (2)CubeMX (3)XCOM(串口调试助手) 2.硬件准备 (1)MFRC-522模块 (2)F1的板子,本例使用经典F103C8T6 (3)ST-li ...

  6. STM32系列(HAL库)——F103C8T6通过SPI方式读写W25Q64—(Flash存储模块)

    1.软件准备 (1)编程平台:Keil5 (2)CubeMX (3)XCOM(串口调试助手) 2.硬件准备 (1)W25Q64模块 (2)F1的板子,本例使用经典F103C8T6 (3)ST-link ...

  7. STM32系列(HAL库)——F103C8T6点亮1.44寸TFT-LCD彩屏

    1.软件准备 (1)编程平台:Keil5 (2)CubeMX (3)中景园屏幕资料链接:点击跳转        提取码:8888 2.硬件准备 (1)1.44寸TFT彩屏 (2)F1的板子,本例使用经 ...

  8. STM32系列(HAL库)——F103C8T6使用SPI方式点亮OLED

    目录 1.软件准备 2.硬件准备 3.CubeMX配置 (1)芯片选择 (2)配置RCC.SYS.时钟树 (3)配置SPI (4)配置GPIO (5)生成代码工程 4.代码移植 (1)复制文件 (2) ...

  9. STM32系列(HAL库)——F103C8T6硬件SPI点亮带字库OLED屏

    1.软件准备 (1)编程平台:Keil5 (2)CubeMX (3)程序:点击下载 2.硬件准备 (1)1.3寸带字库OLED 注意,成品模块无RES引脚,模块正常工作时RES置于高电平,成品模块是硬 ...

最新文章

  1. css字体设置奇怪问题
  2. linux 类似winscp_mac上有什么类似winscp的软件?
  3. 关系管理系统:CustomerDaoimpl中获取总记录数getTotalrecord()
  4. SpringMVC之组合注解@GetMapping
  5. iOS - AsyncSocket 的使用
  6. java struts2值栈ognl_Struts2的值栈和OGNL牛逼啊
  7. bzoj#4555. [Tjoi2016Heoi2016]求和
  8. html界面选择按钮没法取消,如何使用JavaScript取消选择按钮
  9. linux mongodb 升级,MongoDB2.6简单快速升级到3.0
  10. python将字符串s和换行符写入文件fp_Python 文件操作
  11. 微信带翅膀昵称ʚɞ,这也太可了吧
  12. MySQL数据库基础教程(一)-简介
  13. 移动APP开发环境搭建(新手)
  14. jpg转换成mobi电子书
  15. CSS如何设置div半透明效果
  16. youtube的使用体会
  17. day06笔记(2021-09-28)-OOP:面向对象+封装+继承+多态
  18. python读取fits第三方库_如何读取FITS
  19. [ vulhub漏洞复现篇 ] Tiki Wiki CMS Groupware 认证绕过漏洞CVE-2020-15906
  20. 电脑同步控制android设备,Total Control 免费使用电脑同时控制多台手机的教程及使用方法...

热门文章

  1. 绑定域名addon domain和停放域名parked domain的区别
  2. matplotlib之2017年各产业第一季度国民生产总值行业构成分布饼图
  3. Manjaro 输入法配置
  4. JULLIAN MURPHY:潇洒的女性,都具有这三个特质
  5. 晒晒美国公务员的工资 大大出乎中国网友意料
  6. L2-009 抢红包 (25 分)
  7. 【AI英雄风云榜】为TA投票:谁是2017中国AI领域最牛的人?
  8. python处理cad_Python使用pyautocad+openpyxl处理cad文件示例
  9. alignas和alignof的区别
  10. Reasons to Stay Alive by Matt Haig