今晚搞了一下TN9,红外体温计模块。
Data:PC13
CLK:PC14
ACK:PC15
代码:
#include "delay.h"
#include "sys.h"
#include "led.h"
#define uchar unsigned char
#define uint unsigned int
void ACK_0() { GPIO_ResetBits(GPIOC,GPIO_Pin_15);} //
void ACK_1() { GPIO_SetBits(GPIOC,GPIO_Pin_15);}
#define DATA GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)
#define CLOCK GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_14)
float Temp;
float Temp1,Temp2;
unsigned char ReadData[5];
unsigned int a,b;
//-----------------------------------------------------------------------------------------
//????:Delay()
//? ?:?????
//-----------------------------------------------------------------------------------------
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
//-----------------------------------------------------------------------------------------
//????:Delayms()
//-----------------------------------------------------------------------------------------
void Delayms(unsigned int ims)
{
unsigned int i,j;
for(i=0;i<ims;i++)
for(j=0;j<65;j++) { Delay(1); }
}
void TN_IRACK_EN(void)
{
ACK_0();
Delay(1);
}
void TN_IRACK_UN(void)
{
ACK_1();
Delay(1);
}
void InitIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC, ENABLE); //¿ªÆôGPIOA,GPIOBʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void TN_ReadData(unsigned char Flag)
{
unsigned char i,j,k,BitState=0;
for(k=0;k<7;k++)
{
for(j=0;j<5;j++)
{
for(i=0;i<8;i++)
{
while(1)
{
if(CLOCK==0)
break;
Delay(1);
}
Delay(1);
if(DATA==1)
BitState=1;
else
BitState=0;
ReadData[j]= ReadData[j]<<1;
ReadData[j]= ReadData[j]|BitState;
Delay(1);
while(1)
{
if(CLOCK==1)
break;
Delay(1);
}
}
}
if(ReadData[0]==Flag)
k=8;
}
TN_IRACK_UN();
}
float TN_GetData(unsigned char X)
{
//Ack_Clk_Data_init;
InitIO();
TN_ReadData(X);
Temp=(ReadData[1]<<8)|ReadData[2];
Temp=(float)Temp/16.00-273.15;
return Temp;
}
float TN_GetTemp(unsigned char mode)
{
float T;
{
TN_IRACK_UN();
TN_IRACK_EN();
if(mode==0)
{
T=TN_GetData(0x4c);
}
else
{
T=TN_GetData(0x66);
}
}
return T;
}
/********************************************************************************************************
**º¯ÊýÐÅÏ¢ £ºmain(void)
**¹¦ÄÜÃèÊö £º
**ÊäÈë²ÎÊý £ºÎÞ
**Êä³ö²ÎÊý £ºÎÞ
********************************************************************************************************/
int main(void)
{
delay_init();
LED_Init();
while(1) //ÎÞÏÞÑ»·
{
Temp1=TN_GetTemp(0);
Temp2=TN_GetTemp(1);
a=((unsigned int)(Temp1*10));
b=((unsigned int)(Temp2*10));
delay_ms(500);
}
}
效果图:
a是体温,b是环境温度
332表示33.2度,214表示21.4度。
工程:
TN9.rar
(327.75 KB)
|