Show motor speed on LED display 2

This commit is contained in:
jaseg 2023-06-06 14:24:09 +02:00
parent e86efa6e6a
commit 1c893002a9
3 changed files with 23 additions and 2 deletions

View file

@ -7,4 +7,10 @@
void motor_init(void);
void motor_set_speed(int speed_rpm);
struct motor_state {
int speed_rpm;
};
extern struct motor_state st_motor;
#endif /* __MOTOR_H__ */

View file

@ -2,7 +2,12 @@
#include <motor.h>
struct motor_state st_motor;
void motor_init() {
memset(&st_motor, 0, sizeof(st_motor));
/* PB1 (BT1) -> Open drain, TIM2 CH3N for speed PWM*/
GPIOB->MODER &= ~GPIO_MODER_MODER0_Msk;
GPIOB->MODER |= AF(1);
@ -30,9 +35,18 @@ void motor_init() {
}
void motor_set_speed(int speed_rpm) {
if (speed_rpm > 3000) {
speed_rpm = 3000;
} else if (speed_rpm < -3000) {
speed_rpm = -3000;
}
st_motor.speed_rpm = speed_rpm;
int speed = speed_rpm * 65535 / 3000;
if (speed == 0) {
GPIOB->BRR = 1<<12;
} else {
GPIOB->BSRR = 1<<12;
if (speed < 0) {

View file

@ -1,6 +1,7 @@
#include <proto.h>
#include <adc.h>
#include <motor.h>
#include <lcd.h>
#include <led7seg.h>
@ -291,12 +292,12 @@ ErrorCode tx_update_display() {
},
.led = {
sys_time_us/10000,
0,
st_motor.speed_rpm,
0
},
.led_fmt = {
PROTO_LED_FMT_DEC | PROTO_LED_FMT_DP_2,
PROTO_LED_FMT_OFF,
PROTO_LED_FMT_DEC,
PROTO_LED_FMT_OFF,
},
};