比神乐 发表于 2021-1-27 14:59

【AT-START-F407测评】+ TN9

今天捣鼓了一下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;
//floata,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= ReadData<<1;
                                ReadData= ReadData|BitState;
                              Delay(1);
                                while(1)
                                {
                                        if(CLOCK==1)
                                                break;
                                        Delay(1);
                                }
                        }
                }
                if(ReadData==Flag)
                        k=8;
        }
        TN_IRACK_UN();
}
float TN_GetData(unsigned char X)
{
       
AT32_TN9_Init();
        TN_ReadData(X);
        Temp=(ReadData<<8)|ReadData;
        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;
}


/**
* @briefMain Function.
* @paramNone
* @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);
}
}

#ifdefUSE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: 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


/**
* @}
*/

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










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

请问这是怎么回事?谢谢
页: [1]
查看完整版本: 【AT-START-F407测评】+ TN9