用拨码开关调频,下载好后可以用,不过几天后就有异常,高手能帮我看下吗?急急,异常是我没做拨码开关的消抖吗
#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;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
if (GPIOB->IDR & (1<<9) //这里的E是B也就是PB9对吗
tem+=0x80;//这句是0x7f+1的结果吗?
if ((DipSwR ^ GPIOE->IDR)&0x0F) //这里为什么不是 &0xFF --> if ((DipSwR ^ tem)
.....
....
DipSwR = GPIOE->IDR & 0x0F; --> DipSwR = tem;
{
SysTick->CTRL=0;
SysTick->VAL=0;
SysTick->LOAD = 72000-1;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk|SysTick_CTRL_TICKINT_Msk|
SysTick_CTRL_ENABLE_Msk;
}用这个systick时钟和正点原子的delay.c冲突了,只要另外写一个systick时钟整个系统就不工作了
那就随便找个计时器做呗, TIM6 或 7 都可以
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM6 , ENABLE);
TIM_DeInit(TIM6);
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInitStruct.TIM_Period = 72000-1;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct ) ;
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
void TIM6_IRQHandler(void){
if (TIM6->SR & 1){
TIM6->SR &= ~(u32)1;
....... 去抖的东东放这里
}
}
一周热门 更多>