当前位置:网站首页>STM32 infrared communication
STM32 infrared communication
2022-06-30 07:28:00 【weixin_ forty-six million one thousand two hundred and twenty-o】
bsp_exti.c
#include "bsp_exti.h"
static void NVIC_Configuration(void) // Interrupt priority function
{
NVIC_InitTypeDef NVIC_InitStructure;
/* To configure NVIC For priority group 1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // To configure NVIC For priority group 1
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; // Configure interrupt sources :
/*EXTI0_IRQn/EXTI1_IRQn/ EXTI2_IRQn/EXTI3_IRQn/EXTI4_IRQn/EXTI9_5_IRQn/EXTI15_10_IRQn*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Configure preemption priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // Configure sub priority
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable interrupt channel
NVIC_Init(&NVIC_InitStructure); // Write structure members
}
void remote_EXTI_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO Structure
EXTI_InitTypeDef EXTI_InitStructure; // Interrupt structure
NVIC_Configuration(); // Interrupt priority function
// initialization GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); // Turn on GPIO The clock of the port // Turn on the key GPIO The clock of the port
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // Which pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // Configure as floating input
GPIO_Init(GPIOE, &GPIO_InitStructure); // Write structure GPIO member
// initialization EXTI
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); // Turn on reuse GPIO The clock of the port // Turn on the key GPIO The clock of the port
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5); // choice EXTI The source of the signal GPIOE,pin5
EXTI_InitStructure.EXTI_Line = EXTI_Line5; // Select interrupt source , Choose which pin
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // Interrupt mode
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // Both rising and falling edges trigger
EXTI_InitStructure.EXTI_LineCmd = ENABLE; // To interrupt
EXTI_Init(&EXTI_InitStructure); // Write struct interrupt member
}
Insert a code chip here
remote.c
#include "remote.h"
#include "bsp_exti.h"
extern u32 count_100us;
u32 lt=0; // Record the low level time
u32 ht=0; // Record the low level time
u8 ind=0; // Record 32 A data code
u8 remotecode[4]; // Array to hold the address code , Address inversion , Data code , Data inversion
u8 remotestat=0; //0 Indicates that it is not decoded ,1 Indicates decoding ,2 Means to solve the code
void EXTI9_5_IRQHandler(void)
{
u8 triger;
static u8 dat,i,j;
if(EXTI_GetITStatus(EXTI_Line5) != RESET)
{
// Clears the interrupt flag bit
EXTI_ClearITPendingBit(EXTI_Line5);
triger=GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5); // Read port level
// Rising edge
if(triger==1)
{
lt=count_100us; // Capture rising edge time
count_100us=0;
}
// Falling edge
else
{
ht=count_100us; // Capture falling edge time
count_100us=0;
//========================= decode ===========================//
// Judge whether the leading code has come
if(remotestat==0)
{
if((lt<100)&&(lt>80)&&(ht<50)&&(ht>40)) // Determine whether the preamble is correct
{
remotecode[0]=remotecode[1]=remotecode[2]=remotecode[3]=0; // Address code , Address inversion , Data code , Data inversion Starting from scratch
ind=i=0;
remotestat=1;// The preamble correctly enters the decoding -- Decode data code
}
}
else if(remotestat==1) // The preamble correctly enters the decoding -- Decode data code
{
j=ind>>3;//j=ind/8;// The first ind A size Put in the... Of the array j In a byte
if((lt<8)&&(lt>4)&&(ht<7)&&(ht>3)){
remotecode[j]|=0x00;} // If low is 600us, Gao Shi 500us, Express 0
else if((lt<8)&&(lt>4)&&(ht<18)&&(ht>14)){
remotecode[j]|=0x80;} // If low is 600us, Gao Shi 1600us, Express 1
else{
remotestat=0;return;} // Otherwise, decoding fails , sign out
ind++; // Record how many high and low levels
i++; // Record my remotecode[j] How many times
if(i!=8){
remotecode[j]>>=1;} // Every move 8 Save one bit array at a time
else{
i=0;}
if(ind>=32) // All in all 32 High and low levels
{
ind=0;
if((remotecode[2]+remotecode[3])==0xff) // Judge whether the positive and negative data codes are correct
{
remotestat=2; // Correct, then enter the function we want to implement
}
else
{
remotestat=0; // Otherwise, if the data code is incorrect , sign out
}
}
}
}
}
}
边栏推荐
- Install go language development tools
- Qtcreator debug code after configuring CDB debugger view variable value display card
- Resolved: initialize specified but the data directory has files in it Aborting
- 1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability
- QT elementary notes
- grep命令用法
- LabVIEW program code update is slow
- Adjacency matrix representation of weighted undirected graph (implemented in C language)
- The simulation interface does not declare an exception and throws an exception
- How crazy are young people in sideline industry: 3000 monthly salary and 3W sideline income
猜你喜欢

Matter protocol

Basic knowledge of system software development

How to determine the size of the platform byte order?

Swiftui creates a beautiful custom press feedback button

Stm32g0 Tim interrupt use

Can introduction

Minecraft 1.16.5 module development (50) guide book

年轻人搞副业有多疯狂:月薪3000,副业收入3W

Application of stack -- using stack to realize bracket matching (C language implementation)

Install go language development tools
随机推荐
Next initializesecuritycontext failed: unknown error (0x80092012) - the revocation function cannot check whether the certificate is revoked.
uniapp图片下方加标签标图片
期末复习-PHP学习笔记9-PHP会话控制
Ad usage notes
1.someip introduction
社招两年半10个公司28轮面试面经
28 rounds of interviews with 10 companies in two and a half years
Can introduction
Record the problem that the system file cannot be modified as an administrator during the development process
Lt268 the most convenient TFT-LCD serial port screen chip in the whole network
The most convenient serial port screen chip scheme designed at the charging pile in China
我今年毕业,但我不知道我要做什么
Use of ecostruxure (2) IEC61499 to establish function blocks
Golan common shortcut key settings
网络安全-VLAN和Tunk方法详解
Thread network
LabVIEW program code update is slow
Win10 step pit - power on 0xc0000225
DXP software uses shortcut keys
Cubemx completes STM32F103 dual serial port 485 transceiver transmission