LCD的配置
根据比赛提供的赛场资源数据包(G431)中的LCD_Test例程打开CubeMX生成的代码可直接使用
main.c中对LCD使用的程序编写
#include "main.h"
#include "lcd.h"
/*定义lcd处理函数中使用的全局变量*/
__IO uint32_t uwTick_LCD_State_Pointer;
unsigned char Lcd_Disp_String[21];
void LCD_Proc(void);
LCD_Init(); //lcd初始化
LCD_Clear(White); //清屏为白色
LCD_SetBackColor(White); //设置背景颜色
LCD_SetTextColor(Magenta); //设置字体颜色
int main(void)
{
HAL_Init();
SystemClock_Config();
while (1)
{
LCD_Proc(); //lcd处理函数
}
}
void LCD_Proc(void) //lcd处理函数
{
static unsigned char count=0; //定义静态变量
if(uwTick-uwTick_LCD_State_Pointer<1000) return; //扫描时间设置为1s
uwTick_LCD_State_Pointer=uwTick;
count++;
memset(Lcd_Disp_String,0,sizeof(Lcd_Disp_String)); //将字符串清空
sprintf((char*)Lcd_Disp_String, " count num: %03d ",count); //sprintf格式化字符串的处理将count的值赋给数组Lcd_Disp_String
LCD_DisplayStringLine(Line0, Lcd_Disp_String);
}
使用LCD的注意事项
1、在STM32G431单片机屏幕上只能显示出字符串数组中的20位,故字符串数组大小定义为21位(加上末尾‘\0’)
2、引用stdio.h头文件使用 字符串格式化函数 int sprintf(char str, const char format, …)