主要实现通过LVGL中的仪表盘实时显示板卡中ADC采样的数据,
1.之前的分享中已经实现在ra6m5板卡中运行LVGL表盘,现在主要实现表盘的UI绘制代码如下,
static lv_obj_t* meter;
static void set_value(void* indic, int32_t v)
{
lv_meter_set_indicator_value(meter, indic, v); // 设置表头指示值
}
lv_meter_indicator_t* indic;
static void lv_example_meter_1(void)
{
meter = lv_meter_create(lv_scr_act()); // 创建meter对象
lv_obj_center(meter); // 居中显示
lv_obj_set_size(meter, 260, 260); // 设置大小
/Add a scale first/
lv_meter_scale_t* scale = lv_meter_add_scale(meter); // 增加表刻度
lv_meter_set_scale_ticks(meter, scale, 41, 2, 10, lv_palette_main(LV_PALETTE_GREY)); // 设置ticks
lv_meter_set_scale_major_ticks(meter, scale, 8, 4, 15, lv_color_black(), 10); // 设置major ticks
/*Add a blue arc to the start*/
indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_BLUE), 0); //添加蓝色表弧指示
lv_meter_set_indicator_start_value(meter, indic, 0); // 起始指示值0
lv_meter_set_indicator_end_value(meter, indic, 820); // 结束指示值20
/*Make the tick lines blue at the start of the scale*/
indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_BLUE),
false, 0);// 添加蓝色刻度线
lv_meter_set_indicator_start_value(meter, indic, 0); //刻度线起始值0
lv_meter_set_indicator_end_value(meter, indic, 820);//刻度线结束值20
/*Add a red arc to the end*/
indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_RED), 0);//添加红色表弧指示
lv_meter_set_indicator_start_value(meter, indic, 3280); // 起始指示值80
lv_meter_set_indicator_end_value(meter, indic, 4100); // 结束指示值100
/*Make the tick lines red at the end of the scale*/
indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_RED), lv_palette_main(LV_PALETTE_RED), false,
0);// 添加红色刻度线
lv_meter_set_indicator_start_value(meter, indic, 3280); //刻度线起始值80
lv_meter_set_indicator_end_value(meter, indic,4100);//刻度线结束值100
/*Add a needle line indicator*/
indic = lv_meter_add_needle_line(meter, scale, 4, lv_palette_main(LV_PALETTE_GREY), -10); // 添加灰色表针
}
2.接下来通过FSP配置板卡中ADC,板卡中采用P000管脚作为采样端口。
FSP中配置参数
设置好后保存ctrl+s,然后点击generate project content,生成代码,然后关闭FSP。
3.在keil工程中编写adc相关代码如下
void adc_Init(void)
{
fsp_err_t err;
err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
assert(FSP_SUCCESS == err);
}
volatile bool scan_complete_flag = false;
void adc_callback(adc_callback_args_t * p_args)
{
FSP_PARAMETER_NOT_USED(p_args);
scan_complete_flag = true;
}
double adc_read(void)
{
uint16_t adc_data;
double a0;
(void)R_ADC_ScanStart(&g_adc0_ctrl);
scan_complete_flag = false;
while (!scan_complete_flag)
{
;
}
R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_0, &adc_data);
a0 = adc_data;
return a0;
}
4.keil工程中hal_entry函数代码如下;
void hal_entry(void)
{
/* TODO: add your own code here */
int i=300;
lv_ui ui;
SysTick_Init (1000); //初始化系统时钟
LED_Init(); // LED ³õʼ»¯
LCD_Init();
adc_Init();
R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
lv_init(); // lvgl?????
lv_port_disp_init(); // lvgl???????,??lv_init()???
// setup_ui(&ui);
lv_example_meter_1();
while (1)
{
lv_meter_set_indicator_value(meter, indic,(int)adc_read()); // 设置表头指示值
// printf("a0=%f\r\n",adc_read());
R_BSP_SoftwareDelay(5000,BSP_DELAY_UNITS_MILLISECONDS);
}
}
5.编译下载,重启后实现如视频中功能。
更多回帖