Encoder memes

This commit is contained in:
Thomas Muller 2024-01-26 16:04:31 -05:00
parent 848b3dddb5
commit 1c4ed62d84
2 changed files with 47 additions and 27 deletions

View file

@ -11,6 +11,8 @@
void init_switch_gpio() {
// Enable GPIOB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Set up switch on PB5 and PB6, use internal pull ups
GPIO_InitTypeDef GPIO_InitStructure;
@ -86,6 +88,8 @@ int main(void)
// mode_position2(&app, &position2_state);
}
printf("%d\r\n", memecoder_get());
// Read data from CAN
if(can_recv(&rx_msg)) {
u32 arbid = rx_msg.ExtId;

View file

@ -8,46 +8,62 @@ int memecoder_count = 0;
void memecoder_init() {
// Enable GPIOB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Set up GPIO on port B
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
// PHASE_A - PB12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// PHASE_A - PB13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// PHASE_A - PB14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB, &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;
// 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) {
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--;
}
// 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;
}
// memecoder_prev_state = cur_state;
// }
}
void memecoder_set(int value) {
@ -55,7 +71,7 @@ void memecoder_set(int value) {
}
int memecoder_get() {
return memecoder_count;
return TIM_GetCounter(TIM2);
}
void memecoder_zero() {