本帖最后由 Cjy_JDxy 于 2020-4-17 19:26 编辑
今晚搞了一下TN9(红外体温计模块)
代码:
#include "msp.h"
#define uchar unsigned char
#define uint unsigned int
void ACK_0() { P4->OUT&=~BIT4;} //
void ACK_1() { P4->OUT|=BIT4;}
#define DATA (P4IN&0x01)
#define CLOCK (P4IN&0x04)
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)
{
P4DIR=0x10;
P4SEL1=0;
P4SEL0=0;
P4REN=0x05;
P4OUT=0x05;
}
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==0x04)
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;
}
int main(void)
{
volatile uint32_t i;
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // Stop watchdog timer
// The following code toggles P1.0 port
P1->DIR |= BIT0 ; // Configure P1.0 as output
InitIO();
while(1)
{
Temp1=TN_GetTemp(0);
Temp2=TN_GetTemp(1);
a=((unsigned int)(Temp1*10));
b=((unsigned int)(Temp2*10));
P1->OUT ^= 0x01;
Delayms(500);
}
}
效果图:
a表示体温,b是环境温度。235表示23.5度,214表示21.4度
工程:
TN9.rar
(984.75 KB)
|