赞
踩
- #ifndef _REMOTE_H_
- #define _REMOTE_H_
-
- #define IR_1 0X45
- #define IR_2 0X46
- #define IR_3 0X47
- #define IR_4 0X44
- #define IR_5 0X40
- #define IR_6 0X43
- #define IR_7 0X07
- #define IR_8 0X15
- #define IR_9 0X09
- #define IR_star 0X16
- #define IR_0 0X19
- #define IR_signal 0X0D
- #define IR_up 0X18
- #define IR_left 0X08
- #define IR_ok 0X1C
- #define IR_right 0X5A
- #define IR_down 0X52
-
- extern uint8_t IR_Address,IR_Command;
-
- void Remote_Init(void);
- uint8_t IR_GetDataFlag(void);
- uint8_t IR_GetRepeatFlag(void);
- uint8_t IR_GetAddress(void);
- uint8_t IR_GetCommand(void);
- #endif
-
- #include "stm32f10x.h"
-
- void Remote_Init()
- {
- /*¿ªÆôʱÖÓ*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
-
- /*GPIO³õʼ»¯*/
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPD;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_6);
-
- /*ʱ»ùµ¥Ôª³õʼ»¯*/
- TIM_InternalClockConfig(TIM3);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 30000 - 1;
- TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
- /*ÊäÈ벶»ñÅäÖÃ*/
- TIM_ICInitTypeDef TIM_ICInitStructure;
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
- TIM_ICInitStructure.TIM_ICFilter = 0xf;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_CKD_DIV1;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM_ICInit(TIM3,&TIM_ICInitStructure);
-
- /*NVICÖжϷÖ×é*/
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- /*NVICÅäÖÃ*/
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_Init(&NVIC_InitStructure);
-
- /*ʱÖÓÉèÖÃ*/
- TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
-
- /*ʱÖÓʹÄÜ*/
- TIM_Cmd(TIM3,ENABLE);
- }
-
- uint8_t IR_State;
- uint16_t IR_Time;
- uint8_t IR_RepeatFlag,IR_DataFlag;
- uint8_t IR_DATA[4];
- uint8_t IR_pData;
- uint8_t IR_Address,IR_Command;
-
- uint8_t IR_GetDataFlag()
- {
- if (IR_DataFlag)
- {
- IR_DataFlag = 0;
- return 1;
- }
- return 0;
- }
-
- uint8_t IR_GetRepeatFlag()
- {
- if (IR_RepeatFlag)
- {
- IR_RepeatFlag = 0;
- return 1;
- }
- return 0;
- }
-
- uint8_t IR_GetAddress()
- {
- return IR_Address;
- }
-
- uint8_t IR_GetCommand()
- {
- return IR_Command;
- }
-
- void TIM3_IRQHandler()
- {
- uint8_t Addr_Temp,Cmd_Temp;
- if (TIM_GetITStatus(TIM3,TIM_FLAG_CC1) != RESET)
- {
- if (IR_State == 0) //¿ÕÏÐ̬
- {
- TIM_SetCounter(TIM3,0);
- IR_State = 1;
- }
- else if (IR_State == 1) //×¼±¸Ì¬
- {
- IR_Time = TIM_GetCounter(TIM3);
- if (IR_Time > 13500 - 500 && IR_Time < 13500+500) //StartÐźÅ
- {
- IR_State = 2;
- }
- else if (IR_Time > 11250 - 500 && IR_Time < 11250+500) //RepeatÐźÅ
- {
- IR_State = 0;
- IR_RepeatFlag = 1;
- }
- else
- {
-
- }
- TIM_SetCounter(TIM3,0);
- }
- else if (IR_State == 2) //½ÓÊÕ̬
- {
- IR_Time = TIM_GetCounter(TIM3);
- if (IR_Time > 1120 -500 && IR_Time < 1120+500) //Âß¼0
- {
- IR_DATA[IR_pData/8] &= ~(0x01 << (IR_pData %8));
- IR_pData++;
- }
- else if (IR_Time > 2250 -500 && IR_Time < 2250 + 500) //Âß¼1
- {
- IR_DATA[IR_pData/8] |= (0x01 << (IR_pData %8));
- IR_pData++;
- }
- else
- {
- IR_pData = 0;
- IR_State = 1;
- }
-
- if (IR_pData>=32) //Èç¹û½ÓÊÕÍê32λÊý¾Ý
- {
- IR_pData = 0;
- Addr_Temp = ~IR_DATA[1];
- Cmd_Temp = ~IR_DATA[3];
- if (IR_DATA[0] == Addr_Temp && IR_DATA[2] == Cmd_Temp) //Êý¾ÝÑéÖ¤
- {
- IR_Address = IR_DATA[0];
- IR_Command = IR_DATA[2];
- IR_DataFlag = 1;
- }
- IR_State = 0;
- }
- TIM_SetCounter(TIM3,0);
- }
- else
- {
-
- }
- TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。