打印
[活动专区]

【AT-START-F407测评】+ TN9

[复制链接]
302|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
今天捣鼓了一下TN9模块
TN9模块是红外测温模块。
接线:
DATA:PB12
CLK:PB14
ACTION:PD8
代码:
#include <stdio.h>
#include "at32f4xx.h"
#include "at32_board.h"

/** @addtogroup AT32F407_StdPeriph_Examples
  * @{
  */

/** @addtogroup GPIO_LEDToggle
  * @{
  */
#define uchar unsigned char
#define uint unsigned int
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/







       




void ACK_0() { ACK_GPIO->BRE = ACK_PIN;}  //
void ACK_1() { ACK_GPIO->BSRE = ACK_PIN;}



#define DATA            GPIO_ReadInputDataBit(DATA_GPIO,DATA_PIN)

#define CLOCK           GPIO_ReadInputDataBit(CLK_GPIO,CLK_PIN)



float Temp;
float Temp1,Temp2;
unsigned char ReadData[5];
//float  a,b;
void AT32_TN9_Init(void)
{
  GPIO_InitType GPIO_InitStructure;
  /*Enable the LED Clock*/

  RCC_APB2PeriphClockCmd(CLK_GPIO_RCC_CLK|DATA_GPIO_RCC_CLK|ACK_GPIO_RCC_CLK, ENABLE);


  /*Configure the LED pin as ouput push-pull*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pins = ACK_PIN;                                 

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;       
  GPIO_Init(ACK_GPIO, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pins = CLK_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(CLK_GPIO, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pins = DATA_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(DATA_GPIO, &GPIO_InitStructure);
}
//-----------------------------------------------------------------------------------------
//????: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 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)
{
       
  AT32_TN9_Init();
        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;
}


/**
  * @brief  Main Function.
  * @param  None
  * @retval None
  */
int main(void)
{
  AT32_Board_Init();
        AT32_TN9_Init();
       
  for(;;)
  {
    AT32_LEDn_Toggle(LED2);

    Temp1=TN_GetTemp(0);
                Temp2=TN_GetTemp(1);
                Delay_ms(1000);
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {}
}
#endif


/**
  * @}
  */

/**
  * @}
  */
效果图:










另外发现一个问题设个断点调试,运行完,停不下,提示错误。

请问这是怎么回事?谢谢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

380

主题

3213

帖子

7

粉丝