⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于msp430单片机的SD卡读写驱动
💻 C
字号:




//#include <msp430x16x.h>
#include    <msp430xG46x.h>
#include "hal_hardware_board.h"
#include "mmc.h"
#include "diskio.h"
#include "tff.h"	
#include <stdio.h>


#define OPERATIONOK  0
#define OPENFAIL     1
#define READFAIL     2
#define WRITEFAIL    3


unsigned long cardSize = 0;
unsigned char status = 1;
unsigned int  timeout = 0;
int i = 0;

 

int main( void )
{
  
   
    FATFS fs;            // Work area (file system object) for logical drive
    FIL fsrc, fdst;      // file objects
    BYTE buffer[500];   // file copy buffer
    BYTE buffer1[500]={0};   // file copy buffer
    FRESULT res;         // FatFs function common result code
    UINT  bw;         // File R/W count
    UINT  br;         // File R/W count
    INT8U fs_status=0;
    INT16U i;  
    
   
   
    CPU_init();
    
    for(i=0;i<400;i++)
     {
        buffer[i]= i%256;
     }
  //Initialisation of the MMC/SD-card
  while (status != 0)                       // if return in not NULL an error did occur and the
                                            // MMC/SD-card will be initialized again 
  {
    status = mmcInit();
    timeout++;
    if (timeout == 150)                      // Try 50 times till error
    {
      //printf ("No MMC/SD-card found!! %x\n", status);
      break;
    }
  }

  // Read the Card Size from the CSD Register
  cardSize =  mmcReadCardSize();

    // Register a work area for logical drive 0
    f_mount(0, &fs);

    // Open source file
    res = f_open(&fsrc, "srcfile.txt", FA_OPEN_EXISTING | FA_READ|FA_WRITE);
    if (res) fs_status = OPENFAIL ;
    else    fs_status =OPERATIONOK;

    // Create destination file
    res = f_open(&fdst, "dstfile.txt", FA_OPEN_EXISTING | FA_WRITE|FA_READ);
    if (res) fs_status = OPENFAIL ;
    else    fs_status =OPERATIONOK;

    // Copy source to destination
   // for (;;) {
        res = f_write(&fsrc, buffer, 400, &bw);
        if (res || bw < 400) fs_status = READFAIL ;//break;   // error or disk full
        else    fs_status =OPERATIONOK;
        
        res = f_read(&fsrc, buffer1, 400, &br);
        if (res || br == 0) fs_status = READFAIL ;//break;   // error or eof
        else    fs_status =OPERATIONOK;
        
        res = f_write(&fdst, buffer1, 400, &bw);
        if (res || bw < 400) fs_status = WRITEFAIL ;//break;   // error or disk full
     
    f_close(&fsrc);
    f_close(&fdst);

    // Unregister a work area before discard it
    f_mount(0, NULL);
    mmcGoIdle();                              // set MMC in Idle mode

  while (1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -