Encoder class
encoder-mbed
Hardware encoder library for stm32f4.
Import
In your mbed application root folder run:
$ mbed add https://github.com/byq77/encoder-mbed.git
Supported targets
- TARGET_CORE2
- ENCODER_1:
TIM2
(PA_0
andPA_1
) - ENCODER_2:
TIM8
(PC_6
andPC_7
) - ENCODER_3:
TIM3
(PB_4
andPA_7
) - ENCODER_4:
TIM4
(PB_6
andPB_7
)
- ENCODER_1:
- TARGET_PANTHER
- ENCODER_1:
TIM4
(PB_6
andPB_7
) - ENCODER_2:
TIM1
(PE_9
andPE_11
) - ENCODER_3:
TIM3
(PC_6
andPC_7
) - ENCODER_4:
TIM2
(PA_15
andPB_3
)
- ENCODER_1:
- TARGET_NUCLEO_F401RE
TIM1
(PA_8
andPA_9
)TIM2
(PA_0
andPA_1
)TIM3
(PB_4
andPB_5
)TIM4
(PB_6
andPB_7
)
- TARGET_RCP_F407
- ENCODER1:
TIM2
(PA_0
andPA_1
) - ENCODER2:
TIM4
(PD_12
andPD_13
)
- ENCODER1:
You can easily add support for other STM32 targets or change assigned pins by editing following files target/target_board.h
and EncoderMspInitF4.cpp
.
Example code
main.cpp
#include <mbed.h>
#include <Thread.h>
#include <mbed_events.h>
#include <Encoder.h>
DigitalOut led(LED1);
InterruptIn button(BUTTON1,PullDown);
Thread thread;
Encoder encoder(TIM2);
Mutex encoder_mutex;
static void encoder_test()
{
encoder.init();
while(1){
encoder_mutex.lock();
encoder.print_debug_info();
encoder_mutex.unlock();
ThisThread::sleep_for(100);
}
}
static void buttonCallback()
{
ThisThread::sleep_for(50);
if(!button.read())
return;
encoder_mutex.lock();
encoder.resetCount();
printf("RESET!\r\n");
encoder_mutex.unlock();
}
int main()
{
EventQueue * q = mbed_event_queue();
thread.start(encoder_test);
button.rise(q->event(buttonCallback));
while(1)
{
led = !led;
ThisThread::sleep_for(1000);
}
}
app_mbed.json
{
"config": {
},
"macros" : [
],
"target_overrides" : {
"NUCLEO_F401RE": {
"events.shared-dispatch-from-application": 0,
"platform.default-serial-baud-rate": 115200,
"platform.stdio-baud-rate": 115200,
"mbed-encoder.debug-logs": 1
}
}
}