欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

stm32红外通信3简

时间:2023-05-26

bsp_exti.c

#include "bsp_exti.h" static void NVIC_Configuration(void) //中断优先级函数{ NVIC_InitTypeDef NVIC_InitStructure; /* 配置NVIC为优先级组1 */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //配置NVIC为优先级组1 NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; //配置中断源: /*EXTI0_IRQn/EXTI1_IRQn/ EXTI2_IRQn/EXTI3_IRQn/EXTI4_IRQn/EXTI9_5_IRQn/EXTI15_10_IRQn*/ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //配置抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //配置子优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能中断通道 NVIC_Init(&NVIC_InitStructure); //写入结构体成员 } void remote_EXTI_init(void){ GPIO_InitTypeDef GPIO_InitStructure; //GPIO结构体EXTI_InitTypeDef EXTI_InitStructure; //中断结构体 NVIC_Configuration(); //中断优先级函数 //初始化GPIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); //开启GPIO口的时钟//开启按键GPIO口的时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // 哪个引脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //配置为浮空输入 GPIO_Init(GPIOE, &GPIO_InitStructure); //写入结构体GPIO成员 //初始化EXTI RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //开启复用GPIO口的时钟 //开启按键GPIO口的时钟 GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5); //选择EXTI的信号源GPIOE,pin5 EXTI_InitStructure.EXTI_Line = EXTI_Line5; //选择中断源,引脚哪个就选择哪个EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //上升沿和下降沿都触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断 EXTI_Init(&EXTI_InitStructure); //写入结构体中断成员}

remote.c

#include "remote.h"#include "bsp_exti.h"extern u32 count_100us;u32lt=0; //记录低电平时间u32ht=0; //记录低电平时间u8ind=0; //记录32个数据码u8 remotecode[32]; //数组来保存地址码,地址反码,数据码,数据反码voidEXTI9_5_IRQHandler(void) {u8triger;if(EXTI_GetITStatus(EXTI_Line5) != RESET) {//清除中断标志位EXTI_ClearITPendingBit(EXTI_Line5); triger=GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5);//读取端口电平//上升沿if(triger==1){lt=count_100us; //捕获上升沿时间count_100us=0;}//下降沿else{ht=count_100us; //捕获下降沿时间count_100us=0;//=========================解码===========================////判断前导码来了没有if(ind==0){if((lt<100)&&(lt>80)&&(ht<50)&&(ht>40)) //判断前导码是否正确{ind++;}}else{ if((lt<8)&&(lt>4)&&(ht<7)&&(ht>3)){remotecode[ind-1]=0;} //如果低是600us,高是500us,表示0 else if((lt<8)&&(lt>4)&&(ht<18)&&(ht>14)){remotecode[ind-1]=1;} //如果低是600us,高是1600us,表示1 else{ind=0;return;} //否则解码失败,退出ind++;if(ind>=33) //总共有32个高低电平{ind=0;if(remotecode[18]==1&&remotecode[19]==1){LED1_ON;} //判断数据位是否正确else{LED1_OFF;}}}}} }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。