这是之前不能运行的代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
uint8_t IrControl_GetFlag;
uint32_t IrControl_Data;
/*红外遥控函数初始化*/
void IrControl_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line=EXTI_Line8;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
}
/*获取高电平的持续时间*/
uint8_t IrControl_GetTime(void)
{
uint8_t time=0;
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) == 1)
{
time++;
Delay_us(20);
if(time>=250) return time;
}
return time;
}
void EXTI9_5_IRQHandler(void)
{
uint8_t Time=0,Data,Num=0,read=0;
while(1)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) == 1)
{
Time=IrControl_GetTime();
if(Time>=250) break;
if(Time>=200 && Time<250) //高电平持续4500us左右,接收到码头
{
read=1;
}
else if(Time>=10 && Time<50) //高电平持续560us左右 输出为0
{
Data=0;
}
else if(Time>=60 && Time<90) //高电平持续1690us左右 输出为1
{
Data=1;
}
if(read == 1)
{
IrControl_Data<<=1;
IrControl_Data+=Data;
if(Num>=32)
{
IrControl_GetFlag=1;
break;
}
}
Num++;
}
}
EXTI_ClearITPendingBit(EXTI_Line8);
}
这是修改之后可以运行的代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#define IRED_PORT GPIOA
#define IRED_PIN GPIO_Pin_8
#define IRED_PORT_RCC RCC_APB2Periph_GPIOA
uint8_t IR_Receiveflag;
uint32_t IR_Receivecode;
/*红外遥控函数初始化*/
void IRremote_Init(void)
{
RCC_APB2PeriphClockCmd(IRED_PORT_RCC|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=IRED_PIN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource8);
EXTI_ClearITPendingBit(EXTI_Line8);
EXTI_InitStructure.EXTI_Line=EXTI_Line8;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*获取高电平的持续时间*/
uint8_t IRremote_Counttime(void)
{
u8 t=0;
while(GPIO_ReadInputDataBit(GPIOA,IRED_PIN) == 1)
{
t++;
Delay_us(20);
if(t>=250) return t;
}
return t;
}
void EXTI9_5_IRQHandler(void)
{
uint8_t Time=0,Data,Num=0,read=0;
while(1)
{
if(GPIO_ReadInputDataBit(GPIOA,IRED_PIN) == 1)
{
Time=IRremote_Counttime();
if(Time>=250) break;
if(Time>=200 && Time<250) //高电平持续4500us左右,接收到码头
{
read=1;
}
else if(Time>=60 && Time<90) //高电平持续1690us左右 输出为1
{
Data=1;
}
else if(Time>=10 && Time<50) //高电平持续560us左右 输出为0
{
Data=0;
}
if(read == 1)
{
IR_Receivecode<<=1;
IR_Receivecode+=Data;
if(Num>=32)
{
IR_Receiveflag=1;
break;
}
}
Num++;
}
}
EXTI_ClearITPendingBit(EXTI_Line8);
}
请问为什么只是变了几个变量名字就可以运行了,我找bug找了特别久没想到这样解决了
|