在上一个实验中调试了SPI FLASH的读写。也是为了这个实验准备,那就是将汉字字库写入到FLASH中。我所用的汉子字库是2M左右的,板子上没有SD卡卡座,想了想就决定用1K Xmodem协议来传输文件。
字库文件:16_24_32HZK.rar
关于1K Xmodem协议简单的介绍一下,将文件数据分为多个包来传输,其中每个包都有1K的数据。当然加上包头以及后面的校验字节,每个数据包有1028个字节。GD32的存储容量够所以直接选用了1K Xmodem而没有选用128 Xmodem协议。
1K Xmodem协议传输与128 Xmodem协议传输相比较所需要的时间短,如果所用控制器的内存容量小那么可以考虑使用128 Xmodem协议。有兴趣了解的童鞋可以在网上查查更加详细的介绍。
下面是主要的接收代码:
if(gFontFlag != X_REC_END) { while(XmodemInfoStruct.Xmodem_Status != X_REC_END ) { if(XmodemInfoStruct.Xmodem_Status == X_REC_INVALID) { USART_SendData (USART1,XMODEM_NAK); //¿ªÊ¼ XmodemInfoStruct.Xmodem_Status = X_REC_READY; } if(XmodemInfoStruct.Xmodem_Time >= XMODE_TIMEOUT) { USART_SendData (USART1,XMODEM_NAK); //¿ªÊ¼ XmodemInfoStruct.Xmodem_Status = X_REC_READY; XmodemInfoStruct.Xmodem_Time = 0; } if(XmodemInfoStruct.Xmodem_Status == X_REC_RECOK) { if((XmodemInfoStruct.Xmodem_Count & 0x00ff) != gUart1RecBuff[1]) { if((XmodemInfoStruct.Xmodem_Count & 0x00ff) > gUart1RecBuff[1]) { XmodemInfoStruct.Xmodem_Count += 0x100; } XmodemInfoStruct.Xmodem_Count &= 0xff00; XmodemInfoStruct.Xmodem_Count |= gUart1RecBuff[1]; GUI_DispDecAt(XmodemInfoStruct.Xmodem_Count,100,100,5); SPI_Flash_Write(&gUart1RecBuff[3],gFontFlashAddr,1024); gFontFlashAddr += 1024; USART_SendData (USART1,XMODEM_ACK); XmodemInfoStruct.Xmodem_Status = X_REC_READY; } } } if(XmodemInfoStruct.Xmodem_Status == X_REC_END ) { USART_SendData (USART1,XMODEM_ACK); gFontFlag = X_REC_END; DelayMs(500); SPI_Flash_Write(&gFontFlag,FONT_FLASH_FLAG,1); } }
先判断是否已经成功写入过字库,如果没有写入字库,那么再开始Xmodem协议。
在液晶上显示接收的数据包个数:
总共接收2002个数据包,一共2002K的字库数据。主函数里的程序:
int main() { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); LedPinInit(); SysTickInit(); UartInit(); FlashInit(); SPI_Flash_Read(&gFontFlag,FONT_FLASH_FLAG,1); TimerInit(99,7199); AdcInit(); GUI_Init(); GUI_SetBkColor(GUI_RED); GUI_SetColor(GUI_WHITE); GUI_Clear(); GUI_DispStringAt("liklon--->GD32107C-EVAL",1,40); GUI_DispStringAt("www.eepw.com.cn",40,60); GUI_DispStringAt("Xmodem pack number:",10,80); GUI_DispStringAt("电子产品世界",48,160); // TouchInit(); GUI_SetPenSize(10); GUI_DispStringAt((const char *)gReadWriteBuff,38,200); USART_SendData(USART1,'a'); DelayMs(500); if(gFontFlag != X_REC_END) { while(XmodemInfoStruct.Xmodem_Status != X_REC_END ) { if(XmodemInfoStruct.Xmodem_Status == X_REC_INVALID) { USART_SendData (USART1,XMODEM_NAK); //¿ªÊ¼ XmodemInfoStruct.Xmodem_Status = X_REC_READY; } if(XmodemInfoStruct.Xmodem_Time >= XMODE_TIMEOUT) { USART_SendData (USART1,XMODEM_NAK); //¿ªÊ¼ XmodemInfoStruct.Xmodem_Status = X_REC_READY; XmodemInfoStruct.Xmodem_Time = 0; } if(XmodemInfoStruct.Xmodem_Status == X_REC_RECOK) { if((XmodemInfoStruct.Xmodem_Count & 0x00ff) != gUart1RecBuff[1]) { if((XmodemInfoStruct.Xmodem_Count & 0x00ff) > gUart1RecBuff[1]) { XmodemInfoStruct.Xmodem_Count += 0x100; } XmodemInfoStruct.Xmodem_Count &= 0xff00; XmodemInfoStruct.Xmodem_Count |= gUart1RecBuff[1]; GUI_DispDecAt(XmodemInfoStruct.Xmodem_Count,100,100,5); SPI_Flash_Write(&gUart1RecBuff[3],gFontFlashAddr,1024); gFontFlashAddr += 1024; USART_SendData (USART1,XMODEM_ACK); XmodemInfoStruct.Xmodem_Status = X_REC_READY; } } } if(XmodemInfoStruct.Xmodem_Status == X_REC_END ) { USART_SendData (USART1,XMODEM_ACK); gFontFlag = X_REC_END; DelayMs(500); SPI_Flash_Write(&gFontFlag,FONT_FLASH_FLAG,1); } }
效果图:
字库烧写进去后,在GUI的相关文件做修改,具体修改方法查看下面链接,是楼主以前的一篇关于GUI加入汉子字库的介绍性文章:http://www.eepw.com.cn/article/182405.htm
对代码有兴趣的童鞋去楼主的导航帖处下载