|
紅外接收頭收到紅外碼后馬上發(fā)送一個(gè)一樣的紅外碼出去,需要做萬能遙控的自行保存紅外碼再發(fā)出去即可
單片機(jī)源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "Infrared.h"
- int main(void)
- {
- u8 t;
- u8 len;
- u16 times=0;
-
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設(shè)置中斷優(yōu)先級(jí)分組2
- uart_init(9600); //串口初始化為9600
- TIM2_PWM_Init(1895,0);
- Infrared_GPIO_Configuration();
- Infrared_EXTI_Configuration();
- while(1)
- {
- }
- }
復(fù)制代碼- /***************************************************************************************
- * FileName : Infrared.c
- * CopyRight :
- * ModuleName :
- *
- * CPU :
- * RTOS :
- *
- * Create Data : 2015/04/21
- * Author/Corportation : Ray
- *
- * Abstract Description :
- *
- *--------------------------------Revision History--------------------------------------
- * No version Data Revised By Item Description
- * 1 v1.0 2015/4/21 Ray Create this file
- * 2 v2.0 2015/4/23 Ray Sucessfully control the air conditioner
- * 3 v2.1 2015/4/24 Ray Packaging the Infrared Module
- *
- ***************************************************************************************/
- /**************************************************************
- * Debug switch Section
- **************************************************************/
- /**************************************************************
- * Include File Section
- **************************************************************/
- #include "Infrared.h"
- /**************************************************************
- * Macro Define Section
- **************************************************************/
- //debug調(diào)試宏定義,根據(jù)表達(dá)式a的真假執(zhí)行has_bug或no_bug
- #define BUG_DETECT_PRINT(a,has_bug,no_bug) { if(a) \
- printf("%s",has_bug); \
- else \
- printf("%s",no_bug);}
- /**************************************************************
- * Struct Define Section
- **************************************************************/
- /**************************************************************
- * Prototype Declare Section
- **************************************************************/
- /**************************************************************
- * Global Variable Declare Section
- **************************************************************/
- u8 Flag_LearnState = 0;
- u16 PulseTab[MAX_PULSE_LEN];
- /**************************************************************
- * File Static Variable Define Section
- **************************************************************/
- /**************************************************************
- * Function Define Section
- **************************************************************/
- #ifdef INFRARED_RECEIVE
- /**
- * @name void Infrared_GPIO_Configuration()
- * @description 紅外接收端GPIO口設(shè)置
- * @param PA.01 作外部中斷接收口
- * @return
- * @notice
- */
- void Infrared_GPIO_Configuration()
- {
- GPIO_InitTypeDef GPIO_InitType;
-
- RCC_APB2PeriphClockCmd(INFRARED_RCC_GPIOx,ENABLE);
- GPIO_InitType.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
- GPIO_InitType.GPIO_Pin = INFRARED_GPIO_Pinx;
- GPIO_InitType.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(INFRARED_GPIOx,&GPIO_InitType);
- GPIO_EXTILineConfig(INFRARED_EXTI_GPIOx,INFRARED_EXTI_Line);
- }
- /**
- * @name void Infrared_EXTI_Configuration()
- * @description 紅外線接受端外部中斷初始化設(shè)置,設(shè)置為 線路1,使用PA.1作為外部中斷的輸入端
- * @param
- * @return
- * @notice 若改變外部中斷的輸入端,此函數(shù)內(nèi)的線路設(shè)置也需改變s
- */
- void Infrared_EXTI_Configuration()
- {
- EXTI_InitTypeDef EXTI_InitType;
- NVIC_InitTypeDef NVIC_InitType;
-
- EXTI_InitType.EXTI_Line = EXTI_Line1;
- EXTI_InitType.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitType.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitType.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitType);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC 中斷設(shè)置
- NVIC_InitType.NVIC_IRQChannel = EXTI1_IRQn;
- NVIC_InitType.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitType.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitType.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitType);
- }
- /**
- * @name void EXTI1_IRQHandler()
- * @description 外部中斷1中斷處理程序,用于采集紅外波形
- * @param
- * @return 全局變量Flag_LearnState可以用于返回是否有學(xué)習(xí)到波形
- * @notice
- */
- void EXTI1_IRQHandler()
- {
- u16 pulseWidth = 0;
- u16 i = 0;
-
- Flag_LearnState = 0;
- //中斷指示
-
- while(1)
- {
- if(IR_RDATA) //有高脈沖出現(xiàn),代表空閑信號(hào)
- {
- pulseWidth = 0;
- while(IR_RDATA)
- {
- pulseWidth++;
- delay_us(19);
- if(pulseWidth >= 2000) // >40ms 則結(jié)束記錄
- break;
- }
-
- if(pulseWidth<=15 || pulseWidth>=2000) // >40ms || <300us 則結(jié)束記錄
- break;
- PulseTab[i] = pulseWidth*20;
- i++;
- }
- else //載波信號(hào),偶數(shù)位為低電平(載波),奇數(shù)位為高電平(空閑)
- {
- pulseWidth = 0;
- while(IR_RDATA == 0)
- {
- pulseWidth++;
- delay_us(19);
- }
- if(pulseWidth<=15 || pulseWidth>=2000) // >40ms || <300sus 則結(jié)束記錄
- break;
- PulseTab[i] = pulseWidth*20;
- i++;
- }
- }
- PulseTab[i++] = pulseWidth;
- PulseTab[i] = 0xffff;
-
- Flag_LearnState = 1;
- Infrared_Send();
- EXTI_ClearITPendingBit(EXTI_Line1);
- return;
- }
- #endif
- #ifdef INFRARED_SEND
- /**
- * @name void Infrared_Send()
- * @description 紅外發(fā)射,根據(jù) PulseTab[]內(nèi)的數(shù)據(jù)發(fā)波形
- * @param
- * @return
- * @notice
- */
- void Infrared_Send()
- {
- u16 i;
-
- EXTI->IMR &= ~(0x00000002); //關(guān)中斷,避免發(fā)送的紅外線被自己接受
- for(i=0; i<MAX_PULSE_LEN && PulseTab[i]!=0xffff; i++)
- {
- if(i%2 == 0)
- {
- TIM_Cmd(TIM2,ENABLE);
- delay_us(PulseTab[i]);
- TIM_Cmd(TIM2,DISABLE);
- GPIO_SetBits(GPIOA,GPIO_Pin_0);
- }
- else
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_0);
- delay_us(PulseTab[i]);
- }
- }
- GPIO_ResetBits(GPIOA,GPIO_Pin_0);
-
- EXTI->IMR |= (0x00000002); //開中斷
- }
- /**
- * @name void TIM2_PWM_Init(u16 arr,u16 psc)
- * @description 初始化定時(shí)器2的設(shè)置,將定時(shí)器2用于PWM調(diào)制,PWM輸出口為 PA.0
- * @param arr -- u16,定時(shí)器重裝值
- psc -- u16,定時(shí)器分頻值
- * @return
- * @notice PWM頻率 = 72M/((arr+1)*(psc+1)),這里用作紅外發(fā)射的載波,需要生成38kHz的方波,故取arr = 1895,psc = 0。
- */
- void TIM2_PWM_Init(u16 arr,u16 psc)
- {
- /* 初始化結(jié)構(gòu)體定義 */
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* 使能相應(yīng)端口的時(shí)鐘 */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定時(shí)器2時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外設(shè)時(shí)鐘
-
- /* GPIOA.0初始化 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // TIM2 CH1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // PA.0 復(fù)用推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_0);
- /* TIM2 初始化*/
- TIM_TimeBaseInitStructure.TIM_Period = arr; //下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc; //作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值
- TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時(shí)鐘分割:TDTS = Tck_tim
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計(jì)數(shù)模式
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
- /* 定時(shí)器TIM2 Ch1 PWM模式初始化 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時(shí)器模式:TIM PWM1
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
- //TIM_OCInitStructure.TIM_Pulse = (arr+1)/2; //占空比 50%
- TIM_OCInitStructure.TIM_Pulse = (arr+1)/3; //占空比1:3
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
- TIM_OC1Init(TIM2, &TIM_OCInitStructure);
- /* 使能TIM2在CCR1上的預(yù)裝載寄存器 */
- TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
- /* 使能定時(shí)器 */
- // TIM_Cmd(TIM2,ENABLE);
- }
- #endif
-
- /**
- * @name
- * @description
- * @param
- * @return
- * @notice
- */
-
-
-
-
- #ifdef DEBUG
- /*******************************************************************************
- * Function Name : assert_failed
- * Description : Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * Input : - file: pointer to the source file name
- * - line: assert_param error line source number
- * Output : None
- * Return : None
- *******************************************************************************/
- void assert_failed(u8* file, u32 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) */
-
- while (1)
- {}
- }
- #endif
復(fù)制代碼
所有資料51hei提供下載:
空調(diào).7z
(195.4 KB, 下載次數(shù): 129)
2020-5-9 19:06 上傳
點(diǎn)擊文件名下載附件
|
|