今天搞了一下TN9,TN9是红外测温模块。
截取一部分数据手册:
ACK接PC9,DATA接PC7,CLK接PC8.
代码:
#include "main.h"
#include <stdio.h>
#include <string.h>
/** @addtogroup Examples
@{
*/
#define uchar unsigned char
#define uint unsigned int
void ACK_0() { GPIO_ResetBit(ACK_GPIO_PORT,GPIO_PIN_9);} //
void ACK_1() { GPIO_SetBit(ACK_GPIO_PORT,GPIO_PIN_9);}
#define DATA GPIO_ReadInputBit(DATA_GPIO_PORT,DATA_PIN)
#define CLOCK GPIO_ReadInputBit(CLOCK_GPIO_PORT,CLOCK_PIN)
float Temp;
float Temp1,Temp2;
uchar ReadData[5];
uint bai,shi,ge;
uint a,b;
volatile uint32_t tick = 0;
uint8_t txBuf1[] = "Body temperature=: \r\nAmbient temperature=: \r\n";
char txBuf2[4] = {};
//-----------------------------------------------------------------------------------------
//????:Delay()
//? ?:?????
//-----------------------------------------------------------------------------------------
void Delay1(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++) { Delay1(1); }
}
void TN_IRACK_EN(void)
{
ACK_0();
Delay1(1);
}
void TN_IRACK_UN(void)
{
ACK_1();
Delay1(1);
}
void InitIO(void)
{
GPIO_Config_T configStruct;
/* Enable the GPIO_LED Clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
/* Configure the GPIO_LED pin */
configStruct.pin = ACK_PIN;
configStruct.mode = GPIO_MODE_OUT_PP;
configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOC, &configStruct);
configStruct.pin = DATA_PIN | CLOCK_PIN;
configStruct.mode = GPIO_MODE_IN_FLOATING;
configStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOC, &configStruct);
}
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;
Delay1(1);
}
Delay1(1);
if(DATA==1)
BitState=1;
else
BitState=0;
ReadData[j]= ReadData[j]<<1;
ReadData[j]= ReadData[j]|BitState;
Delay1(1);
while(1)
{
if(CLOCK==1)
break;
Delay1(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;
}
/** @addtogroup USART_Printf
@{
*/
/** @addtogroup USART_Printf_Variables Variables
@{
*/
/**@} end of group USART_Printf_Variables */
/** @addtogroup USART_Printf_Functions Functions
@{
*/
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
uint8_t i;
GPIO_Config_T GPIO_ConfigStruct;
USART_Config_T USART_ConfigStruct;
APM_MINI_LEDInit(LED2);
RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_ConfigStruct.pin = GPIO_PIN_9;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
USART_ConfigStruct.mode = USART_MODE_TX;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_Config(USART1, &USART_ConfigStruct);
USART_Enable(USART1);
SysTick_Config(SystemCoreClock / 1000);
while(1)
{
Temp1=TN_GetTemp(0);
Temp2=TN_GetTemp(1);
sprintf(txBuf2,"%.1f",Temp1);
txBuf1[18]=txBuf2[0];
txBuf1[19]=txBuf2[1];
txBuf1[20]=txBuf2[2];
txBuf1[21]=txBuf2[3];
sprintf(txBuf2,"%.1f",Temp2);
txBuf1[45]=txBuf2[0];
txBuf1[46]=txBuf2[1];
txBuf1[47]=txBuf2[2];
txBuf1[48]=txBuf2[3];
for(i = 0; i < sizeof(txBuf1); i++)
{
while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
USART_TxData(USART1, txBuf1[i]);
}
Delayms(500);
APM_MINI_LEDToggle(LED2);
}
}
/*!
* @brief Delay
*
* @param None
*
* @retval None
*
*/
void Delay(void)
{
tick = 0;
while(tick < 500);
}
效果图:
|