用STM32输入捕获功能做一个红外接收系统,接收波形,把每个波形的持续时间记录到数组里。但是测试的时候发现,板子重启或刚刚烧录完程序后,第一次接收红外信号的时候,捕获不到第一个电平信号。但是从第二次再接收就能接收到了,求解这是什么问题。
以下是我的程序
#include "sys.h"
#include "stm32f10x.h"
#include "receive.h"
#include "delay.h"
#include "usart.h"
#include "port.h"
#include "led.h"
#define START 0X01
#define WAIT_RISING 0X02
#define WAIT_FALLING 0X03
void receive_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_0); //3õê¼»ˉGPIOA.6
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler =(72-1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x03;
TIM_ICInit(TIM5, &TIM_ICInitStructure)
TIM_Cmd(TIM5,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel=TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
}
u8 i=0;
u8 state=START;
u16 buf[200];
u8 overflow;
u16 GET_VAL;
u8 get;
u8 len;
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)!= RESET)
{
if(state==WAIT_RISING||state==WAIT_FALLING)
{
if(overflow<5) ++overflow;
else
{
overflow=0;
get|=0x01;
}
}
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC1)!= RESET)
{
if(RDATA)
{
if(state==WAIT_RISING)
{
GET_VAL=TIM_GetCapture1(TIM5);
buf[i]=GET_VAL+overflow*65536;
i++;
}
overflow=0;
TIM_SetCounter(TIM5,0);
state=WAIT_FALLING;
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);
}
else
{
if(state==WAIT_FALLING)
{
GET_VAL=TIM_GetCapture1(TIM5);
buf[i]=GET_VAL+overflow*65536;
i++;
}
overflow=0;
TIM_SetCounter(TIM5,0);
state=WAIT_RISING;
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
}
下面是主程序
#include "sys.h"
#include "stm32f10x.h"
#include "receive.h"
#include "delay.h"
#include "usart.h"
#include "port.h"
#include "led.h"
#define START 0x01
extern u8 i;
extern u8 len;
extern u8 get;
extern u16 buf[];
extern u8 state;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
receive_Init();
uart_init(115200);
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
if((get&0x01)==0x01)
{
len=i;
for(i=0;i<len;i++)
{
printf("%d
",i);
printf("%d us
",buf[i]);
}
get=0;
i=0;
len=0;
state=START;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>