这是我的源代码,基本上是按照原子哥的写的,现在有两个问题,
第一个是中断只进入了4到5次,不是应该进入30多次吗?
第二个问题是,我打印出了高电平的时间,发现定时器好像是10us计数一次,不知道哪里有问题,
[mw_shl_code=c,true]#include "ir.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
#include "misc.h"
unsigned char gbIRCode;
bit IRflg = 0;
#define RDATA GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
//RmtSta
//bit7 to indicate recive the lead code
//bit6 indicate recive all inforation of a key
//bit5 reversed
//bit4flag to indicate the high level captured
//[3:0] times the key pressed
BYTE RmtSta = 0;
u16 Dval;
u32 RmtRec = 0;
BYTE RmtCnt = 0;
static char countup = 0;
short time[34];
static char countdown = 0;
void IR_State_Init(void)
{
//something to be initialized
}
void IR_Init( void )
{
GPIO_InitTypeDef GPIO_Initstruct;
NVIC_InitTypeDef NVIC_Initstruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_Initstruct.GPIO_Pin = GPIO_Pin_0;
GPIO_Initstruct.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Initstruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_Initstruct);
GPIO_SetBits(GPIOA, GPIO_Pin_0);
TIM_TimeBaseStruct.TIM_Period = 10000;
TIM_TimeBaseStruct.TIM_Prescaler = (72 - 1);
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0X03;
TIM_ICInit(TIM2, &TIM_ICInitStruct);
TIM_Cmd(TIM2, ENABLE);
NVIC_Initstruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_Initstruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_Initstruct.NVIC_IRQChannelSubPriority = 3;
NVIC_Initstruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_Initstruct);
TIM_ITConfig(TIM2, TIM_IT_Update |TIM_IT_CC1, ENABLE); //
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
if (RmtSta & 0x80)
{
RmtSta &= ~0X10;
if ((RmtSta & 0X0F) == 0X00)RmtSta |= 1 << 6;
if ((RmtSta & 0X0F) < 14)RmtSta++;
else
{
RmtSta &= ~(1 << 7);
RmtSta &= 0XF0;
}
}
}
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
if (RDATA)
{
TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Falling);
TIM_SetCounter(TIM2, 0);
RmtSta |= 0X10;
countup++;
//printf("countup %d",countup);
} else
{
//printf("countdown %d",countdown);
Dval = TIM_GetCapture1(TIM2);
printf("interval time %d
",Dval);
TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Rising);
if (RmtSta & 0X10)
{
if (RmtSta & 0X80)
{
if (Dval > 300 && Dval < 800)
{
RmtRec <<= 1;
RmtRec |= 0;
} else if (Dval > 1400 && Dval < 1900)
{
RmtRec <<= 1;
RmtRec |= 1;
} else if (Dval > 2200 && Dval < 2600)
{
RmtCnt++;
RmtSta &= 0XF0;
}
} else if (Dval > 4200 && Dval < 5000)
{
RmtSta |= 1 << 7;
RmtCnt = 0;
}
}
RmtSta &= ~(1 << 4);
}
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update | TIM_IT_CC1);
}
BYTE Remote_Scan( void )
{
BYTE sta = 0;
BYTE t1, t2;
if (RmtSta & (1 << 6))
{
t1 = RmtRec >> 24;
t2 = (RmtRec >> 16) & 0xFF;
if ((t1 == (BYTE)~t2) && (t1 == REMOTE_ID))
{
t1 = RmtRec >> 8;
t2 = RmtRec;
if (t1 == (BYTE)~t2)
{
sta = t1;
}
}
if ((sta == 0) || ((RmtSta & 0x80) == 0))
{
RmtSta &= ~(1 << 6);
RmtCnt = 0;
}
}
//printf("countup %d
",countup);
//printf("countdown %d
",countdown);
return (unsigned char)RmtRec<<16;
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>