红外测温模块选用 ZyTemp 公司的 TN905 红外测温模块,其测温原理应用斯特凡-玻尔兹曼定律和维恩位移定律,是一种集成的红外探测器, TN905 模块采用 3V 供电, 数字输出接口为 SPI 接口。
电路原理图如下:
单片机源程序如下:
- /***********************************************************************/
- /* */
- /* FILE :Main.c */
- /* DATE : */
- /* DESCRIPTION :Main Program */
- /* CPU TYPE : */
- /* */
- /* NOTE:THIS IS A TYPICAL EXAMPLE. */
- /* */
- /***********************************************************************/
- /******************************************************************************
- Includes <System Includes> , "Project Includes"
- ******************************************************************************/
- #include "iodefine.h"
- #include "userdefine.h"
- #include "system.h"
- #include "lcd.h"
- #include "sensor.h"
- #include "key_led_buzzer.h"
- #include "ad.h"
- #include "e2prom.h"
- #include "timer.h"
- /******************************************************************************
- Macro definitions
- ******************************************************************************/
- #define ALARM_DEGREE 3700 /* Body temperature alarm value 37 degree Celsius */
- /******************************************************************************
- Global Variables
- ******************************************************************************/
- _Bool g_AlarmFlag; /* g_AlarmFlag is true if body "temperature > ALARM_DEGREE" */
- uint8_t g_RunMode; /* MCU run mode:MODE_WAIT,MODE_READY,MODE_SETTING,MODE_SET_FINISH,MODE_MEASURE,MODE_CALCULATE,MODE_DISPLAY */
- uint8_t g_MeasureMode; /* Measure mode: object temperature or ambiant temperature */
- uint8_t g_AlarmCounter; /* Alarm counter if body "temperature > ALARM_DEGREE" */
- uint8_t g_WaitCounter; /* Wait counter, MCU will enter halt mode if no any action after 30s */
- uint8_t g_SetData; /* Temperature calibration data */
- uint8_t g_ObjectTemp[5]; /* Object temperature original data */
- uint8_t g_AmbiantTemp[5]; /* Ambiant temperature original data */
- uint8_t *g_DataPoint; /* Data point for temperature data */
- uint8_t g_DataNum; /* Temperature data counter */
- uint8_t g_ObjCount; /* Object temperature calculate data counter */
- uint8_t g_AmbiCount; /* Ambiant temperature calculate data counter */
- uint32_t g_ObjectData; /* Object temperature calculate data */
- uint32_t g_AmbiantData; /* Ambiant temperature calculate data */
- uint32_t g_ObjectDataTemp[5]; /* Object temperature calculate data array for average */
- uint32_t g_AmbiantDataTemp[5]; /* Ambiant temperature calculate data array for average */
- uint32_t g_DisplayData; /* Temperature data for LCD display */
- /******************************************************************************
- Extern global Variables defined in other files
- ******************************************************************************/
- extern uint8_t g_KeyEnable;
- extern uint8_t g_KeyStatus;
- extern uint8_t g_KeyOldStatus;
- extern uint8_t g_KeyReturn;
- extern uint8_t g_aDataBuffer[];
- /******************************************************************************
- Private Function Prototypes
- ******************************************************************************/
- static uint32_t Calculate_Temperature( uint32_t * DataPoint);
- static void ModeTransferByKey(void);
- /******************************************************************************
- * Function Name : main
- * Description : main loop.
- * Arguments : none
- * Return Value : none
- ******************************************************************************/
- void main(void)
- {
- /* Local variables */
- uint8_t t_tempdata;
- /* Initialize global variables */
- g_RunMode = MODE_READY; /* Initial run mode */
- g_MeasureMode = MEASURE_OBJ; /* Initial measure mode */
- g_AlarmFlag = FALSE; /* Alarm flag is set to 0 */
- /* System initialize */
- System_Init();
- /* Get temperature calibration data*/
- EEPROM_ReadRandom(0x0000,0x01,g_aDataBuffer);
- g_SetData = g_aDataBuffer[0];
- /* Main loop */
- while(1)
- {
- /* Transfer run mode according to pressing key */
- ModeTransferByKey();
- /* Run mode handle */
- switch( g_RunMode )
- {
- case MODE_WAIT:
- LCD_Stop(); /* Close LCD display brefore enter to halt mode */
- __halt(); /* Enter to halt mode */
- break;
- case MODE_READY:
- LCD_Start();
- break;
- case MODE_SETTING:
- BLON = 1; /* Blink LCD when calibration data is setting */
- break;
- case MODE_SET_FINISH:
- BLON = 0; /* Close blink LCD when calibration data setting is finished */
- EEPROM_WriteByte(0x0000, g_SetData); /* Get calibration data */
- /* Transfer to MODE_READY after MODE_SET_FINISH */
- g_RunMode = MODE_READY;
- break;
- case MODE_MEASURE: /* Get the original data of temperature */
- {
- t_tempdata = g_ObjectTemp[0] + g_ObjectTemp[1] + g_ObjectTemp[2];
- if ( (g_ObjectTemp[0] == 0x4c) && (g_ObjectTemp[4] == 0x0D) && (t_tempdata == g_ObjectTemp[3]) )
- {
- g_ObjectData = g_ObjectTemp[1];
- g_ObjectData = ( g_ObjectData<<8 ) + g_ObjectTemp[2];
- g_ObjectData *= 100;
- g_ObjectData = ( g_ObjectData/16 - 27315 );
-
- g_ObjectDataTemp[g_ObjCount] = g_ObjectData;
- g_ObjCount++;
- if( g_ObjCount > 4 ) /* Get data 5 times */
- {
- g_RunMode = MODE_CALCULATE;
- g_ObjCount = 0;
- }
- }
- t_tempdata = g_AmbiantTemp[0] + g_AmbiantTemp[1] + g_AmbiantTemp[2];
- if ( (g_AmbiantTemp[0] == 0x66) && (g_AmbiantTemp[4] == 0x0D) && (t_tempdata == g_AmbiantTemp[3]) )
- {
- g_AmbiantData = g_AmbiantTemp[1];
- g_AmbiantData = ( g_AmbiantData<<8 ) + g_AmbiantTemp[2];
- g_AmbiantData *= 100;
- g_AmbiantData = ( g_AmbiantData/16 - 27315 );
- g_AmbiantDataTemp[g_AmbiCount] = g_AmbiantData;
- g_AmbiCount++;
- if( g_AmbiCount > 4 ) /* Get data 5 times */
- {
- g_RunMode = MODE_CALCULATE;
- g_AmbiCount = 0;
- }
- }
- }
-
- break;
- case MODE_CALCULATE: /* Calculate the real data of temperature */
- if( g_MeasureMode == MEASURE_OBJ )
- g_DisplayData = Calculate_Temperature( g_ObjectDataTemp );
-
- else
- g_DisplayData = Calculate_Temperature( g_AmbiantDataTemp );
- /* Adjust temperature according to setting data */
- if( g_SetData >= 0x80)
- /* Calibration data is minus if data >= 0x80 */
- g_DisplayData = g_DisplayData - (uint16_t)( (g_SetData&0x7f)*10);
- else
- /* Calibration data is plus if data > 0x80 */
- g_DisplayData = g_DisplayData + (uint16_t)( (g_SetData&0x7f)*10);
-
- if( g_MeasureMode == MEASURE_OBJ )
- {
- /* g_AlarmFlag is true if body "temperature > ALARM_DEGREE" */
- if( g_DisplayData > ALARM_DEGREE )
- g_AlarmFlag = TRUE;
- }
-
- g_RunMode = MODE_DISPLAY;
- break;
- }
- /* LCD display process */
- Display_Process();
- }
- }
- /******************************************************************************
- End of function main
- ******************************************************************************/
- /******************************************************************************
- * Function Name : Calculate_Temperature
- * Description : This function calculate the real temperature.
- * Arguments : none
- * Return Value : uint32_t-
- the temperature data*100(for example: 3200 means 32 Celsius degree)
- ******************************************************************************/
- static uint32_t Calculate_Temperature( uint32_t * DataPoint)
- {
- /* Local variables */
- uint8_t i = 0;
- uint32_t t_max_data,t_min_data;
- uint32_t t_TempData;
- t_max_data = *DataPoint;
- t_min_data = *DataPoint;
- t_TempData = 0;
- while(i++ < 5)
- {
- if( *DataPoint > t_max_data )
- t_max_data = *DataPoint;
- if( *DataPoint < t_min_data )
- t_min_data = *DataPoint;
-
- t_TempData += *DataPoint;
- DataPoint++;
- }
- /* Get the average value after delete the max data and min data */
- t_TempData = ( t_TempData - t_max_data - t_min_data );
- t_TempData = t_TempData/3;
- return(t_TempData);
- }
- /******************************************************************************
- End of function Calculate_Temperature
- ******************************************************************************/
- /******************************************************************************
- * Function Name : ModeTransferByKey
- * Description : This function enter run mode according to pressing key.
- * Arguments : none
- * Return Value : none
- ******************************************************************************/
- static void ModeTransferByKey(void)
- {
- /* Key handle when confirm key is pressed */
- if(g_KeyEnable == KEY_PRESS)
- {
- if(g_KeyReturn == KEY_GAUGE)
- {
- if( g_RunMode != MODE_SETTING) /* Enter MODE_MEASURE if gauge key is pressed not in setting mode */
- {
- g_RunMode = MODE_MEASURE;
- g_WaitCounter = 0;
- BUZZER = LOW; /* Buzzzer on when press "measure" key */
- LED = HIGH; /* LED on when press "measure" key */
- }
- else
- g_RunMode = MODE_SET_FINISH;
- }
- if(g_KeyReturn == KEY_OBJECT)
- {
- if( g_RunMode != MODE_SETTING)
- {
- g_MeasureMode = MEASURE_OBJ; /* Enter to MEASURE_OBJ mode */
- }
- else
- {
- /* Calibration data -1 in setting mode */
- g_SetData--;
- }
- }
- if(g_KeyReturn == KEY_AMBIANT)
- {
- if( g_RunMode != MODE_SETTING)
- {
- g_MeasureMode = MEASURE_AMBI;
- }
- else
- {
- /* Calibration data +1 in setting mode */
- g_SetData++;
- }
- }
- g_KeyEnable = KEY_RELEASE; /* Release key staus after key handling */
- }
- }
- /******************************************************************************
- End of function ModeTransferByKey
- ******************************************************************************/
复制代码
所有资料51hei提供下载:
非接触式红外感应体温计(全套资料).7z
(3.04 MB, 下载次数: 48)
|