#include "memecoder.h" #include "ch32v20x.h" int memecoder_prev_state = 0; int memecoder_count = 0; void memecoder_init() { // Enable GPIOA RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // Set up GPIO on port A GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // PHASE_A - PA0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_Init(GPIOA, &GPIO_InitStructure); // PHASE_B - PA1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_DeInit(TIM2); TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); TIM_ICInitTypeDef TIM_ICInitTypeDefStructure; TIM_ICStructInit(&TIM_ICInitTypeDefStructure); TIM_ICInit(TIM2, &TIM_ICInitTypeDefStructure); TIM_ICInitTypeDefStructure.TIM_Channel = TIM_Channel_2; TIM_ICInit(TIM2, &TIM_ICInitTypeDefStructure); TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Falling, TIM_ICPolarity_Falling); TIM_Cmd(TIM2, ENABLE); } void memecoder_update() { // u8 cur_state = // GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) << 0 | // GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) << 1;// | // GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) << 2; // if(cur_state != memecoder_prev_state) { // printf("%d\r\n", cur_state); // if( (memecoder_prev_state == 0 && cur_state == 1) || // (memecoder_prev_state == 1 && cur_state == 3) || // (memecoder_prev_state == 3 && cur_state == 2) || // (memecoder_prev_state == 2 && cur_state == 0)) { // if( (memecoder_prev_state == 1 && cur_state == 3) || // (memecoder_prev_state == 3 && cur_state == 2) || // (memecoder_prev_state == 2 && cur_state == 6) || // (memecoder_prev_state == 6 && cur_state == 4) || // (memecoder_prev_state == 4 && cur_state == 5) || // (memecoder_prev_state == 5 && cur_state == 1)) { // memecoder_count++; // } else { // memecoder_count--; // } // memecoder_prev_state = cur_state; // } } void memecoder_set(int value) { memecoder_count = value; } int memecoder_get() { return (int16_t)TIM_GetCounter(TIM2) / 4; } void memecoder_zero() { memecoder_count = 0; }