今晚搞了一下TN9(红外体温计模块)
DATA:PB8
CLOCK:PB7
ACK:PB6
代码:
#include "gd32e23x.h"
#include "gd32e231c_start.h"
#include "systick.h"
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define ACK_PIN GPIO_PIN_6
#define ACK_GPIO_PORT GPIOB
#define ACK_GPIO_CLK RCU_GPIOB
#define DATA_PIN GPIO_PIN_8
#define DATA_GPIO_PORT GPIOB
#define DATA_GPIO_CLK RCU_GPIOB
#define CLK_PIN GPIO_PIN_7
#define CLK_GPIO_PORT GPIOB
#define CLK_GPIO_CLK RCU_GPIOB
void led_flash(int times);
void ACK_0() { GPIO_BC(ACK_GPIO_PORT) = ACK_PIN;} //
void ACK_1() { GPIO_BOP(ACK_GPIO_PORT) = ACK_PIN;}
#define DATA gpio_input_bit_get(DATA_GPIO_PORT,DATA_PIN)
#define CLOCK gpio_input_bit_get(CLK_GPIO_PORT,CLK_PIN)
float Temp;
float Temp1,Temp2;
unsigned char ReadData[5];
//float 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)
{
/* enable the key clock */
rcu_periph_clock_enable(DATA_GPIO_CLK);
rcu_periph_clock_enable(RCU_CFGCMP);
/* configure button pin as input */
gpio_mode_set(DATA_GPIO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DATA_PIN);
gpio_mode_set(CLK_GPIO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, CLK_PIN);
/* configure led GPIO port */
gpio_mode_set(ACK_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ACK_PIN);
gpio_output_options_set(ACK_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, ACK_PIN);
}
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)
{
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;
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* initialize KEY and LED, configure SysTick */
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
systick_config();
gd_eval_led_init(LED1);
/* flash the LED for test */
led_flash(1);
delay_1ms(500);
gd_eval_led_on(LED1);
InitIO();
while(1)
{
Temp1=TN_GetTemp(0);
Temp2=TN_GetTemp(1);
delay_1ms(500);
}
}
/*!
\brief flash the LED for test
\param[in] times: times to flash the LEDs
\param[out] none
\retval none
*/
void led_flash(int times)
{
int i;
for (i = 0;i < times;i++){
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
}
}
效果图:
工程:
TN9.rar
(608.37 KB)
|