cancel
Showing results for 
Search instead for 
Did you mean: 

IR remote

OKuma
Associate II

I'm trying to code IR remote which uses NEC protocol using STM32f030 .

I'm able to do the code under while(1) as as a GPIO input

But i'm not able to implement it as an interrupt .

Kindly help me regarding the same

12 REPLIES 12

What have you done so far, and what exactly is your problem?

Jw

TIM_HandleTypeDef htim1;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t data;
uint8_t count;
int i;
void delay_us (uint16_t us)
{
	__HAL_TIM_SET_COUNTER(&htim1,0);  // set the counter value a 0
	while (__HAL_TIM_GET_COUNTER(&htim1) < us);  // wait for the counter to reach the us input in the parameter
}
uint32_t receive_data (void)
{
	uint32_t code=0;
 
		  /* The START Sequence begin here
	   * there will be a pulse of 9ms LOW and
	   * than 4.5 ms space (HIGH)
	   */
	  while (!(HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_9)));  // wait for the pin to go high.. 9ms LOW
 
	  while ((HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_9)));  // wait for the pin to go low.. 4.5ms HIGH
 
	  /* START of FRAME ends here*/
 
	  /* DATA Reception
	   * We are only going to check the SPACE after 562.5us pulse
	   * if the space is 562.5us, the bit indicates '0'
	   * if the space is around 1.6ms, the bit is '1'
	   */
 
	  for (int i=0; i<32; i++)
	  {
		  count=0;
 
		  while (!(HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_9))); // wait for pin to go high.. this is 562.5us LOW
 
		  while ((HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_9)))  // count the space length while the pin is high
		  {
			  count++;
			  delay_us(10);
 
		  }
 
		  if (count > 12) // if the space is more than 1.2 ms
		  {
			  code |= (1UL << (31-i));   // write 1
		  }
 
		  else code &= ~(1UL << (31-i));  // write 0
	  }
 
		return code;
}
 
void convert_code (uint32_t code)
{
		switch (code)
		{
			case (0x1FE58A7):
			//	lcd_send_cmd (0x86);
			//	lcd_send_data ('1');
		 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);
			  HAL_Delay(100);
						break;
 
			case (0x1FE40BF):
		 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
			 HAL_Delay (100);
				break;
							default :
				break;
		}
}
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
  
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
	  	  	  while (HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_9));   // wait for the pin to go low
	 	 	  data = receive_data ();
	 	 	  convert_code (data);
 
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */

I'm trying to light some LEDS connected to pins using IR remote on STM32f030

This code that I implemented waits for the pin to go low in main function .

But I want to use this as an interupt rather than a GPIO input because in mu main function I don't want to wait for the pin as rest of the things below it won't work .

SO I need help in implementing this code as as interrupt .

Set up a freely running timer.

Set one of its channels to input capture, set properly in GPIO AF to given timer channel.

Set the input capture to act upon both edges (alternatively, maybe better, you can set one channel "direct" to one edge; and the adjacent channel to act on the other edge).

Enable interrupt upon input capture, and process the incoming edges there.

JW

OKuma
Associate II

Thank you for your answer .

Can you please explain in a bit detail as i'm new to this controller or can you help me with some code or links or something that would help me understand better .

  • Thank you for your answer .
  • Can you please explain in a bit detail as i'm new to this controller or can you help me with some code or links or something that would help me understand better .
  •  
  • Like
  • Reply
  • Select as Best

Write an answer...

It's a similar problem than what ST calls PWM input. How to use timer for that, is described in the timer chapter of RM, and you should be able also to find examples in Cube.

That should be a good starting point.

JW

Piranha
Chief II

AN3174 and AN4834 are pretty nice.

Indeed. Thanks Piranha for the pointers.

JW

OKuma
Associate II

Thanks for the help

I'm able to generate PWM interrupt using input capture mode

But I'm not able to read the signal using HAL library as signal is in STM32F0 is not able to generate delay in microseconds .

Can you help regarding that