【GMT43智慧液晶模組】例程十三:FATFS實驗——檔案操作

XiaomaGee發表於2018-01-31

實驗原理:

  STM32F429上帶有SDIO控制器,GMT43液晶模組上將SDIO連線到TF卡座。本實驗

將Micro SD卡插入TF卡座上即可。通過FATFS建立test.txt檔案,並且寫入資料0-255,然後

讀出並顯示在液晶屏上。

示例截圖:

原始碼下載連結:

連結:https://pan.baidu.com/s/1raoiNl6 密碼:5w39

GMT43購買連結:

核心程式碼:

 

 

int main(void)
{
    FRESULT res;

    rcc.initialize();
    ads7843.initialize();
    SDRAM_Init();
    SDRAM_GPIOConfig();
    FMC_SDRAMWriteProtectionConfig(FMC_Bank2_SDRAM,DISABLE);
    lcd_tft.initialize();
    systick.initialize();

    GUI_Init();
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();

    pwm.initialize(80);
    GUI_Delay(200);
    LCD_ON;

    if(SD_Init() != 0){
        GUI_SetColor(GUI_RED);
        GUI_SetFont(&GUI_FontFixedsys16);
        GUI_DispStringHCenterAt("SD Error!",480/2,272/2);
        while(1);
    }

    res = f_mount(&fatfs,"0:",1);
    if(res != RES_OK){
        GUI_SetColor(GUI_RED);
        GUI_SetFont(&GUI_FontFixedsys16);
        GUI_DispStringHCenterAt("f_mount Error!",480/2,272/2);        
        while(1);
    }    
    power_on.check_touch();

    frame.process();
}
static void frame_process(void)
{
    FIL file;                                                                //檔案物件
    int i,j;
    unsigned int counter;

    GUI_CURSOR_Show();
    WM_SetCreateFlags(WM_CF_MEMDEV);
    GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);

    sprintf(buffer,"Is to write and read\r\n");
    MULTIEDIT_AddText(hItem,buffer);
    memset(buffer,0,sizeof(buffer));
    GUI_Exec();

    f_open(&file,"0:/test.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
    f_lseek(&file,0); 
    for(i = 0;i < 512;i++){
        write_buffer[i] = i % 256;
    }
    f_write(&file,write_buffer,512,&counter);                          //將緩衝器中的內容寫入原始檔    
    f_lseek(&file,0);                                                     //移動讀指標到檔案首
    f_read(&file,read_buffer,512,&counter);
    f_close(&file);  

    MULTIEDIT_SetText(hItem,buffer);                                   //上次顯示的文字清空

    sprintf(buffer,"Complete\r\n");
    MULTIEDIT_AddText(hItem,buffer);
    memset(buffer,0,sizeof(buffer));

    sprintf(buffer,"write data:\r\n");
    MULTIEDIT_AddText(hItem,buffer);
    memset(buffer,0,sizeof(buffer));

    for(i = 0;i < 64;i++){
        for(j=0;j < 8;j++){
            sprintf(buffer," %02X",write_buffer[i*8+j]);    
            MULTIEDIT_AddText(hItem,buffer);
            memset(buffer,0,sizeof(buffer));
        }
        sprintf(buffer,"\r\n");
        MULTIEDIT_AddText(hItem,buffer);
        memset(buffer,0,sizeof(buffer));
    }

    sprintf(buffer,"read data:\r\n");
    MULTIEDIT_AddText(hItem,buffer);
    memset(buffer,0,sizeof(buffer));

    for(i = 0;i < 64;i++){
        for(j=0;j < 8;j++){
            sprintf(buffer," %02X",read_buffer[i*8+j]);    
            MULTIEDIT_AddText(hItem,buffer);
            memset(buffer,0,sizeof(buffer));
        }
        sprintf(buffer,"\r\n");
        MULTIEDIT_AddText(hItem,buffer);
        memset(buffer,0,sizeof(buffer));
    }

    while(1){
        GUI_Exec();
    }
}

 

相關文章