库里似乎没有,找代理FAE也没提过,自己跟着寄存器搓了下
void Encoder_Init() {
stc_gpio_cfg_t stcGpioCfg = {0};
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //端口外设时钟使能
stcGpioCfg.enDir = GpioDirIn;
stcGpioCfg.enDrv = GpioDrvL;
stcGpioCfg.enPu = GpioPuEnable;
stcGpioCfg.enPd = GpioPdDisable;
stcGpioCfg.enOD = GpioOdDisable;
stcGpioCfg.enCtrlMode = GpioAHB;
Gpio_Init(ENCODER_GPIO_Port, ENCODER_A_PHASE_Pin, &stcGpioCfg);
Gpio_SetAfMode(ENCODER_GPIO_Port, ENCODER_A_PHASE_Pin, GpioAf4);
Gpio_Init(ENCODER_GPIO_Port, ENCODER_B_PHASE_Pin, &stcGpioCfg);
Gpio_SetAfMode(ENCODER_GPIO_Port, ENCODER_B_PHASE_Pin, GpioAf6);
stc_tim3_mode23_cfg_t stcTim3BaseCfg = {0};
Sysctrl_SetPeripheralGate(SysctrlPeripheralTim3, TRUE); //Timer3外设时钟使能
stcTim3BaseCfg.enWorkMode = Tim3WorkMode2; //锯齿波模式
stcTim3BaseCfg.enCT = Tim3Timer; //定时器功能,计数时钟为内部PCLK
stcTim3BaseCfg.enPRS = Tim3PCLKDiv2; //PCLK/2
stcTim3BaseCfg.enCntDir = Tim3CntUp; //向上计数,在三角波模式时只读
Tim3_Mode23_Init(&stcTim3BaseCfg); //TIM3 的模式23功能初始化
// see RM_HC32L13x系列参考手册_Rev2.35 14.2.7 正交编码计数功能
M0P_TIM3_MODE23->MSCR_f.SMS = 6;
M0P_TIM3_MODE1->FLTR_f.FLTA0 = 0b100;
M0P_TIM3_MODE1->FLTR_f.FLTB0 = 0b100;
M0P_TIM3_MODE23->CRCH0_f.CFA_CRA_BKSA = 0b11;
M0P_TIM3_MODE23->CRCH0_f.CFB_CRB_BKSB = 0b11;
Tim3_M23_Cnt16Set(encoder_central_count);
Tim3_M23_Run();
encoder_check = true;
}
uint16_t Encoder_GetCnt() {
return Tim3_M23_Cnt16Get();
}
void Encoder_SetCnt(uint16_t cnt) {
Tim3_M23_Cnt16Set(cnt);
}
|