当前位置:网站首页>Stm32f030f4 reading infrared remote control data

Stm32f030f4 reading infrared remote control data

2022-06-24 23:35:00 mialo163

One frame data structure (NEC agreement )

 

  data 0 And data 1 It means

 

  The phase of the signal received at the receiving head is opposite

Configure external interrupts

EXTI_InitTypeDef   EXTI_InitStructure;
GPIO_InitTypeDef   GPIO_InitStructure;
NVIC_InitTypeDef   NVIC_InitStructure;
  
 void EXTI0_Config(void)
{
  /* Enable GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Configure PA0 pin as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  /* Connect EXTI0 Line to PA0 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

  /* Configure EXTI0 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //EXTI_Trigger_Falling;// EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI0 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

Obtain high level pulse width time

u8 HW_ReceiveTime(void)
{
	u8 t=0;
 
	while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)//???
	{
		t++;
		delay_us(20);//20us In units of 
 
		if(t>=250) return t;//????
	}
 
	return t;
}

External interrupt processing

void EXTI0_1_IRQHandler()	  
{
	 u8 time=0,startSignal=0,Data=0,Count=0;
	 unsigned long HW_ReceivedData=0;
	 
	 if(EXTI_GetITStatus(EXTI_Line0) != RESET)//?????EXTI??????????,????EXTI_Line???
      {
           	
	 		//if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))  GPIO_SetBits(GPIOA,GPIO_Pin_0);//??LED
			//else  GPIO_ResetBits(GPIOA,GPIO_Pin_0);//?
			while(1)
			{
				
					if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
					{
						 time = HW_ReceiveTime();
						
						  
						 if(time>=250)
						 {
						 	
							startSignal=0;
							HW_ReceivedData=0;
							Count=0;
							 
							break;
						 } 
						 else if(time>=200 && time<250)// Pilot code  4.5ms
						 {
						 	  startSignal=1;//??????
								
							  HW_ReceivedData=0;
							  Count=0;
							  continue;
						 } 
						 else if(time>=60 && time<90) // Level width 1.5ms
						 {
							
							 Data=1;//???? 1
						 }
						 else if(time>=10 && time<50)  // Level width 0.6ms
						 {
							 Data=0;//???? 0
						 }
//						  else
//						{
//							break;
//						
//						}
						 

						 if(startSignal==1)
						  {
							 	HW_ReceivedData<<=1;
								HW_ReceivedData+=Data;// Deposit data
								Count++;
								if(Count>=32)// full 32 Bit fetch number 
								{
//									for(i=0;i<4;i++)
//									{
//										printf("%x\n",HW_ReceivedData);
//									}
									//printf("0x%lx\n",HW_ReceivedData);
									unsigned char cha[4]= {0};
									
									cha[0] = (HW_ReceivedData & 0xFF000000)>>24;
									cha[1] = (HW_ReceivedData & 0x00FF0000)>>16;
									cha[2] = (HW_ReceivedData & 0x0000FF00)>>8;
									cha[3] = HW_ReceivedData & 0x000000FF;
									if(cha[0]==0x00&&cha[1]==0xFF)// User code 
									{
										if(cha[2]==0x90&&(cha[2]+cha[3])==0xFF)// Judge the inverse code 
										{
                                               //.....  Get encoded data processing 



                                        }
                                    }
                                }		
				    }							
			}
			//    /* Clear the EXTI line 0 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line0);
//          	EXTI_ClearITPendingBit(EXTI_Line0);//???????
          EXTI_ClearFlag(EXTI_Line0);//??????? 
     } 
}

 

 

原网站

版权声明
本文为[mialo163]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211119255536.html