登录
|
立即注册
|
使用QQ帐号登录
论坛
>
51单片机
发帖
|
单片机+MLX90614传感器非接触式温度监测程序与电路图
看6715
|
回4
|
收藏
楼主
ID:497748
只看他
2019-3-25 10:56
我之前做的医用非接触式温度监测
使用的
MLX90614传感器作为检测芯片;
STC15W408作为主控芯片;
下面是我的程序源码;
电路原理图如下:
单片机源程序如下:
代码:
//**********************************************************************************************
// BASIC INFORMATION ABOUT THIS PROGRAM
//***********************************************************************************************
//File name: main.c
//Version: 01.00
//Company:
//Details: SMBus comunication with MLX90614 using 89C52
// Language C: uVision4 compiler
// Fosc=24MHz, Tcy=0.5us,Pull-up Resister 2k
//Author:
//*****************************************************************************************************************
// HEADER FILES
//*****************************************************************************************************************
//#include <reg52.h>
#include "main.h"
#include "delay.h"
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,};
sbit PP7=P1^0;
sbit PP6=P3^6;
sbit PP5=P3^3;
sbit PP4=P1^4;
sbit PP3=P1^1;
sbit PP2=P3^7;
sbit PP1=P3^2;
sbit PP0=P1^3; //a
sbit led=P5^5;
void Data(unsigned char da)
{
PP7=(bit)(da&0x80);
PP6=(bit)(da&0x40);
PP5=(bit)(da&0x20);
PP4=(bit)(da&0x10);
PP3=(bit)(da&0x08);
PP2=(bit)(da&0x04);
PP1=(bit)(da&0x02);
PP0=(bit)(da&0x01);
}
//*****************************************************************************************************************
// FUNCTION'S DEFINITIONS
//*****************************************************************************************************************
void MCUinit(void)
{
//IO setting-up
// P1=0xFF; //All chanels are digital I/O
// P3=0xFF;
//P2M0=0x00;P2M1=0xff;
//SMBus setting-up
SDA = mSDA_HIGH; //The bus is in idle state
SCL = mSCL_HIGH; //SDA and SCL are in high level from pull up resitors
}//End of init()
//-----------------------------------------------------------------------------------------------------------------
unsigned int MemRead(unsigned char uSlaveAddress,unsigned char UC_command)
{
unsigned int StData; // Data storage (DataH:DataL)
unsigned char Pec; // PEC byte storage
unsigned char DataL; // Low data byte storage
unsigned char DataH; // High data byte storage
unsigned char arr[6]; // Buffer for the sent bytes
unsigned char PecReg; // Calculated PEC byte storage
unsigned char ErrorCounter; // Defines the number of the attempts for communication with MLX90614
unsigned char SLA;
ErrorCounter=0x0F; // Initialising of ErrorCounter
SLA = uSlaveAddress<<1;
do{
repeat:
STOP_bit(); //If slave send NACK stop comunication
--ErrorCounter; //Pre-decrement ErrorCounter
if(!ErrorCounter){ //ErrorCounter=0?
break; //Yes,go out from do-while{}
}
START_bit(); //Start condition
if(TX_byte(SLA)){ //Send SlaveAddress
goto repeat; //Repeat comunication again
}
if(TX_byte(UC_command)){ //Send command
goto repeat; //Repeat comunication again
}
START_bit(); //Repeated Start condition
if(TX_byte(SLA+1)){ //Send SlaveAddress
goto repeat; //Repeat comunication again
}
DataL=RX_byte(ACK); //Read low data,master must send ACK
DataH=RX_byte(ACK); //Read high data,master must send ACK
Pec=RX_byte(NACK); //Read PEC byte, master must send NACK
STOP_bit(); //Stop condition
arr[5]=SLA; //
arr[4]=UC_command; //
arr[3]=SLA+1; //Load array arr
arr[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{}
StData = DataH*256+DataL; //改用这种方法时正确
return StData;
}
/**
//---------------------------------------------------------------------------------------------
void SendRequest(void)
{
SCL = mSCL_LOW; //SCL 1 ____________|<-----80ms------->|______________
delay(DEL80ms); // 0 |__________________|
SCL = mSCL_HIGH;
}
//---------------------------------------------------------------------------------------------
void DummyCommand(unsigned char byte)
{
START_bit(); //Start condition
TX_byte(byte); //Send Slave Address or whatever,no need ACK checking
STOP_bit(); //Stop condition
}
//---------------------------------------------------------------------------------------------
**/
float CalcTemp(unsigned int value)
{
float temp;
temp=(value*0.02)-273.15;
return temp;
}
void Display (unsigned int tempreture)
{
// unsigned char numb1; //b
unsigned char numb2; //s
unsigned char numb3; //g
unsigned char numb4;//xiao s
// numb1=(unsigned char)(tempreture/1000);
numb2=(unsigned char)((tempreture%1000)/100);
numb3=(unsigned char)((tempreture%100)/10);
numb4=(unsigned char)(tempreture%10);
Data(table[numb2]);LED3=0; delay(5);LED3=1;
Data(table1[numb3]);LED2=0; delay(5);LED2=1;
Data(table[numb4]);LED1=0; delay(5);LED1=1;
}
//*****************************************************************************************************************
// MAIN PROGRAM
//*****************************************************************************************************************
void main(void)
{
unsigned char SlaveAddress; //Contains device address
unsigned char uCommand; //Contains the access command
unsigned int uData; //Contains data value
float t; //Contains calculated temperature in degrees Celsius
unsigned int tt;
// LCD_Initial(); //LCD initialization
MCUinit(); //MCU initialization
SlaveAddress=SA<<1; //Set device address
uCommand=RAM_Access|RAM_Tobj1; //Form RAM access command + RAM address
// SendRequest(); //Switch to SMBus mode - this is need if module is in PWM mode only
// DummyCommand(SlaveAddress); //This is need if Request Command is sent even when the module is in SMBus mode
// delay(DEL200ms); //Wait after POR,Tvalid=0.15s
delay200ms();
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
所有资料51hei提供下载:
医用温度检测-代码及原理图.zip
(165.02 KB)
(下载次数: 128, 2019-3-25 10:54 上传)
下载积分: 黑币 -5
沙发
ID:671440
只看他
2019-12-23 16:00
好用,谢谢!
板凳
ID:33784
只看他
2020-2-15 23:57
为啥我只能上电第一次能读取成功,后来数据就不更新了。
地板
ID:676587
只看他
2020-5-25 15:45
我想问一下大家,这个测温模块直接与单片机引脚相接就可以了,不用外加设计什么放大电路了对吗?
5
#
ID:1116361
只看他
2024-4-12 15:20
好用,很有参考价值,继续学习。
51黑电子论坛
Powered by
Discuz!
X3.1
首页
|
标准版
|
触屏版
|
电脑版