⬆️ ⬇️

IR remote on stm32





Hello.



Description of the library for reading, decoding and subsequent sending of infrared signals from various household consoles, using the stm32 microcontroller. Based on the IRremote library for Arduino, and adapted to stm32.



The library uses one timer, both for receiving and sending a signal. The receiver is connected to any pin (GPIO_Input), and the transmitter is connected to one of the timer channels operating in the PWM Generation mode (PWM). The example uses the first channel of timer number 4 - PB6 (transmitter) and pin PB5 (receiver).

')

To receive a signal, the timer operates in interrupt mode — every 50 µs checks the state of the input pin, and when transmitted, switches to PWM Generation mode, sends a signal, and then goes into interrupt mode.



Settings are in the file IRremote.h



extern TIM_HandleTypeDef htim4; //     -   50  (     72) #define MYPRESCALER 71 //   1 #define MYPERIOD 49 // 50  //     -     #define MYSYSCLOCK 72000000 //     recive_IR #define RECIV_PIN (HAL_GPIO_ReadPin(recive_IR_GPIO_Port, recive_IR_Pin)) 


If you configure another timer, you need to specify the appropriate structure name - htim4, and do the same in the IRremote.c and irSend.c files. I was too lazy to defain all this economy. When choosing another timer in Cuba, you need to specify only the channel and the internal clock source ...







All the rest of the program will configure itself. If you select a different channel number, you also need to rename it in the irSend.c file.



With the rest of the settings, I think everything is clear - based on the system frequency (in the example 72 MHz), the values ​​of the prescaler and overflow are substituted for interrupting every 50 μs. The following indicates the frequency and reading pin.



Below are defines that define the protocols involved ...



 ////////////////////////////////////   //////////////////////////////////////// #define DECODE_RC5 1 //     RC5   0 #define SEND_RC5 1 //       RC5   0 #define DECODE_RC6 1 #define SEND_RC6 1 ... 


Disabling unnecessary protocols reduces the size of the program. In principle, you can even cut the functions of unused protocols and the corresponding files (the files have distinctive names).



The program is extremely simple, the my_decode (& results) function decodes the received signal and outputs the button code, protocol type and packet length ...







I have no other remotes.



To send a decoded signal, use the function with the appropriate name ...



 sendSAMSUNG(0x707048b7, 32); my_enableIRIn(); 


The my_enableIRIn () function is necessary, it turns off the PWM and puts the timer into receive mode. The same function is used for initialization (before an infinite loop). Because of this function, you will not be able to catch your own signal - this can be solved, but I don’t see any sense in it.



If you can not determine the type of protocol ...





... then there is nothing terrible in this, the button code is received anyway.



If you need not only to receive an unknown signal, but also to send it, then you need to uncomment the lines for “displaying raw data” ...







... and send the read data using the sendRaw () function ...



 uint8_t khz = 38; //    ,    36  40  unsigned int raw_signal[] = {1300, 400, 1300, 400, 450, 1200, 1300, 400, 1300, 400, 450, 1200, 500, 1200, 450, 1250, 450, 1200, 500, 1200, 450, 1250, 1300}; sendRaw(raw_signal, sizeof(raw_signal) / sizeof(raw_signal[0]), khz); my_enableIRIn(); //   ( ) 


My sending raw does not work well.



The library uses a DWT counter for microsecond delays. As far as I know, not all stm32 have it, and it is possible that it is not configured the same everywhere. If your stone does not have a DWT, then you need to come up with something to replace in the custom_delay_usec (unsigned long us) function at the end of the irSend.c file, the setting at the beginning.



It's all.



Library

Source: https://habr.com/ru/post/456890/



All Articles