The porting of LVGL9.1 was completed last time, and this time the motor PWM modulation based on the LVGL library was implemented.
This experiment uses the LVGL slider control, which is relatively simple to implement. The simulation effect based on the official routine is shown in the figure below.
The key codes are as follows
static lv_obj_t * label;
static void slider_event_cb(lv_event_t * e)
{
lv_obj_t * slider = lv_event_get_target(e);
/*Refresh the text*/
lv_label_set_text_fmt(label, "%"LV_PRId32, lv_slider_get_value(slider));
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
}
/**
* Create a slider and write its value on a label.
*/
void lv_example_get_started_4(void)
{
/*Create a slider in the center of the display*/
lv_obj_t * slider = lv_slider_create(lv_screen_active());
lv_obj_set_width(slider, 200); /*Set the width*/
lv_obj_center(slider); /*Align to the center of the parent (screen)*/
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL); /*Assign an event function*/
/*Create a label above the slider*/
label = lv_label_create(lv_screen_active());
lv_label_set_text(label, "0");
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
}
The motor controlled this time is a coreless brush motor
The control circuit diagram of the motor is
The speed of the motor is driven by PWM modulation. The PWM drive control is the same as the LCD backlight control. Since there is no MOS tube on hand and the drive circuit cannot be built, the LCD backlight brightness is used here to represent the motor speed.
Implement adjustable PWM in slider_event_cb function
static void slider_event_cb(lv_event_t * e)
{
lv_obj_t * slider = lv_event_get_target(e);
uint8_t pwm = lv_slider_get_value(slider);
/*Refresh the text*/
lv_label_set_text_fmt(label, "%"LV_PRId32, lv_slider_get_value(slider));
lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
/* 避免背光不显示*/
if(pwm<20)
pwm=20;
lcd_set_brightness(pwm);
}
The effect is as shown below. Slide to adjust the screen brightness or motor speed.