今天搞了一下TN9,无线红外模块。
ACTION接PB8,CLOCK接PB7,DATA接PB5
代码
- #include "at32f425_board.h"
- #include "at32f425_clock.h"
- #include "stdio.h"
- #include "at32f425.h"
- /** @addtogroup AT32F425_periph_examples
- * @{
- */
- #define uchar unsigned char
- #define uint unsigned int
- /** @addtogroup 425_GPIO_led_toggle GPIO_led_toggle
- * @{
- */
- #define ACK_PIN GPIO_PINS_8
- #define ACK_GPIO GPIOB
- #define ACK_GPIO_CRM_CLK CRM_GPIOB_PERIPH_CLOCK
- #define CLK_PIN GPIO_PINS_7
- #define CLK_GPIO GPIOB
- #define CLK_GPIO_CRM_CLK CRM_GPIOB_PERIPH_CLOCK
- #define DATA_PIN GPIO_PINS_5
- #define DATA_GPIO GPIOB
- #define DATA_GPIO_CRM_CLK CRM_GPIOB_PERIPH_CLOCK
- void ACK_0() { gpio_bits_reset(GPIOB,GPIO_PINS_8);} //
- void ACK_1() { gpio_bits_set(GPIOB,GPIO_PINS_8);}
- uint16_t TN9[1] = {ACK_PIN};
- crm_periph_clock_type TN9_gpio_crm_clk[1] = {ACK_GPIO_CRM_CLK};
- gpio_type *TN9_port[3] = {ACK_GPIO};
- float Temp;
- float Temp1,Temp2;
- unsigned char ReadData[5];
- //float a,b;
- //-----------------------------------------------------------------------------------------
- void Delay(unsigned char us) //5,7,9
- {
- while(--us);
- }
- //-----------------------------------------------------------------------------------------
- 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(gpio_input_data_bit_read(CLK_GPIO,CLK_PIN)==0)
- break;
- Delay(1);
- }
- Delay(1);
- if(gpio_input_data_bit_read(DATA_GPIO,DATA_PIN)==1)
- BitState=1;
- else
- BitState=0;
- ReadData[j]= ReadData[j]<<1;
- ReadData[j]= ReadData[j]|BitState;
- Delay(1);
- while(1)
- {
- if(gpio_input_data_bit_read(CLK_GPIO,CLK_PIN)==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;
- }
- void TN9_io_init(void)
- {
- gpio_init_type gpio_init_struct;
- /* enable the led clock */
- crm_periph_clock_enable(TN9_gpio_crm_clk[0],TRUE);
-
- /* set default parameter */
- gpio_default_para_init(&gpio_init_struct);
- /* configure the led gpio */
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
- gpio_init_struct.gpio_pins = ACK_PIN;
- gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
- gpio_init(TN9_port[0],&gpio_init_struct);
-
-
- }
- void at32_input_init(void)
- {
- gpio_init_type gpio_init_struct;
- /* enable the button clock */
- crm_periph_clock_enable(CLK_GPIO_CRM_CLK, TRUE);
- /* set default parameter */
- gpio_default_para_init(&gpio_init_struct);
- /* configure button pin as input with pull-up/pull-down */
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
- gpio_init_struct.gpio_pins = CLK_PIN;
- gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
- gpio_init(CLK_GPIO, &gpio_init_struct);
-
- gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
- gpio_init_struct.gpio_pins = DATA_PIN;
- gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
- gpio_init(DATA_GPIO, &gpio_init_struct);
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] main function.
- * @param none
- * @retval none
- */
- int main(void)
- {
- long int i;
- system_clock_config();
- at32_board_init();
- TN9_io_init();
- at32_input_init();
-
- while(1)
- {
-
-
- Temp1=TN_GetTemp(0);
- Temp2=TN_GetTemp(1);
-
- Delayms(500);
- }
- }
效果图:
上面是体温,下面是环境温度。
|