【iCore4 雙核心板_ARM】例程十四:FATFS實驗——檔案操作

XiaomaGee發表於2017-08-30

實驗現象:

核心程式碼:

int main(void)
{    
    int i,j;
    FIL file;
    FATFS fatfs;
    static FRESULT res;
    unsigned char write_buffer[512];                                      
    unsigned char read_buffer[512];                                       
    unsigned int counter;
    
    system_clock.initialize();                                                                                        
    led.initialize();                                                                                                
    usart6.initialize(115200);                                                                                  
    usart6.printf("\x0c");                                                             
    usart6.printf("\033[1;32;40m");                                                 
    usart6.printf("\r\nHello, I am iCore4!\r\n\r\n");                                     
    if(BSP_SD_Init() != 0){
        usart6.printf("SD ERROR!\r\n");
        while(1){
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
            HAL_Delay(500);
        }
    }else{
        usart6.printf("SD Initialize successful!\r\n");
    }

    HAL_Delay(500);
    res = f_mount(&fatfs,"0:",1);
    if(res != RES_OK){
        usart6.printf("f_mount error!\r\n");
        while(1){
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
            HAL_Delay(500);            
        }
    }else{
        usart6.printf("f_mount successful!\r\n");
    }

    res = f_open(&file,"0:/SD¿¨²âÊÔ.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS);//´ò¿ªÇý¶¯Æ÷0ÉϵÄÔ´Îļþ
    if(res != RES_OK){
        usart6.printf("f_open error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }        
    }else{
        usart6.printf("f_open successful!\r\n");
    }
    
    res = f_lseek(&file,0);                                               
    if(res != RES_OK){
        usart6.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }                
    }else{
        usart6.printf("f_lseek successful!\r\n");
    }
    
    for(i = 0;i < 512;i++){
        write_buffer[i] = i % 256;
    }    

    res = f_write(&file,write_buffer,512,&counter);                       //½«»º³åÆ÷ÖеÄÄÚÈÝдÈëÔ´Îļþ
    if(res != RES_OK || counter != 512){
        usart6.printf("f_write error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }    
    }else{
        usart6.printf("f_write successful!\r\n");    
    }    

    res = f_lseek(&file,0);                                                  
    if(res != RES_OK){
        usart6.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }                
    }else{
        usart6.printf("f_lseek successful!\r\n");
    }

    res = f_read(&file,read_buffer,512,&counter);
    if(res != RES_OK || counter != 512){
        usart6.printf("f_read error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }    
    }else{
        usart6.printf("f_read successful!\r\n");
    }
    f_close(&file);    
  
    usart6.printf("read data:\r\n");
    for(i = 0;i < 32;i++){
        for(j = 0; j < 16; j ++)
            usart6.printf(" %02X",read_buffer[i*16+j]);
        usart6.printf("\r\n");
    }    
    
    while(1){
        LED_GREEN_ON;
        HAL_Delay(500);
        LED_GREEN_OFF;
        HAL_Delay(500);            
    }    
}

原始碼下載連結:

連結:https://pan.baidu.com/s/1jJDh134 密碼:3pyh

iCore4連結:

相關文章