用拨码开关调频,下载好后可以用,不过几天后就有异常,高手能帮我看下吗?急急,异常是我没做拨码开关的消抖吗
#include "stm32f10x.h"
#include "key.h"
#include "sys.h"
#include "delay.h"
#include "timer.h"
#include "led.h"
#include "24cxx.h"
extern u16 f,w;
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
{
t++;
if(t<w)
{
LED3=0;
LED4=0;
}
else
{
LED3=1;
LED4=1;
}
if(t>=f)t=0;
}
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //清除TIMx更新中断标志
}
//按键初始化函数
void KEY_Init(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//PB9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;//KEY0-KEY6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE0,1,2,3
}
////////////
void KEY_Scan1()//拨码开关调频
{
if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==1)//0
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=200;w=100;
}
else if(KEY0==0&&KEY1==1&&KEY2==1&&KEY3==1)//1
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=140;w=70;
}
else if(KEY0==1&&KEY1==0&&KEY2==1&&KEY3==1)//2
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=100;w=50;
}
else if(KEY0==0&&KEY1==0&&KEY2==1&&KEY3==1)//3
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=70;w=35;
}
else if(KEY0==1&&KEY1==1&&KEY2==0&&KEY3==1)//4
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=50;w=25;
}
else if(KEY0==0&&KEY1==1&&KEY2==0&&KEY3==1)//5
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=40;w=20;
}
else if(KEY0==1&&KEY1==0&&KEY2==0&&KEY3==1)//6
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=30;w=15;
}
else if(KEY0==0&&KEY1==0&&KEY2==0&&KEY3==1)//7
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=16;w=8;
}
//
else if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==0)//8
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=10;w=5;
}
else if(KEY0==0&&KEY1==1&&KEY2==1&&KEY3==0)//9
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
f=8;w=4;
}
else
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE); //时钟使能
LED3=0;LED4=1;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //ê±Öóê1Äü
//¶¨ê±Æ÷TIM33õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM6ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
//ÖD¶ÏóÅÏ輶NVICéèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM6ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÏèÕ¼óÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉNVIC¼Ä′æÆ÷
TIM_Cmd(TIM6, ENABLE); //ê1ÄüTIMx
}
void TIM6_IRQHandler(void)
{
static u8 flag;
u8 tem;
tem =GPIOE->IDR&0x7F;
if (GPIOB->IDR & 1<<9)
tem +=0x80;
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //¼ì2éTIM6¸üDÂÖD¶Ï·¢éúóë·ñ
{
flag++;
if(flag==4)
{
if ((DipSwR ^ tem))//消抖
{
DipSwR= tem; //消抖
}
}
else
{
flag=0;
}
}
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //Çå3yTIMx¸üDÂÖD¶Ï±êÖ¾
}
u8 disp(void)
{
tem =GPIOE->IDR&0x7F;
if (GPIOB->IDR & 1<<9)
tem +=0x80;
DipSwR=tem;
return(DipSwR);
}
void disp_scan(void)
{
switch(disp())
{
//0-9
case 0xFF:f=200;w=100;break;//0 0
case 0x7F:f=140;w=70;break;//01
case 0xFE:f=100;w=50;break;//02
........
}
}
主函数中只要调取
disp_scan();其中DipSwR为全局变量,棒我看下哈,这样就实现消抖了吧
一周热门 更多>