找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5597|回复: 3
收起左侧

单片机+MLX90614传感器非接触式温度监测程序与电路图

[复制链接]
ID:497748 发表于 2019-3-25 10:56 | 显示全部楼层 |阅读模式
我之前做的医用非接触式温度监测
使用的MLX90614传感器作为检测芯片;
STC15W408作为主控芯片;
下面是我的程序源码;

电路原理图如下:

原理图

原理图


单片机源程序如下:
  1. //**********************************************************************************************
  2. //                                                                BASIC INFORMATION ABOUT THIS PROGRAM
  3. //***********************************************************************************************
  4. //File name:        main.c
  5. //Version:                01.00
  6. //Company:               
  7. //Details:                 SMBus comunication with MLX90614 using 89C52
  8. //                                Language C: uVision4 compiler
  9. //                                Fosc=24MHz, Tcy=0.5us,Pull-up Resister 2k
  10. //Author:                       
  11. //*****************************************************************************************************************
  12. //                                                                                HEADER FILES
  13. //*****************************************************************************************************************
  14. //#include <reg52.h>
  15. #include "main.h"
  16. #include "delay.h"   
  17. unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,};
  18. unsigned char code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,};

  19. sbit PP7=P1^0;
  20. sbit PP6=P3^6;
  21. sbit PP5=P3^3;
  22. sbit PP4=P1^4;
  23. sbit PP3=P1^1;
  24. sbit PP2=P3^7;
  25. sbit PP1=P3^2;          
  26. sbit PP0=P1^3;                   //a
  27. sbit led=P5^5;
  28. void Data(unsigned char da)
  29. {
  30.   PP7=(bit)(da&0x80);
  31.   PP6=(bit)(da&0x40);
  32.   PP5=(bit)(da&0x20);
  33.   PP4=(bit)(da&0x10);
  34.   PP3=(bit)(da&0x08);
  35.   PP2=(bit)(da&0x04);
  36.   PP1=(bit)(da&0x02);
  37.   PP0=(bit)(da&0x01);

  38. }
  39. //*****************************************************************************************************************
  40. //                                                                                FUNCTION'S DEFINITIONS
  41. //*****************************************************************************************************************
  42. void MCUinit(void)
  43. {
  44. //IO setting-up       
  45. //        P1=0xFF;                 //All chanels are digital I/O
  46. //        P3=0xFF;
  47.          //P2M0=0x00;P2M1=0xff;  
  48. //SMBus setting-up       
  49.         SDA = mSDA_HIGH;                     //The bus is in idle state
  50.         SCL = mSCL_HIGH;                         //SDA and SCL are in high level from pull up resitors
  51. }//End of init()
  52.        
  53. //-----------------------------------------------------------------------------------------------------------------

  54. unsigned int MemRead(unsigned char uSlaveAddress,unsigned char UC_command)
  55. {
  56.         unsigned int  StData;                        // Data storage (DataH:DataL)
  57.         unsigned char Pec;                                // PEC byte storage
  58.         unsigned char DataL;                        // Low data byte storage
  59.         unsigned char DataH;                        // High data byte storage
  60.         unsigned char arr[6];                        // Buffer for the sent bytes
  61.         unsigned char PecReg;                        // Calculated PEC byte storage
  62.         unsigned char ErrorCounter;                // Defines the number of the attempts for communication with MLX90614
  63.         unsigned char SLA;

  64.         ErrorCounter=0x0F;                                // Initialising of ErrorCounter
  65.         SLA = uSlaveAddress<<1;
  66.         do{
  67.         repeat:
  68.                 STOP_bit();                                        //If slave send NACK stop comunication       
  69.                 --ErrorCounter;                                //Pre-decrement ErrorCounter
  70.                 if(!ErrorCounter){                        //ErrorCounter=0?
  71.                         break;                                        //Yes,go out from do-while{}
  72.                 }
  73.                 START_bit();                                //Start condition
  74.                
  75.                 if(TX_byte(SLA)){                        //Send SlaveAddress
  76.                         goto        repeat;                        //Repeat comunication again
  77.                 }         
  78.                 if(TX_byte(UC_command)){        //Send command       
  79.                         goto        repeat;                        //Repeat comunication again
  80.                 }
  81.                 START_bit();                                //Repeated Start condition
  82.                
  83.                 if(TX_byte(SLA+1)){                         //Send SlaveAddress
  84.                         goto        repeat;         //Repeat comunication again
  85.                 }

  86.                 DataL=RX_byte(ACK);                        //Read low data,master must send ACK
  87.                 DataH=RX_byte(ACK);                 //Read high data,master must send ACK
  88.                 Pec=RX_byte(NACK);                        //Read PEC byte, master must send NACK
  89.                 STOP_bit();                                        //Stop condition
  90.                
  91.                
  92.                 arr[5]=SLA;                //
  93.                 arr[4]=UC_command;                        //
  94.                 arr[3]=SLA+1;                //Load array arr
  95.                 arr[2]=DataL;                                //
  96.                 arr[1]=DataH;                                //
  97.                 arr[0]=0;                                        //
  98.                 PecReg=PEC_calculation(arr);//Calculate CRC                 
  99.                         
  100.         }while(PecReg != Pec);                //If received and calculated CRC are equal go out from do-while{}
  101.        
  102.         StData = DataH*256+DataL;        //改用这种方法时正确

  103.        
  104.         return StData;                                                       
  105. }
  106. /**
  107. //---------------------------------------------------------------------------------------------
  108. void SendRequest(void)
  109. {
  110.         SCL = mSCL_LOW;                        //SCL 1 ____________|<-----80ms------->|______________
  111.         delay(DEL80ms);                        //           0                    |__________________|
  112.         SCL = mSCL_HIGH;
  113. }
  114. //---------------------------------------------------------------------------------------------
  115. void DummyCommand(unsigned char byte)
  116. {
  117.         START_bit();                //Start condition
  118.         TX_byte(byte);                //Send Slave Address or whatever,no need ACK checking
  119.         STOP_bit();                        //Stop condition
  120. }
  121. //---------------------------------------------------------------------------------------------
  122. **/

  123. float CalcTemp(unsigned int value)
  124. {
  125.         float temp;
  126.        
  127.         temp=(value*0.02)-273.15;
  128.        
  129.         return temp;
  130. }

  131. void Display (unsigned int tempreture)
  132. {
  133.    
  134.   // unsigned char numb1;          //b
  135.    unsigned char numb2;                 //s
  136.    unsigned char numb3;                        //g
  137.    unsigned char numb4;//xiao s

  138. //  numb1=(unsigned char)(tempreture/1000);
  139.    numb2=(unsigned char)((tempreture%1000)/100);
  140.    numb3=(unsigned char)((tempreture%100)/10);
  141.    numb4=(unsigned char)(tempreture%10);
  142.    
  143.    Data(table[numb2]);LED3=0; delay(5);LED3=1;
  144.    
  145.    Data(table1[numb3]);LED2=0; delay(5);LED2=1;
  146.    

  147.    Data(table[numb4]);LED1=0; delay(5);LED1=1;
  148. }




  149. //*****************************************************************************************************************
  150. //                                                                                MAIN PROGRAM
  151. //*****************************************************************************************************************       
  152. void main(void)
  153. {
  154.         unsigned char         SlaveAddress;                         //Contains device address
  155.         unsigned char        uCommand;                                  //Contains the access command
  156.         unsigned int         uData;                                          //Contains data value       
  157.         float                        t;                                                //Contains calculated temperature in degrees Celsius       
  158.         unsigned int tt;       
  159. //        LCD_Initial();                                                        //LCD initialization
  160.         MCUinit();                                                                  //MCU initialization          
  161.         SlaveAddress=SA<<1;                                                //Set device address
  162.         uCommand=RAM_Access|RAM_Tobj1;                 //Form RAM access command + RAM address                        
  163.        
  164. //        SendRequest();                                                        //Switch to SMBus mode - this is need if module is in PWM mode only
  165. //        DummyCommand(SlaveAddress);                                //This is need if Request Command is sent even when the module is in SMBus mode
  166. //        delay(DEL200ms);                                                //Wait after POR,Tvalid=0.15s
  167.         delay200ms();
  168.                
  169. ……………………

  170. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
医用温度检测-代码及原理图.zip (165.02 KB, 下载次数: 123)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:671440 发表于 2019-12-23 16:00 | 显示全部楼层
好用,谢谢!
回复

使用道具 举报

ID:33784 发表于 2020-2-15 23:57 | 显示全部楼层
为啥我只能上电第一次能读取成功,后来数据就不更新了。
回复

使用道具 举报

ID:676587 发表于 2020-5-25 15:45 | 显示全部楼层
我想问一下大家,这个测温模块直接与单片机引脚相接就可以了,不用外加设计什么放大电路了对吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表