在SD卡下面建了一个my_data的TXT文件,文件内容是hello!但是始终无法读出来!
下面是代码,希望大神们帮忙看看,哪里出现问题了!
diskio.c
#include "diskio.h" /* FatFs lower layer API */
#include "sdio_sdcard.h" /* Example: Header file of existing MMC/SDC contorl module */
/* Definitions of physical drive number for each drive */
#define ATA 0 /* Example: Map ATA harddisk to physical drive 0 */
#define MMC 1 /* Example: Map MMC/SD card to physical drive 1 */
#define USB 2 /* Example: Map USB MSD to physical drive 2 */
/*-----------------------------------------------------------------------*/
/* Get Drive Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
return 0;
}
/*-----------------------------------------------------------------------*/
/* Inidialize a Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
u8 state;
state=SD_Init();
if(state==0)
return RES_OK;
else
return 1;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */
)
{
u8 res;
if (count == 1)
{
res = SD_ReadBlock(buff, sector ,512);
}
else
{
res = SD_ReadMultiBlocks(buff, sector ,512, count);
}
if (res == SD_OK)
{
return RES_OK;
}
return RES_ERROR;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if _USE_WRITE
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */
)
{
u8 res;
if (count == 1)
{
res = SD_WriteBlock((uint8_t *)buff, sector ,512);
}
else
{
res = SD_WriteMultiBlocks((uint8_t *)buff, sector ,512, count);
}
if (res == SD_OK)
{
return RES_OK;
}
else
return RES_ERROR;
}
#endif
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
#if _USE_IOCTL
DRESULT disk_ioctl (
BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
return RES_OK;
}
#endif
DWORD get_fattime (void)
{
DWORD date=0;
return date;
}
main.c
#include "pbdata.h"
#include "ff.h"
#include "diskio.h"
#include "integer.h"
#include "string.h"
void RCC_Configuration(void);
void GPIO_COM_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
SD_Error Status=SD_OK;
extern SD_CardInfo SDCardInfo;
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
u8 state;
FATFS fs; //逻辑驱动器的工作区(文件系统对象)
FRESULT res;//FATFS函数公共结果代码
FILINFO fileInfo;
UINT br; //文件读/写字节计数
u16 i;
DIR dir;
FIL file;//文件对象
char data[512];
char buffer[512];
int main(void)
{
RCC_Configuration(); //系统时钟初始化
GPIO_COM_Configuration();//端口初始化
USART_Configuration();
NVIC_Configuration();
Status=SD_Init();
if(Status==SD_OK)
{
printf("SD卡初始化成功!\r\n");
}
else
{
printf("SD卡初始化失败!\r\n");
}
printf("制造商ID号: %d\r\n",SDCardInfo.SD_cid.ManufacturerID);
printf("卡的类型: %d\r\n",SDCardInfo.CardType);
printf("卡的容量: %d\r\n",SDCardInfo.CardCapacity);
printf("块的大小: %d\r\n",SDCardInfo.CardBlockSize);
if(disk_initialize(0)==0)
printf("SD卡准备就绪!\r\n");
//为逻辑驱动器注册工作区
res = f_mount(&fs,0,1);
res = f_open(&file, "my_data.txt", FA_OPEN_EXISTING | FA_READ);
if(res!=FR_OK)
{
while(1);
}
for (;;)
{
res = f_read(&file, buffer, sizeof(buffer), &br);
if (res || br == 0) break; // error or eof
for( i = 0; i < br; ++i )
printf("%c",buffer);
printf("\n");
}
f_close(&file);
f_mount(&fs,0,0);
printf("操作完成!\r\n");
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);
delay_ms(1000);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
delay_ms(1000);
}
}
大神们,帮帮忙吧!谢谢1
|