单片机 :STM32F407
开发板:DMF407电机开发板
平台:keil V5.31
HSE 为8MHZ
HSI为16MHZ
一、内存申请:
exfuns_init(); /* 为fatfs相关变量申请内存 */
二、初始化
USBH_Init(&g_hUSBHost, USBH_UserProcess, HOST_FS);
三、注册
USBH_RegisterClass(&g_hUSBHost, USBH_MSC_CLASS);
四、启动
USBH_Start(&g_hUSBHost);
五、处理
delay_ms(1800);
btim_timx_int_init(1000, 840); /* 84 000 000 / 840 = 100 000 100Khz的计数频率,计数1000次为10ms */
while(usb_mount_status==0) /* 等待 USB 设备挂载完成 */
{
lcd_show_string(30, 30, 200, 16, 16, "STM32", RED);
lcd_show_string(30, 50, 200, 16, 16, "USB Connecting...", RED);
delay_ms(200);
delay_ms(200);
}
while (fonts_init()) /* 检查字库 */
{
UPD:
printf("update font\r\n");
usb_mount_status = 0;
lcd_clear(WHITE); /* 清屏 */
USBH_Init(&g_hUSBHost, USBH_UserProcess, HOST_FS);
USBH_RegisterClass(&g_hUSBHost, USBH_MSC_CLASS);
USBH_Start(&g_hUSBHost);
delay_ms(1800);
btim_timx_int_init(1000, 840); /* 84 000 000 / 840 = 100 000 100Khz的计数频率,计数1000次为10ms */
while(usb_mount_status==0) /* 等待 USB 设备挂载完成 */
{
lcd_show_string(30, 30, 200, 16, 16, "STM32", RED);
lcd_show_string(30, 50, 200, 16, 16, "USB Connecting...", RED);
delay_ms(200);
delay_ms(200);
}
lcd_clear(WHITE); /* 清屏 */
lcd_show_string(30, 30, 200, 16, 16, "STM32", RED);
lcd_show_string(30, 50, 200, 16, 16, "Font Updating...", RED);
key = fonts_update_font(20, 90, 16, (uint8_t *)"1:", RED); /* 更新字库 */
while (key) /* 更新失败 */
{
lcd_show_string(30, 90, 200, 16, 16, "Font Update Failed!", RED);
delay_ms(200);
lcd_fill(20, 90, 200 + 20, 90 + 16, WHITE);
delay_ms(200);
}
TIM3->CR1&=~(1<<0); /* 关闭定时器3 */
lcd_show_string(30, 90, 200, 16, 16, "Font Update Success! ", RED);
delay_ms(1500);
lcd_clear(WHITE); /* 清屏 */
}
TIM3->CR1&=~(1<<0); /* 关闭定时器3 */
lcd_clear(WHITE); /* 清屏 */
text_show_string(30, 30, 200, 16, "正点原子STM32开发板", 16, 0, RED);
text_show_string(30, 50, 200, 16, "GBK字库测试程序", 16, 0, RED);
text_show_string(30, 70, 200, 16, "正点原子@ALIENTEK", 16, 0, RED);
text_show_string(30, 90, 200, 16, "按KEY0,更新字库", 16, 0, RED);
text_show_string(30, 110, 200, 16, "内码高字节:", 16, 0, BLUE);
text_show_string(30, 130, 200, 16, "内码低字节:", 16, 0, BLUE);
text_show_string(30, 150, 200, 16, "汉字计数器:", 16, 0, BLUE);
text_show_string(30, 180, 200, 24, "对应汉字为:", 24, 0, BLUE);
text_show_string(30, 204, 200, 16, "对应汉字(16*16)为:", 16, 0, BLUE);
text_show_string(30, 220, 200, 16, "对应汉字(12*12)为:", 12, 0, BLUE);
while (1)
{
for (i = 0x81; i < 0xff; i++) /* GBK内码高字节范围为0x81~0xFE */
{
fontx[0] = i;
lcd_show_num(118, 110, i, 3, 16, BLUE); /* 显示内码高字节 */
for (j = 0x40; j < 0xfe; j++) /* GBK内码低字节范围为 0x40~0x7E, 0x80~0xFE) */
{
if (j == 0x7f)
continue;
fontcnt++;
lcd_show_num(118, 130, j, 3, 16, BLUE); /* 显示内码低字节 */
lcd_show_num(118, 150, fontcnt, 5, 16, BLUE); /* 汉字计数显示 */
fontx[1] = j;
text_show_font(30 + 132, 180, fontx, 24, 0, BLUE);
text_show_font(30 + 144, 204, fontx, 16, 0, BLUE);
text_show_font(30 + 108, 220, fontx, 12, 0, BLUE);
t = 200;
while (t--) /* 延时,同时扫描按键 */
{
delay_ms(1);
key = key_scan(0);
if (key == KEY0_PRES)
{
goto UPD; /* 跳转到UPD位置(强制更新字库) */
}
}
LED0_TOGGLE();
}
}
}
测试结果:
1、不插U盘:


2、插一个不能识别的U盘


3、可以识别的U盘,并加载字库文件


