- extern int8_t read_datp[32];
- int main(void)
- {
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- system_clock_config();
- at32_board_init();
- /* usb gpio config */
- usb_gpio_config();
- #ifdef USB_LOW_POWER_WAKUP
- usb_low_power_wakeup_config();
- #endif
- uart_print_init(115200);
- /* enable otgfs clock */
- crm_periph_clock_enable(OTG_CLOCK, TRUE);
- /* select usb 48m clcok source */
- usb_clock48m_select(USB_CLK_HEXT);
- /* enable otgfs irq */
- nvic_irq_enable(OTG_IRQ, 0, 0);
- /* init usb */
- usbh_init(&otg_core_struct,
- USB_FULL_SPEED_CORE_ID,
- USB_ID,
- &uhost_class_handler,
- &usbh_user_handle);
- app_oled_init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"AT32F425",16);
- OLED_ShowString(0,2,"U disk TEST",16);
- delay_ms(200);
- while(1)
- {
- usbh_loop_handler(&otg_core_struct.host);
- OLED_ShowString(24,4,read_datp,16);
- }
- }
其中,公关变量read_datp用于获取OLED屏的显示信息。
在实现了U盘信息显示之后,该如何实现数据的U盘记录呢?
方法是将记录的数据写到数值中,然后再一次性写入。
所涉及的函数为usbh_user_application(),其内容为:
- extern int8_t read_datp[32];
- static usb_sts_type usbh_user_application(void)
- {
- usb_sts_type status = USB_OK;
- FRESULT res;
- uint32_t len;
- uint8_t write_data[32] =
- {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x41,0x42,0x61,0x62};
- res = f_mount(&fs, "", 0);
- if(res == FR_OK)
- {
- if(f_open(&file, "0:AT32.txt", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
- {
- USBH_DEBUG("Open AT32.txt failed");
- }
- else
- {
- res = f_write(&file, write_data, sizeof(write_data), &len);
- if(res != FR_OK || len == 0)
- {
- USBH_DEBUG("Write AT32.txt failed");
- }
- else
- {
- USBH_DEBUG("Write AT32.txt Success");
- }
- f_close(&file);
- }
- if(f_open(&file, "0:AT32.txt", FA_READ) != FR_OK)
- {
- USBH_DEBUG("Open AT32.txt failed");
- }
- else
- {
- res = f_read(&file, read_datp, sizeof(read_datp), &len);
- if(res != FR_OK || len == 0)
- {
- USBH_DEBUG("Read AT32.txt failed");
- }
- else
- {
- USBH_DEBUG("Read AT32.txt Success");
- }
- f_close(&file);
- }
- f_mount(NULL, "", 0);
- }
- return status;
- }
其运行效果如图5所示
图5 数值存储与显示
若使用数组write_data来存放汉字字模,则可以获得软字库的作用。
以16*16点阵汉字为例,一个汉字要占用32字节。
存放“鸿”字的字模数组为:
uint8_t write_data[32] =
{0x12,0x64,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,0x00,0x04,0x7E,0x01,0x10,0x30,0x1F,0x08,0x08,0x00,0x13,0x12,0x12,0x52,0x82,0x7E,0x00};
相应的汉字显示函数为:
- void OLED_ShowCHineseR(uint8_t x,uint8_t y,uint8_t no)
- {
- uint8_t t,adder=0;
- OLED_Set_Pos(x,y);
- for(t=0;t<16;t++)
- {
- OLED_WR_Byte(read_datp[t],OLED_DATA);
- adder+=1;
- }
- OLED_Set_Pos(x,y+1);
- for(t=0;t<16;t++)
- {
- OLED_WR_Byte(read_datp[16+t],OLED_DATA);
- adder+=1;
- }
- }
实现图6所示测试效果的主程序为:
- int main(void)
- {
- nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
- system_clock_config();
- at32_board_init();
- usb_gpio_config();
- #ifdef USB_LOW_POWER_WAKUP
- usb_low_power_wakeup_config();
- #endif
- uart_print_init(115200);
- crm_periph_clock_enable(OTG_CLOCK, TRUE);
- usb_clock48m_select(USB_CLK_HEXT);
- nvic_irq_enable(OTG_IRQ, 0, 0);
- usbh_init(&otg_core_struct,
- USB_FULL_SPEED_CORE_ID,
- USB_ID,
- &uhost_class_handler,
- &usbh_user_handle);
- app_oled_init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"AT32F425",16);
- OLED_ShowString(0,2,"U disk TEST",16);
- OLED_ShowCHinese(16,4,0);
- OLED_ShowCHinese(32,4,1);
- delay_ms(200);
- while(1)
- {
- usbh_loop_handler(&otg_core_struct.host);
- OLED_ShowCHineseR(64,4,0);
- }
- }
在显示界面中,“鸿雁”2字是通过读取字库来实现的,而单个“鸿”字则是通过读取U盘来实现的。
图6 汉字显示