实验现象:
核心代码:
int main(void)
{ /* USER CODE BEGIN 1 */
int i;
int j;
FIL file;
FATFS fatfs; //Âß¼Çý¶¯Æ÷µÄ¹¤×÷Çø
static FRESULT res; //FRESULTº¯Êý¹«¹²½á¹û´úÂë
unsigned char write_buffer[]; //дÎļþ»º³åÇø
unsigned char read_buffer[]; //¶ÁÎļþ»º³åÇø
unsigned int counter; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init(); /* Configure the system clock */
SystemClock_Config(); /* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
MX_USART1_UART_Init();
SystemClock_Config(); /* USER CODE BEGIN 2 */
usart1.printf("\x0c"); //ÇåÆÁ
usart1.printf("\033[1;32;40m"); //½«×ÖÌåÉèÖóÉÂÌÉ«
usart1.printf("\r\n\r\nhello! I am iCore1S!\r\n\r\n\r\n"); HAL_Delay();
res = f_mount(&fatfs,(TCHAR const*)SDPath,); //¹ÒÔØSD¿¨
if(res != RES_OK){
usart1.printf("f_mount error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}
else{
usart1.printf("f_mount successful!\r\n");
}
res = f_open(&file,"0:/test.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS); //´ò¿ªÇý¶¯Æ÷0ÉϵÄÔ´Îļþ
if(res != RES_OK){
usart1.printf("f_open error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}else{
usart1.printf("f_open successful!\r\n");
} res = f_lseek(&file,); //Òƶ¯Ð´Ö¸Õëµ½ÎļþÊ×
if(res != RES_OK){
usart1.printf("f_lseek error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}else{
usart1.printf("f_lseek successful!\r\n");
} for(i = ;i < ;i++){
write_buffer[i] = i % ;
}
res = f_write(&file,write_buffer,sizeof(write_buffer),&counter); //½«»º³åÆ÷ÖеÄÄÚÈÝдÈëÔ´Îļþ
if(res != RES_OK || counter != ){
usart1.printf("f_write error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}else{
usart1.printf("f_write successful!\r\n");
} res = f_lseek(&file,); //Òƶ¯¶ÁÖ¸Õëµ½ÎļþÊ×
if(res != RES_OK){
usart1.printf("f_lseek error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}else{
usart1.printf("f_lseek successful!\r\n");
} res = f_read(&file,read_buffer,sizeof(read_buffer),&counter);
if(res != RES_OK || counter != ){
usart1.printf("f_read error!\r\n");
while(){
HAL_Delay();
LED_RED_TOGGLE;
}
}else{
usart1.printf("f_read successful!\r\n");
}
f_close(&file); //¹Ø±ÕÔ´Îļþ usart1.printf("read data:\r\n");
for(i = ;i < ;i++){
for(j = ; j < ; j ++)
usart1.printf(" %02X",read_buffer[i*+j]);
usart1.printf("\r\n");
} /* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
while ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */
HAL_Delay();
LED_GREEN_TOGGLE;
}
/* USER CODE END 3 */ }
源代码下载链接:
链接:http://pan.baidu.com/s/1c2rkKuc 密码:k4e9