hc32l13x 多个编码器开关,带按键功能、防抖功能、检测上升沿,正转计数增加,反转计数减少,主函数、中断函数、初始化函数分开
时间: 2024-06-08 07:07:36 浏览: 155
下面是一个示例程序,实现了多个编码器开关,带按键功能、防抖功能、检测上升沿,正转计数增加,反转计数减少的功能,主函数、中断函数、初始化函数分开。
```c
#include "hc32l13x.h"
#define ENC_A1_PORT GpioPortA
#define ENC_A1_PIN GpioPin4
#define ENC_B1_PORT GpioPortA
#define ENC_B1_PIN GpioPin3
#define ENC_A2_PORT GpioPortB
#define ENC_A2_PIN GpioPin5
#define ENC_B2_PORT GpioPortB
#define ENC_B2_PIN GpioPin4
#define KEY_PORT GpioPortC
#define KEY_PIN GpioPin0
#define COUNT_MAX 9999
#define COUNT_MIN -9999
volatile int16_t enc1_count = 0;
volatile int16_t enc2_count = 0;
void Gpio_Init(void)
{
stc_gpio_config_t stcGpioCfg;
/* Encoder1 A */
Gpio_StructInit(&stcGpioCfg);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvHiz;
Gpio_Init(ENC_A1_PORT, ENC_A1_PIN, &stcGpioCfg);
/* Encoder1 B */
Gpio_StructInit(&stcGpioCfg);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvHiz;
Gpio_Init(ENC_B1_PORT, ENC_B1_PIN, &stcGpioCfg);
/* Encoder2 A */
Gpio_StructInit(&stcGpioCfg);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvHiz;
Gpio_Init(ENC_A2_PORT, ENC_A2_PIN, &stcGpioCfg);
/* Encoder2 B */
Gpio_StructInit(&stcGpioCfg);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvHiz;
Gpio_Init(ENC_B2_PORT, ENC_B2_PIN, &stcGpioCfg);
/* Key */
Gpio_StructInit(&stcGpioCfg);
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvHiz;
Gpio_Init(KEY_PORT, KEY_PIN, &stcGpioCfg);
}
void Timer4_Init(void)
{
stc_tim4_config_t stcConfig;
DDL_ZERO_STRUCT(stcConfig);
stcConfig.enClkDiv = Tim4PclkDiv1;
stcConfig.enMode = Tim4WorkModeSawtooth;
stcConfig.enOverflowAction = Tim4OverflowCount;
stcConfig.u16PeriodVal = 0xFFFF;
stcConfig.enOutputPolarity = Tim4PWMInvalid;
stcConfig.enExtTrig = Tim4ExtTiggerDisable;
stcConfig.enOutputMask = Tim4OutputMaskDisable;
stcConfig.enStopInIdle = Tim4StopInIdleDisable;
Tim4_Config(&stcConfig);
Tim4_ClearCountFlag();
Tim4_EnableIrq();
EnableNvic(TIM4_IRQn, IrqLevel3, TRUE);
Tim4_Start();
}
void Encoder1_Process(void)
{
static uint8_t enc_a_last = 0;
uint8_t enc_a_cur = Gpio_GetInputIO(ENC_A1_PORT, ENC_A1_PIN);
uint8_t enc_b_cur = Gpio_GetInputIO(ENC_B1_PORT, ENC_B1_PIN);
uint8_t enc_dir = 0;
if (enc_a_cur != enc_a_last)
{
if (enc_b_cur == enc_a_cur)
{
enc_dir = 1;
}
else
{
enc_dir = -1;
}
if (enc_dir > 0)
{
if (enc1_count < COUNT_MAX)
{
enc1_count++;
}
}
else if (enc_dir < 0)
{
if (enc1_count > COUNT_MIN)
{
enc1_count--;
}
}
}
enc_a_last = enc_a_cur;
}
void Encoder2_Process(void)
{
static uint8_t enc_a_last = 0;
uint8_t enc_a_cur = Gpio_GetInputIO(ENC_A2_PORT, ENC_A2_PIN);
uint8_t enc_b_cur = Gpio_GetInputIO(ENC_B2_PORT, ENC_B2_PIN);
uint8_t enc_dir = 0;
if (enc_a_cur != enc_a_last)
{
if (enc_b_cur == enc_a_cur)
{
enc_dir = 1;
}
else
{
enc_dir = -1;
}
if (enc_dir > 0)
{
if (enc2_count < COUNT_MAX)
{
enc2_count++;
}
}
else if (enc_dir < 0)
{
if (enc2_count > COUNT_MIN)
{
enc2_count--;
}
}
}
enc_a_last = enc_a_cur;
}
void Key_Process(void)
{
static uint8_t key_last = 0;
uint8_t key_cur = Gpio_GetInputIO(KEY_PORT, KEY_PIN);
if (key_cur != key_last)
{
if (key_cur == 0)
{
/* Key pressed */
// Do something...
}
}
key_last = key_cur;
}
void TIM4_IRQHandler(void)
{
Encoder1_Process();
Encoder2_Process();
Key_Process();
Tim4_ClearCountFlag();
}
int main(void)
{
/* Initialize GPIO */
Gpio_Init();
/* Initialize Timer4 */
Timer4_Init();
while (1)
{
// Do something...
}
return 0;
}
```
以上是一个示例程序,仅供参考,具体实现可以根据实际需求进行修改。
阅读全文