[新品上市] 【APM32F107VCT6 MINI开发板测评】TN9

[复制链接]
540|0
 楼主| 比神乐 发表于 2023-2-21 13:09 | 显示全部楼层 |阅读模式
今天搞了一下TN9,TN9是红外测温模块。
截取一部分数据手册:
2.jpg
ACK接PC9,DATA接PC7,CLK接PC8.
代码:
  1. #include "main.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /** @addtogroup Examples
  5.   @{
  6. */
  7. #define uchar unsigned char
  8. #define uint unsigned int
  9.        
  10. void ACK_0() { GPIO_ResetBit(ACK_GPIO_PORT,GPIO_PIN_9);}  //
  11. void ACK_1() { GPIO_SetBit(ACK_GPIO_PORT,GPIO_PIN_9);}



  12. #define DATA            GPIO_ReadInputBit(DATA_GPIO_PORT,DATA_PIN)

  13. #define CLOCK           GPIO_ReadInputBit(CLOCK_GPIO_PORT,CLOCK_PIN)



  14. float Temp;
  15. float Temp1,Temp2;
  16. uchar ReadData[5];
  17. uint bai,shi,ge;
  18. uint a,b;
  19. volatile uint32_t tick = 0;
  20. uint8_t txBuf1[] = "Body temperature=:    \r\nAmbient temperature=:    \r\n";
  21. char txBuf2[4] = {};


  22. //-----------------------------------------------------------------------------------------
  23. //????:Delay()
  24. //?    ?:?????
  25. //-----------------------------------------------------------------------------------------
  26. void Delay1(unsigned char us)  //5,7,9
  27. {
  28.   while(--us);  
  29. }

  30. //-----------------------------------------------------------------------------------------
  31. //????:Delayms()
  32. //-----------------------------------------------------------------------------------------
  33. void Delayms(unsigned int ims)
  34. {
  35.   unsigned int i,j;
  36.     for(i=0;i<ims;i++)
  37.       for(j=0;j<65;j++) { Delay1(1); }
  38. }
  39. void TN_IRACK_EN(void)
  40. {
  41.         ACK_0();
  42.   Delay1(1);
  43. }


  44. void TN_IRACK_UN(void)
  45. {
  46.         ACK_1();
  47.   Delay1(1);
  48. }

  49. void InitIO(void)
  50. {
  51.                 GPIO_Config_T  configStruct;

  52.     /* Enable the GPIO_LED Clock */
  53.     RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);

  54.     /* Configure the GPIO_LED pin */
  55.     configStruct.pin = ACK_PIN;
  56.     configStruct.mode = GPIO_MODE_OUT_PP;
  57.     configStruct.speed = GPIO_SPEED_50MHz;

  58.     GPIO_Config(GPIOC, &configStruct);
  59.    
  60.                 configStruct.pin = DATA_PIN | CLOCK_PIN;
  61.     configStruct.mode = GPIO_MODE_IN_FLOATING;
  62.     configStruct.speed = GPIO_SPEED_50MHz;

  63.     GPIO_Config(GPIOC, &configStruct);
  64. }
  65. void TN_ReadData(unsigned char Flag)
  66. {
  67.         unsigned char i,j,k,BitState=0;
  68.         for(k=0;k<7;k++)
  69.         {
  70.                 for(j=0;j<5;j++)
  71.                 {
  72.                         for(i=0;i<8;i++)
  73.                         {
  74.                                 while(1)
  75.                                 {
  76.                                   if(CLOCK==0)
  77.                                     break;
  78.                                   Delay1(1);
  79.                                 }
  80.                                 Delay1(1);
  81.                                 if(DATA==1)
  82.                                   BitState=1;
  83.                                 else
  84.                                   BitState=0;
  85.                                 ReadData[j]= ReadData[j]<<1;
  86.                                 ReadData[j]= ReadData[j]|BitState;
  87.                                 Delay1(1);
  88.                                 while(1)
  89.                                 {
  90.                                   if(CLOCK==1)
  91.                                     break;
  92.                                   Delay1(1);
  93.                                 }
  94.                         }
  95.                 }
  96.                 if(ReadData[0]==Flag)
  97.                         k=8;
  98.         }
  99.         TN_IRACK_UN();
  100. }
  101. float TN_GetData(unsigned char X)
  102. {
  103.         //Ack_Clk_Data_init;
  104.         InitIO();
  105.         TN_ReadData(X);
  106.         Temp=(ReadData[1]<<8)|ReadData[2];
  107.         Temp=(float)Temp/16.00-273.15;
  108.         return Temp;
  109. }
  110. float TN_GetTemp(unsigned char mode)
  111. {
  112.        
  113.         float T;
  114.        
  115.         {       
  116.                 TN_IRACK_UN();
  117.                 TN_IRACK_EN();
  118.                 if(mode==0)
  119.                 {
  120.                         T=TN_GetData(0x4c);
  121.                
  122.                 }
  123.                 else
  124.                 {
  125.                         T=TN_GetData(0x66);
  126.                        
  127.                 }
  128.         }
  129.        
  130.        
  131.         return T;
  132. }

  133. /** @addtogroup USART_Printf
  134.   @{
  135. */

  136. /** @addtogroup USART_Printf_Variables Variables
  137.   @{
  138. */


  139. /**@} end of group USART_Printf_Variables */

  140. /** @addtogroup USART_Printf_Functions Functions
  141.   @{
  142. */

  143. /*!
  144. * [url=home.php?mod=space&uid=247401]@brief[/url]       Main program
  145. *
  146. * @param       None
  147. *
  148. * @retval      None
  149. *
  150. */
  151. int main(void)
  152. {
  153.     uint8_t i;

  154.     GPIO_Config_T GPIO_ConfigStruct;
  155.     USART_Config_T USART_ConfigStruct;

  156.     APM_MINI_LEDInit(LED2);

  157.     RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));

  158.     GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  159.     GPIO_ConfigStruct.pin = GPIO_PIN_9;
  160.     GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  161.     GPIO_Config(GPIOA, &GPIO_ConfigStruct);

  162.     USART_ConfigStruct.baudRate = 115200;
  163.     USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  164.     USART_ConfigStruct.mode = USART_MODE_TX;
  165.     USART_ConfigStruct.parity = USART_PARITY_NONE;
  166.     USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  167.     USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  168.     USART_Config(USART1, &USART_ConfigStruct);

  169.     USART_Enable(USART1);

  170.     SysTick_Config(SystemCoreClock / 1000);

  171.     while(1)
  172.     {
  173.         Temp1=TN_GetTemp(0);
  174.         Temp2=TN_GetTemp(1);

  175.                                 sprintf(txBuf2,"%.1f",Temp1);
  176.                                 txBuf1[18]=txBuf2[0];
  177.                                 txBuf1[19]=txBuf2[1];
  178.                                 txBuf1[20]=txBuf2[2];
  179.                                 txBuf1[21]=txBuf2[3];
  180.                                 sprintf(txBuf2,"%.1f",Temp2);
  181.                                 txBuf1[45]=txBuf2[0];
  182.                                 txBuf1[46]=txBuf2[1];
  183.                                 txBuf1[47]=txBuf2[2];
  184.                                 txBuf1[48]=txBuf2[3];
  185.                                 for(i = 0; i < sizeof(txBuf1); i++)
  186.         {
  187.             while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
  188.             USART_TxData(USART1, txBuf1[i]);
  189.         }

  190.         Delayms(500);

  191.         
  192.                                
  193.         APM_MINI_LEDToggle(LED2);
  194.     }
  195. }

  196. /*!
  197. * @brief       Delay
  198. *
  199. * @param       None
  200. *
  201. * @retval      None
  202. *
  203. */
  204. void Delay(void)
  205. {
  206.     tick = 0;

  207.     while(tick < 500);
  208. }
效果图:
0.jpg 1.jpg 3.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部