STM32F103ZET6+红外温度传感器mlx90614芯片
【STM32F103ZET6+红外温度传感器mlx90614芯片】单片机读取温度通过串口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 ACK0
#define NACK 1
#define SA0x00 //从机地址,单个MLX90614时地址为0x00,多个时地址默认为0x5a
#define RAM_ACCESS0x00 //RAM access command
#define EEPROM_ACCESS 0x20 //EEPROM access command
#define RAM_TOBJ10x07 //To1 address in the eeprom#define SMBUS_PORTGPIOB
#define SMBUS_SCKGPIO_Pin_6
#define SMBUS_SDAGPIO_Pin_7#define RCC_APB2Periph_SMBUS_PORTRCC_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 line
SMBus_Delay(5);
// Wait a few microseconds
SMBUS_SCK_H();
// Set SCL line
SMBus_Delay(5);
// Generate bus free time between Stop
SMBUS_SDA_L();
// Clear SDA line
SMBus_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 line
SMBus_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 line
SMBus_Delay(5);
// Wait a few microseconds
SMBUS_SDA_L();
// Clear SDA line
SMBus_Delay(5);
// Wait a few microseconds
SMBUS_SCK_H();
// Set SCL line
SMBus_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 SDA
Tx_buffer<<=1;
// Get next bit for checking
}Ack_bit=SMBus_ReceiveBit();
// Get acknowledgment bit
return 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 minimum
SMBUS_SCK_H();
// Set SCL line
SMBus_Delay(6);
// High Level of Clock Pulse
SMBUS_SCK_L();
// Clear SCL line
SMBus_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 Pulse
SMBUS_SCK_H();
// Set SCL line
SMBus_Delay(5);
// High Level of Clock Pulse
if (SMBUS_SDA_PIN())
{
Ack_bit=1;
}
else
{
Ack_bit=0;
}
SMBUS_SCK_L();
// Clear SCL line
SMBus_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)
{
u8RX_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 1in RX_buffer
RX_buffer |=0x01;
}
else
{
RX_buffer <<= 1;
// If the bit is LOW save 0 in RX_buffer
RX_buffer &=0xfe;
}
}
SMBus_SendBit(ack_nack);
// Sends acknowledgment bit
return 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
主函数
#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);
//串口初始化为115200
LED_Init();
//LED端口初始化
LCD_Init();
SMBus_Init();
while(1)
{a = SMBus_ReadTemp();
printf("温度 = %f℃\r\n",a);
LED0=!LED0;
delay_ms(250);
}
}
推荐阅读
- 《教育心理学》读书笔记五---关注特殊群体学生|《教育心理学》读书笔记五---关注特殊群体学生 做有温度的教育
- 广州太古仓夜拍
- 2020-08-25冷焊机补轴,温度低精度高,焊后手可以摸,内应力小不变形
- 2019-06-27温度|2019-06-27温度 - 草稿
- 剽悍行动营12复盘-活的有温度
- 密语
- 生活的温度,
- 做有温度的教育
- 有温度的文字
- 锅炉排烟温度偏高的九大原因