标题:
hk32f030m单片机定时器消抖,解决按键按下读取电平抖动
[打印本页]
作者:
sr861126
时间:
2023-9-21 11:31
标题:
hk32f030m单片机定时器消抖,解决按键按下读取电平抖动
用定时器按一定时间间隔扫描按键
#include "main.h"
#define A3_OFF GPIO_SetBits(GPIOA,GPIO_Pin_3);
#define A3_ON GPIO_ResetBits(GPIOA,GPIO_Pin_3);
#define u8 uint8_t
#define KEY0_Read GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)
unsigned char key0_flag = RESET;
#define BSRR_VAL 0x0006
uint8_t a=9;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
u8 Key_left=1,Key_up,Key_right, Key_down;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
static uint8_t fac_us = 0;
static uint16_t fac_ms = 0;
/**
* @brief Main program.
* @param None
* @retval None
*/
void KEY_Scan(void);
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void delay_ms(uint16_t nms)
{
uint32_t temp;
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
fac_us = SystemCoreClock/8000000;
fac_ms = (uint16_t)fac_us*1000;
if( nms > 0 )
{
SysTick->LOAD=(uint32_t)nms*fac_ms; //时间加载(SysTick->LOAD为24bit)
SysTick->VAL =0x00; //清空计数器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //开始倒数
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16))); //等待时间到达
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //关闭计数器
SysTick->VAL =0X00; //清空计数器
}
}
void KEY_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Schmit = GPIO_Schmit_Disable;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void GPIO_CONIG()
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7| GPIO_Pin_2| GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 842; // 32M/38K = 842.10
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse =421;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
//定时器3中断服务程序
void TIM2_IRQHandler(void) //TIM2中断
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查TIM2更新中断发生与否
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx更新中断标志
// NEC_IR_Remote_Periodic_Task();
KEY_Scan();
}
}
void KEY_Scan(void)
{
static unsigned int key0_cnt = 0;
if( RESET == KEY0_Read )
{
key0_cnt ++;
if( 380 == key0_cnt )
{
key0_flag =1;
}
if( key0_cnt > 38000 )
{
key0_cnt = 38000;
}
}
else
{
key0_cnt = 0;
}
}
int main(void)
{
KEY_GPIO_Init();
TIM2_Configuration();
GPIO_CONIG();
GPIO_SetBits(GPIOC,GPIO_Pin_7);
while(1)
{
// GPIO_SetBits(GPIOC,GPIO_Pin_7);
// Delay(838709);//500ms
// Delay(838709);
//else if(key!=1)
// GPIO_ResetBits(GPIOC,GPIO_Pin_7);
if( 1 == key0_flag )
{
GPIO_Toggle(GPIOC,GPIO_Pin_7); key0_flag=0;
}
}
}
复制代码
原理图: 无
仿真: 无
代码:
GPIO_IOToggle.7z
(19.72 KB, 下载次数: 1)
2023-9-24 21:48 上传
点击文件名下载附件
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1