手头有一个STM32F103C8的板子,PB0接了一个VS1838B红外接收头,电路是手册推荐的,VCC串100欧电阻,OUT加22K上拉。但是现在有个奇怪的现象,我开EXIT0中断接收脉冲,总是只能得到18位的数据,用示波器看了一下,第17和第18位的脉冲间隔居然有10.2MS,貌似是16位的数据码被混到一起了,低电平下不去。电路检测了N回,没有发现问题,上拉电阻也试过换成10K,现象依旧。去掉上拉电阻把GPIO设置成上拉输入,甚至浮动输入,现象依旧。起初以为是接收头的问题,换了N个还是一样,最后拆下来直接放到以前做的一块51板上,一点问题没有,说明接收头也是正常的。看了一眼手册,这个接收头的工作电压从2.7~5.5都行,应该不是电压的问题,后来中断改成脉冲计数方式也不行,而且脉冲数据很稳定,每次都是一样的,不像是有干扰。不知道有没有哪位朋友碰到过这种现样没,调了N天,实在无解了。这么简单的东西居然一点办法都没有,中断配置如下:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
//设置中断通道
nvic_init_struct.NVIC_IRQChannel = EXTI0_IRQn ;
nvic_init_struct.NVIC_IRQChannelCmd = ENABLE;
nvic_init_struct.NVIC_IRQChannelPreemptionPriority = 0;
nvic_init_struct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvic_init_struct);
//初始化GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = RF_PIN;
GPIO_Init(GPIO_RF_PORT,&GPIO_InitStructure);
//中断引脚设置
GPIO_EXTILineConfig(IT_RF_PORT,IT_RF_PIN);
//设置中断
exit_init_struct.EXTI_Line = IT_RF_LINE;
exit_init_struct.EXTI_LineCmd = ENABLE;
exit_init_struct.EXTI_Mode = EXTI_Mode_Interrupt;
exit_init_struct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_Init(&exit_init_struct);
//EXTI_GenerateSWInterrupt(IT_RF_LINE);
//设置定时器3用于红外解码
nvic_init_struct.NVIC_IRQChannel = TIMER_RF_IRQ ;
nvic_init_struct.NVIC_IRQChannelCmd = ENABLE;
nvic_init_struct.NVIC_IRQChannelPreemptionPriority = 0;
nvic_init_struct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&nvic_init_struct);
RCC_APB2PeriphClockCmd(APB_RF,ENABLE);
TIM_TimeBaseStructure.TIM_Period = 10;
TIM_TimeBaseStructure.TIM_Prescaler = 719; //72M的系统时钟做720分频,计数10次,10分之一MS中断一次
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIMER_RF, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIMER_RF,TIM_FLAG_Update);//清除中断,
TIM_ITConfig(TIMER_RF, TIM_IT_Update, ENABLE);
TIM_Cmd(TIMER_RF,ENABLE);
中断代码:
//中断函数
volatile u8 time=0;
u8 data[36];
u8 start = 1;
volatile u8 bit = 0;
u32 code;
void EXTI0_IRQHandler(void){
EXTI_ClearITPendingBit(IT_RF_LINE);
if(start){
time = 0;
start = 0;
}
else{
data[bit] = time;
bit++;
start = 1;
if(bit == 33){
bit = 0;
code =rf_decode(data);
}
}
}
void TIM1_UP_IRQHandler(void){
TIM_ClearITPendingBit(TIMER_RF,TIM_FLAG_Update);
time++;
} |