Table of contents
learning target
The content of this section is about the knowledge of infrared remote control. In fact, it was introduced in detail in 51, so I won't repeat it here, because the knowledge points are exactly the same, that is, the code is written a little differently, but the principle is the same.
content
(16) 51 single-chip microcomputer - infrared remote controlhttps://blog.csdn.net/weixin_66578482/article/details/126141850 This note introduces the infrared remote control in detail, and interested students can take a look.
introduce
Infrared remote control is a wireless, non-contact control technology, with strong anti-interference ability, reliable information transmission, low power consumption, low cost, easy implementation, etc. It is widely used by many electronic devices, especially household appliances, and is becoming more and more Many applications are used in computer systems.
Since the infrared remote control does not have the ability to control the controlled object through obstacles like the radio remote control, when designing the infrared remote control, it is not necessary for each set (transmitter and receiver) to be different like the radio remote control. The remote control frequency or code (otherwise, the wall will control or interfere with the neighbor's household appliances), so the infrared remote control of similar products can have the same remote control frequency or code, and there will be no remote control signal "drop-in" situation. This provides great aspects for mass production and popularizing infrared remote control on household appliances. Since infrared light is invisible light, it has little impact on the environment, and the wavelength of infrared light fluctuation is much smaller than that of radio waves, so infrared remote control will not affect other household appliances, nor will it affect nearby radio equipment.
code
//remote.c #include "remote.h" #include "delay.h" #include "usart.h" //Infrared remote control initialization //Set IO and input capture of TIM2_CH1 void Remote_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM1_ICInitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//Enable GPIOA clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//TIM1 clock enable //GPIOA8 alternate function, pull up GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Multiplexing function GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//Push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//pull up GPIO_Init(GPIOA, &GPIO_InitStructure);//initialization GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //GPIOA8 is multiplexed as TIM1 TIM_TimeBaseStructure.TIM_Prescaler=167; prescaler,1M count frequency,1us plus 1. TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //count up mode TIM_TimeBaseStructure.TIM_Period=10000; //Set counter auto-reload value Maximum 10ms overflow TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //Initialize TIM2 input capture parameters TIM1_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Select input IC1 to map to TI1 TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Rising edge capture TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Mapped to TI1 TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //Configure input frequency division, no frequency division TIM1_ICInitStructure.TIM_ICFilter = 0x03;//IC1F=0003 8 timer clock cycle filtering TIM_ICInit(TIM1, &TIM1_ICInitStructure);//Initialize Timer 2 input capture channel TIM_ITConfig(TIM1,TIM_IT_Update|TIM_IT_CC1,ENABLE);//Allow update interrupt, allow CC1IE to capture interrupt TIM_Cmd(TIM1,ENABLE ); //enable timer 1 NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//preempt priority 1 NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //subpriority 3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable NVIC_Init(&NVIC_InitStructure); //Initialize NVIC registers NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//preempt priority 3 NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //subpriority 2 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable NVIC_Init(&NVIC_InitStructure); //Initialize NVIC registers } //Remote control reception status //[7]: Received the boot code flag //[6]: Got all the information of a button //[5]: Reserved //[4]: Whether the rising edge of the flag has been captured //[3:0]: Overflow timer u8 RmtSta=0; u16 Dval; //The value of the counter at the falling edge u32 RmtRec=0; //IR received data u8 RmtCnt=0; //number of key presses //Timer 1 overflow interrupt void TIM1_UP_TIM10_IRQHandler(void) { if(TIM_GetITStatus(TIM1,TIM_IT_Update)==SET) //overflow interrupt { if(RmtSta&0x80)//Last time data was received { RmtSta&=~0X10; //Cancel the rising edge has been captured flag if((RmtSta&0X0F)==0X00)RmtSta|=1<<6;//Marking that the collection of key-value information for a key has been completed if((RmtSta&0X0F)<14)RmtSta++;// more than 130ms else// no key press { RmtSta&=~(1<<7);//clear boot logo RmtSta&=0XF0; //clear counter } } } TIM_ClearITPendingBit(TIM1,TIM_IT_Update); //clear interrupt flag } //Timer 1 Input Capture Interrupt Service Routine void TIM1_CC_IRQHandler(void) { if(TIM_GetITStatus(TIM1,TIM_IT_CC1)==SET) //Handling capture (CC1IE) interrupts { if(RDATA)//Rising edge capture { TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Falling); //CC1P=1 set as falling edge capture TIM_SetCounter(TIM1,0); //clear timer value RmtSta|=0X10; //Flag rising edge has been captured }else //Falling edge capture { Dval=TIM_GetCapture1(TIM1);//Reading CCR1 can also clear the CC1IF flag TIM_OC1PolarityConfig(TIM1,TIM_ICPolarity_Rising); //CC1P=0 set as rising edge capture if(RmtSta&0X10) //Complete a high level capture { if(RmtSta&0X80)//received boot code { if(Dval>300&&Dval<800) //560 is the standard value, 560us { RmtRec<<=1; //Shift one place to the left. RmtRec|=0; //received 0 }else if(Dval>1400&&Dval<1800) //1680 is the standard value, 1680us { RmtRec<<=1; //Shift one place to the left. RmtRec|=1; //received 1 }else if(Dval>2200&&Dval<2600) //Get the information that the key value of the key is increased, 2500 is the standard value of 2.5ms { RmtCnt++; //Increase the number of keys by 1 RmtSta&=0XF0; //clear timer } }else if(Dval>4200&&Dval<4700) //4500 is the standard value of 4.5ms { RmtSta|=1<<7; //Marks that the boot code was successfully received RmtCnt=0; //Clear keystroke counter } } RmtSta&=~(1<<4); } } TIM_ClearITPendingBit(TIM1,TIM_IT_CC1); //clear interrupt flag } //Handling infrared keyboards //return value: // 0, no key is pressed //Other, the key value of the key pressed. u8 Remote_Scan(void) { u8 sta=0; u8 t1,t2; if(RmtSta&(1<<6))//Get all the information about a button { t1=RmtRec>>24; //get address code t2=(RmtRec>>16)&0xff; //get address inverse code if((t1==(u8)~t2)&&t1==REMOTE_ID)//Verify remote identification code (ID) and address { t1=RmtRec>>8; t2=RmtRec; if(t1==(u8)~t2)sta=t1;//key value is correct } if((sta==0)||((RmtSta&0X80)==0))//Key data error/remote control has not been pressed { RmtSta&=~(1<<6);//Clear received valid key ID RmtCnt=0; //Clear keystroke counter } } return sta; }
// main.c #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" #include "remote.h" int main(void) { u8 key; u8 t=0; u8 *str=0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//Set system interrupt priority group 2 delay_init(168); //Initialize delay function uart_init(115200); //Initialize the serial port baud rate to 115200 LED_Init(); //Initialize the LED s Remote_Init(); //Infrared reception initialization while(1) { key=Remote_Scan(); if(key) { delay_ms(200); printf ("KEYVAL:%d",key); //show key value printf ("\r\n\r\n"); printf ("KEYCNT:%d",RmtCnt); //Display keystrokes printf ("\r\n\r\n"); switch(key) { case 0:str="ERROR";break; case 162:str="POWER";break; case 98:str="UP";break; case 2:str="PLAY";break; case 226:str="ALIENTEK";break; case 194:str="RIGHT";break; case 34:str="LEFT";break; case 224:str="VOL-";break; case 168:str="DOWN";break; case 144:str="VOL+";break; case 104:str="1";break; case 152:str="2";break; case 176:str="3";break; case 48:str="4";break; case 24:str="5";break; case 122:str="6";break; case 16:str="7";break; case 56:str="8";break; case 90:str="9";break; case 66:str="0";break; case 82:str="DELETE";break; } printf ("SYMBOL:%s",str); //show SYMBOL printf ("\r\n\r\n"); }else delay_ms(200); t++; if(t==20) { t=0; LED0=!LED0; } } }
Summarize
In fact, it is the infrared remote control in 51, there is nothing to sum up.