talon/User/memecoder.c

80 lines
2.3 KiB
C
Raw Normal View History

2024-01-13 12:57:51 -05:00
#include "memecoder.h"
#include "ch32v20x.h"
int memecoder_prev_state = 0;
int memecoder_count = 0;
void memecoder_init() {
2024-02-10 16:16:29 -05:00
// Enable GPIOA
2024-01-26 16:04:31 -05:00
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
2024-01-13 12:57:51 -05:00
2024-02-10 16:16:29 -05:00
// Set up GPIO on port A
2024-01-13 12:57:51 -05:00
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
2024-01-26 16:04:31 -05:00
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
2024-01-13 12:57:51 -05:00
2024-02-10 16:16:29 -05:00
// PHASE_A - PA0
2024-01-26 16:04:31 -05:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2024-01-13 12:57:51 -05:00
2024-02-10 16:16:29 -05:00
// PHASE_B - PA1
2024-01-26 16:04:31 -05:00
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2024-01-13 12:57:51 -05:00
2024-01-26 16:04:31 -05:00
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);
2024-01-13 12:57:51 -05:00
}
void memecoder_update() {
2024-01-26 16:04:31 -05:00
// 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;
// }
2024-01-13 12:57:51 -05:00
}
void memecoder_set(int value) {
memecoder_count = value;
}
int memecoder_get() {
2024-02-10 16:16:29 -05:00
return (int16_t)TIM_GetCounter(TIM2) / 4;
2024-01-13 12:57:51 -05:00
}
void memecoder_zero() {
memecoder_count = 0;
}