欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
hk32f030m單片機(jī)定時(shí)器消抖,解決按鍵按下讀取電平抖動(dòng)
[打印本頁(yè)]
作者:
sr861126
時(shí)間:
2023-9-21 11:31
標(biāo)題:
hk32f030m單片機(jī)定時(shí)器消抖,解決按鍵按下讀取電平抖動(dòng)
用定時(shí)器按一定時(shí)間間隔掃描按鍵
#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; //時(shí)間加載(SysTick->LOAD為24bit)
SysTick->VAL =0x00; //清空計(jì)數(shù)器
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //開(kāi)始倒數(shù)
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16))); //等待時(shí)間到達(dá)
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //關(guān)閉計(jì)數(shù)器
SysTick->VAL =0X00; //清空計(jì)數(shù)器
}
}
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);
//中斷優(yōu)先級(jí)NVIC設(shè)置
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);
}
//定時(shí)器3中斷服務(wù)程序
void TIM2_IRQHandler(void) //TIM2中斷
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //檢查T(mén)IM2更新中斷發(fā)生與否
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx更新中斷標(biāo)志
// 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;
}
}
}
復(fù)制代碼
原理圖: 無(wú)
仿真: 無(wú)
代碼:
GPIO_IOToggle.7z
(19.72 KB, 下載次數(shù): 1)
2023-9-24 21:48 上傳
點(diǎn)擊文件名下載附件
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1