Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 27, 2024, 06:53:07 18:53


Login with username, password and session length


Pages: [1]
Print
Author Topic: READ SD MMC <64M SOURCE mikroC (fix bug )  (Read 4771 times)
0 Members and 1 Guest are viewing this topic.
sinmax
Guest
« on: November 21, 2007, 01:55:49 13:55 »

use mikroC_for_PIC_6210

main.c


//
/*
所有命令 以 0x0A  即\n为结束符
建立一个新文件11112222.TXT
CRF|11112222TXT# @ ^

删除文件11112222.TXT
DELF|11112222TXT# @ ^

效验文件是否存在
CRCF|11112222TXT# @ ^

读文件11112222TXT读第5行
READLN|11112222TXT#5 @ ^

在文件11112222TXT第3行后插入111111111111111111111111111116

INSTLN|11112222TXT#3@111111111111111111111111111116^
RENF|SWAP0001TMP#11112222TXT@311133333333^   <必须执行>

删除文件11112222TXT 的第3行
DELLN|11112222TXT#3@ ^
RENF|SWAP0001TMP#11112222TXT@311133333333^   <必须执行>

在文件11112222TXT尾部添加一条记录
APPLN|11112222TXT#30@111111111111111111111111111116^

将所有文件名列出
GETDIR

文件重命名  将SWAP0001TMP改为11112222TXT
RENF|SWAP0001TMP#11112222TXT@311133333333^

*/
//CREATE_FILE|<filename>0x0A
//DEL_FILE|<filename>0x0A
//CRC_FILE|<filename>0x0A
//READ_FILE|<filename>|<address>|<size>0x0A
//RWRITE_FILE|<filename>|<address>|<size>|<data>0x0A      RWRITE_FILE|1.txt|200|aabbccdd0x0A
//APPEND_FIL|<filename>|<data>0x0A
//SET_FILE_DATE|<filename>|<datetime>0x0A
//GET_FILE_DATE|<filename>0x0A
//GET_FILE_SIZE|<filename>0x0A
//GET_DISK_ALL_FILE0x0A
//GET_DISK_LABEL0x0A
//unsigned short inputs[60] = "";
unsigned char inputStr[60] = "";
unsigned long i,size  ;
unsigned char inputCtr = 0;
unsigned short caracter;
unsigned short SDdata[512];
unsigned char filename[14] = "00000001TXT"; // File    names
unsigned char file_contents[30] = "";

unsigned int lnum  ,cnt=0,usa=0,usb=0;
unsigned Reserved_Sectors, SectorsPerFAT, Offset, RootOffset;
unsigned long RootDir;
unsigned char command1[14], filesize1[12];
unsigned short NumberOfFAT;

char  NeedOutCOM=0;

void COM_Write(char *s)
{
  while (*s)
  {
    USART_Write(*s++);
  }
}
void I_Write_Str(char *ostr)
{
  unsigned short i;
  i = 0;
  while (ostr)
  {
    USART_Write(ostr[i++]);
  }
  USART_Write('\r');
  USART_Write('\n');
} // ~
//RWRITE_FILE|1.txt|200|aabbccdd0x0A   brkstr(RWRITE_FILE|1.txt|200|aabbccdd0x0A)

//int change30(char *str)
//{
//   char *ptr,i;
//   i=0;
//   ptr=str;
//   while(*ptr!=0)
//   {
//      str_result=*ptr;
//      i++;
//      ptr++;
//   }
//   for(i=i;i<30;i++)
//   {
//      str_result=' ';
//   }
//}


int brkstr()
{
  unsigned i = 0, j = 0,  l = 0, slen = 0;
  asm CLRWDT;
 
  for (i = 0; i < 14; i++)
     command1=0x00;
  for (i = 0; i < 12; i++)
     filesize1=0x00;
  for (i = 0; i < 14; i++)
     filename=0x20;
     filename[11]=0x00;
  for (i = 0; i < 30; i++)
     file_contents=0x20;
  file_contents[29]=0x00;
  file_contents[30]=0x00;


  slen = strlen(inputStr);
  strcpy(command1,"");

  for (i = 0; i < slen; i++)
  {
    if (inputStr == '|')
    {

        for (j = 0; j < i; j++)
          command1[j] = inputStr[j];
        l=i+1;
        break;
    }
  }
  strcpy(filename,"");

  for (i = l; i < slen; i++)
  {
    if (inputStr == '#')
    {
        for (j = l; j < i; j++)
          filename[j-l] = toupper(inputStr[j]);     //test
        l=i+1;
        break;
    }
  }
 

 
  strcpy(filesize1,"");

  for (i = l; i < slen; i++)
  {
    if (inputStr == '@')
    {
        for (j = l; j < i; j++)
          filesize1[j-l] = inputStr[j];
          l=i+1;
        break;
    }
  }
  strcpy(file_contents,"");

  for (i = l; i < slen; i++)
  {
    if (inputStr == '^')
    {
        for (j = l; j < i; j++)
          file_contents[j-l] = inputStr[j];
          l=i+1;
        break;
    }
  }


//QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
//  i=0;
//   ptr=file_contents;
//   while(*ptr!=0)
//   {
//      temp_file_contents=*ptr;
//      i++;
//      ptr++;
//   }
//   for(i=i;i<30;i++)
//   {
//      temp_file_contents=' ';
//   }
//
//
//  strcpy(file_contents,temp_file_contents);


//     I_Write_Str(command1);

//     I_Write_Str(filename);

//     I_Write_Str(filesize1);

//     I_Write_Str(file_contents);
      return 0;
}


int SD_Read_FAT_Data()
{
  int icount;
  for (icount = 0; icount <= 512; icount++)
  {
    SDdata[icount] = 0x00;
  }
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    MMC_Read_Sector(0, SDdata);
  }
  else
  {}
    return 0;
}

//M-M-M--------- Creates new file and writes some data to it   ok

int M_Create_New_File()
{
  int icount, j, k,l;
  char txt[16];
  asm CLRWDT;
  SD_Read_FAT_Data();
  Reserved_Sectors = SDdata[0x0E] *256+SDdata[0x0F];
  //  wordtostr(Reserved_Sectors,txt);
  //  COM_Write (txt);
  SectorsPerFAT = SDdata[0x16] *256+SDdata[0x17];
  //  wordtostr(SectorsPerFAT,txt);
  //  COM_Write (txt);
  NumberOfFAT = SDdata[0x10];
  //  wordtostr(NumberOfFAT,txt);
  //  COM_Write (txt);
  RootDir = (unsigned long)SectorsPerFAT *(unsigned long)NumberOfFAT;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = (Reserved_Sectors + RootDir);
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = RootDir / 256;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  //   longtostr(RootDir, txt);
  //   COM_Write(txt);
  for (icount = 0; icount <= 512; icount++)
  {
    SDdata[icount] = 0x00;
  }
  //send = ALL FILENAME
  k = 0;
  //    RootDIR 一共有24个区
  for (l = RootDir; l <= (RootDir + 24); l++)
  {
    if (!Mmc_Fat_Init(&PORTC, 2))
    {
      MMC_Read_Sector(l, SDdata);
    }
    asm CLRWDT;

    for (icount = 0; icount <= 512; icount++)
    {
      k = icount % 32;
      if (k == 0)
      if ((SDdata[icount] == 0XE5) || (SDdata[icount] == 0X00))
      {


        SDdata[icount+0]=filename[0];
        SDdata[icount+1]=filename[1];
        SDdata[icount+2]=filename[2];
        SDdata[icount+3]=filename[3];
        SDdata[icount+4]=filename[4];
        SDdata[icount+5]=filename[5];
        SDdata[icount+6]=filename[6];
        SDdata[icount+7]=filename[7];
        SDdata[icount+8]=filename[8];
        SDdata[icount+9]=filename[9];
        SDdata[icount+10]=filename[10];
        SDdata[icount+11]=0x20;
        Mmc_Write_Sector(l, SDdata);

        goto  writeok;

      }
    }
  }
 
  writeok:

   //判断文件是否存在
  if (!Mmc_Fat_Init(&PORTC, 2))
  if (Mmc_Fat_Assign(filename, 0))
  {
    return 1;
  }
  else
  {
    return  0;
  }
  else
  {
    return 0;
  }


}




int M_Create_New_File2()
{
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    Mmc_Fat_Assign(&filename, 1);
      // Will  not   find     file  and  then   create  file
    return 1;
  }
  else
  {
    return 0;
  };
} // ~

//M-M-M--------- Opens an existing file and appends data to it
//               (and alters the date/time stamp)
int M_Open_File_Append()
{
    int size1;
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    asm CLRWDT;
    Mmc_Fat_Assign(&filename, 0);
    Mmc_Fat_Set_File_Date(2007, 6, 7, 12, 35, 0);
    Mmc_Fat_Append(); // Prepare

    size1=atoi(filesize1);
    file_contents[size1]=0x0D;
    file_contents[size1+1]=0x00;

    Mmc_Fat_Write(file_contents, size1+1); // Write
   

    //I_Write_Str(file_contents);
    return 1;
  }
  else
  {
    return 0;
  }
} // ~
//M-M-M--------- Deletes a file. If file doesn't exist, it will first be created
//               and then deleted.
int M_Delete_File()
{
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    Mmc_Fat_Assign(filename, 0);
    Mmc_Fat_Delete();
    return 1;
  }
  else
  {
    return 0;
  }
} // ~



//读出第?行的内容
int M_Open_File_Readln()
{
  unsigned int   t,j;
  //input filesize1 这时候 代表行号
  unsigned long  flag1;
  flag1=0;

  for (i = 0; i < 31; i++)
     file_contents=0x20;
     
  t=0;
  if (!Mmc_Fat_Init(&PORTC, 2))
  {

    Mmc_Fat_Assign(&filename, 0);
    size=  Mmc_Fat_Get_File_Size();
    //size1=atoi(filesize1);
    /*
    Mmc_Fat_Reset(&size);
    for (i = 1; i <= size; i++)
    {
    asm CLRWDT;
      Mmc_Fat_Read(&caracter);
      //Usart_Write(caracter);
      if (caracter==0x0D)
        {
        t++;
          if (t==lnum)
          {
          flag1=i;
          break;
          }
        }
    }
   
    */
    flag1=31*lnum;
     
    j=0;                   //每行定长        30
    Mmc_Fat_Reset(&size);
    for (i = 1; i <= size; i++)
    {
    asm CLRWDT;
        Mmc_Fat_Read(&caracter);
      if ((i>=(flag1-30)) && (i<=flag1))
        {
        if (caracter==0x0D)
        caracter=0x20;
        if (caracter==0x00)
        caracter=0x20;
        if (caracter==0x0A)
        caracter=0x20;

        file_contents[j]=caracter;
        j++;
       // Usart_Write(file_contents[j]);
        }
    }
    if (NeedOutCOM ==1)
        I_Write_Str(file_contents);
    return 1;
  }
  else
  {
    return 0;
  }
} // ~




//插一条数据
int M_Open_File_Insert()
{

  unsigned int   a=0,b=0,c=0;
  //unsigned long  f=0,g=0;
  unsigned char tempfile[14] = "SWAP0001TMP"; // File    names
  unsigned char tempfile2[14]= "           "; // File    names
  unsigned char temp_file_contents[30] = "";
    //////////建立临时文件  将 f点前的数据写入这个临时文件
    a=atoi(filesize1) ; //a传入的  要插入的行序号
    strcpy(temp_file_contents,file_contents); //保存临时inst的字符串
//    I_Write_Str(filesize1);
   
    strcpy(tempfile2,filename);
    strcpy(filename,tempfile);
    M_Create_New_File(); //建立 SWAP0001.TMP
//    I_Write_Str(filename);

    for (b=1;b<=a;b++)
    {
    asm CLRWDT;
    strcpy(filename,tempfile2);  //恢复文件名
//    I_Write_Str(filename);
   
    lnum =b;                     //要读的行数
    M_Open_File_Readln()  ;      //读文件的第lnum行到file_contents
   
    strcpy(filename,tempfile);   //搞临时文件
//    I_Write_Str(filename);
   
    filesize1[0]=0x33;
    filesize1[1]=0x30;
    filesize1[2]=0x00;
//    I_Write_Str (filesize1);
//    I_Write_Str(file_contents);
    M_Open_File_Append();        //把记录添加到临时文件

    }
    strcpy(file_contents,temp_file_contents);
    strcpy(filename,tempfile);   //搞临时文件
    //I_Write_Str(file_contents);
    filesize1[0]=0x33;
    filesize1[1]=0x30;
    filesize1[2]=0x00;
    M_Open_File_Append();        //把要插入的新字符串添加到临时文件//////////append 那个临时文件


    ///////////////////////确定一共有多少行  多少条记录
    /*
    strcpy(filename,tempfile2);  //恢复文件名 ok
    Mmc_Fat_Assign(&filename, 0);
    size=  Mmc_Fat_Get_File_Size();
    Mmc_Fat_Reset(&size);
    for (i = 1; i <= size; i++)
    {
      Mmc_Fat_Read(&caracter);
        if (caracter==0x0D)
        {
        c++;
        }
    }
    */
    strcpy(filename,tempfile2);  //恢复文件名
    Mmc_Fat_Assign(&filename, 0);
    c=  Mmc_Fat_Get_File_Size();
    c = ( c / 31);  //算有多少行

   
    //////////将f点后的数据写入临时文件
   
   
    for (b=(a+1);b<=(c+0);b++)     //ok
    {
    asm CLRWDT;
    strcpy(filename,tempfile2);  //恢复文件名
//    I_Write_Str(filename);

    lnum =b;                     //要读的行数
    M_Open_File_Readln()  ;      //读文件的第lnum行到file_contents

    strcpy(filename,tempfile);   //搞临时文件
//    I_Write_Str(filename);

    filesize1[0]=0x33;
    filesize1[1]=0x30;
    filesize1[2]=0x00;
   // I_Write_Str ("<");
   // I_Write_Str(file_contents);
   // I_Write_Str (">");
    M_Open_File_Append();        //把记录添加到临时文件
    }
    asm CLRWDT;
    /////////删除原文件
    strcpy(filename,tempfile2);  //恢复文件名
    M_Delete_File();
    //复制临时文件为 原文件名
   
    //删除临时文件


       return 1;

}


//M-M-M--------- Opens an existing file, reads data from it and puts it to USART
int M_Open_File_Read()
{

  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    Mmc_Fat_Assign(&filename, 0);
    size=  Mmc_Fat_Get_File_Size();
    //size1=atoi(filesize1);

    Mmc_Fat_Reset(&size);
    for (i = 1; i <= size; i++)
    {
      Mmc_Fat_Read(&caracter);
      Usart_Write(caracter);
     }
    return 1;
  }
  else
  {
    return 0;
  }
} // ~
//M-M-M--------- Opens an existing file and rewrites it
int M_Open_File_Rewrite()
{
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    Mmc_Fat_Assign(&filename, 0);
    Mmc_Fat_Rewrite();
    Mmc_Fat_Write(file_contents, 30); // write

    return 1;
  }
  else
  {
    return 0;
  }
} // ~
//M-M-M--------- Tests whether file exists, and if so sends its creation date
//               and file size via USART
int M_GET_FILE_SIZE()
{
  unsigned long fsize;
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    if (Mmc_Fat_Assign(filename, 0))
    {
      // ---
      // get
      // file
      // size   unsigned long fsize;
      fsize = Mmc_Fat_Get_File_Size();
      //LongToStr((signed long)fsize, filesize1);
      //I_Write_Str(filesize1);
      return 1;
    }
    else
      return 0;
  }
  else
    return 0;
} // ~
//M-M-M--------- Tests whether file exists, and if so sends its creation date
//               and file size via USART
int M_Test_File_Exist()
{
  if (!Mmc_Fat_Init(&PORTC, 2))
  if (Mmc_Fat_Assign(filename, 0))
  {
    return 1;
  }
  else
  {
    return  - 1;
  }
  else
  {
    return 0;
  }
} // ~
/*
//M-M-M---------  appends data to it
//               (and alters the date/time stamp)
int M_SET_FILE_DATE()
{
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    Mmc_Fat_Assign(&filename, 0);
    Mmc_Fat_Set_File_Date(2007, 7, 7, 7, 7, 7);
    return 1;
  }
  else
  {
    return 0;
  }
} // ~
//M-M-M---------Get_File_Date
int M_Get_File_Date()
{
  unsigned long fsize;
  unsigned int year;
  unsigned short month, day, hour, minute;
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    if (Mmc_Fat_Assign(filename, 0))
    {
      // ---
      // file
      // has
      // been
      // found
      // -
      // get
      // its
      // date
      Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
      WordToStr(year, filesize1);
      I_Write_Str(filesize1);
      ByteToStr(month, filesize1);
      I_Write_Str(filesize1);
      WordToStr(day, filesize1);
      I_Write_Str(filesize1);
      WordToStr(hour, filesize1);
      I_Write_Str(filesize1);
      WordToStr(minute, filesize1);
      I_Write_Str(filesize1);
    }
  }
  else
  {
    return 0;
  }
} // ~
*/



//M-M-M---------  appends data to it
//               (and alters the date/time stamp)
void M_GET_DISK_LABEL_DISK()
{
  int icount;
  //   char            txt[16];
  SD_Read_FAT_Data();
  Reserved_Sectors = SDdata[0x0E] *256+SDdata[0x0F];
  //  wordtostr(Reserved_Sectors,txt);
  //  COM_Write (txt);
  SectorsPerFAT = SDdata[0x16] *256+SDdata[0x17];
  //  wordtostr(SectorsPerFAT,txt);
  //  COM_Write (txt);
  NumberOfFAT = SDdata[0x10];
  //  wordtostr(NumberOfFAT,txt);
  //  COM_Write (txt);
  RootDir = (unsigned long)SectorsPerFAT *(unsigned long)NumberOfFAT;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = (Reserved_Sectors + RootDir);
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = RootDir / 256;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  //   longtostr(RootDir, txt);
  //   COM_Write(txt);
  asm CLRWDT;
  for (icount = 0; icount <= 512; icount++)
  {
      SDdata[icount] = 0x00;
  }
  if (!Mmc_Fat_Init(&PORTC, 2))
  {
    MMC_Read_Sector(RootDir, SDdata);
  }
  else
  {}
  //                 0x00 0x0A
  //send disk lable
  for (icount = 0; icount <= 0x0A; icount++)
  {
    USART_Write(SDdata[icount]);
  }

  // COM_Write
  // (SDdata);
} // ~


//M-M-M---------  appends data to it
//               (and alters the date/time stamp)
int M_GET_DISK_ALL_FILE()
{
  int icount, j, k,l,m,n=1,w=0;
  char txt[16];
  unsigned long fsize;

  SD_Read_FAT_Data();
  asm CLRWDT;
  //fat16

  m=atoi(filename);
  if (m==0)            //返回DISK大小
  {
//void LongToStr(long number, char *output);
  fsize=SDdata[0x22];
  fsize=fsize<<8;
  fsize=fsize+SDdata[0x21];
  fsize=fsize<<8;
  fsize=fsize+SDdata[0x20];
  fsize=fsize*512;


  LongToStr((unsigned long )fsize,txt);
  I_Write_Str(txt);
  return 1;
  }

  Reserved_Sectors = SDdata[0x0E] *256+SDdata[0x0F];
  //  wordtostr(Reserved_Sectors,txt);
  //  COM_Write (txt);
  SectorsPerFAT = SDdata[0x16] *256+SDdata[0x17];
  //  wordtostr(SectorsPerFAT,txt);
  //  COM_Write (txt);
  NumberOfFAT = SDdata[0x10];
  //  wordtostr(NumberOfFAT,txt);
  //  COM_Write (txt);
  RootDir = (unsigned long)SectorsPerFAT *(unsigned long)NumberOfFAT;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = (Reserved_Sectors + RootDir);
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = RootDir / 256;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  //   longtostr(RootDir, txt);
  //   COM_Write(txt);
  for (icount = 0; icount <= 512; icount++)
  {
    SDdata[icount] = 0x00;
  }
  //send = ALL FILENAME
  k = 0;
  //    RootDIR 一共有24个区

  file_contents[0]=0x00;

  for (l = RootDir; l <= (RootDir + 24); l++)
  {
    if (!Mmc_Fat_Init(&PORTC, 2))
    {
      MMC_Read_Sector(l, SDdata);
    }
    asm CLRWDT;
    for (icount = 0; icount <= 512; icount++)
    {
      w=1;
      k = icount % 32;
      if ((k == 0) && (SDdata[icount] != 0XE5) && (SDdata[icount] != 0X00))
      {
      if  ((l==RootDir) && (icount==0)) continue; //不输出  DISK LABLE

        for (j = 0; j <= 7; j++)
        {
          txt[j] = (SDdata[icount + j]);
        }
        txt[8] = 0x00; //是否带分割
        n++;


        if  ((n>((m-2)*4+1)) && (n <=(m-1)*4+1))
           strcat(file_contents,txt);

        //m 传入的参数    m=2     m= (2 -1 )*4  =4       m=3  = 8

        if (m!=1)
          if (((m-1)*4+1)  ==n)  //返回第几个文件名称
           {
           file_contents[32]=0x00;
           COM_Write(file_contents);
           file_contents[0]=0x00;
           w=0;
           break;                  //提高读写速度
           }
      }
    }
  }
        if (m==1)
            {
            if ((n-1)==0)
            COM_Write(0xA5);
            else
            {
            intToStr((unsigned int )(n-1),txt);  //返回 文件有多少个
            COM_Write(txt);
            }
            }

        if (w==1)
          COM_Write(file_contents);

          COM_Write("\r\n");
} // ~



int M_File_COPY()    //删除第?行   将第?行删除
{

  int icount, j, k,l;
  char txt[16];
  asm CLRWDT;
  SD_Read_FAT_Data();
  Reserved_Sectors = SDdata[0x0E] *256+SDdata[0x0F];
  //  wordtostr(Reserved_Sectors,txt);
  //  COM_Write (txt);
  SectorsPerFAT = SDdata[0x16] *256+SDdata[0x17];
  //  wordtostr(SectorsPerFAT,txt);
  //  COM_Write (txt);
  NumberOfFAT = SDdata[0x10];
  //  wordtostr(NumberOfFAT,txt);
  //  COM_Write (txt);
  RootDir = (unsigned long)SectorsPerFAT *(unsigned long)NumberOfFAT;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = (Reserved_Sectors + RootDir);
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  RootDir = RootDir / 256;
  //  longtostr(RootDir,txt);
  //  COM_Write (txt);
  //   longtostr(RootDir, txt);
  //   COM_Write(txt);
  for (icount = 0; icount <= 512; icount++)
  {
    SDdata[icount] = 0x00;
  }
  //send = ALL FILENAME
  k = 0;
  //    RootDIR 一共有24个区
  for (l = RootDir; l <= (RootDir + 24); l++)
  {
    if (!Mmc_Fat_Init(&PORTC, 2))
    {
      MMC_Read_Sector(l, SDdata);
    }
    asm CLRWDT;
    for (icount = 0; icount <= 512; icount++)
    {
      k = icount % 32;
      if ((k == 0) && (SDdata[icount] != 0XE5) && (SDdata[icount] != 0X00))
      {
        for (j = 0; j <= 11; j++)
        {
          txt[j] = (SDdata[icount + j]);
        }
        txt[11] = 0x00;
        if (strcmp(filename,txt)==0)
        {

  SDdata[icount+0]=filesize1[0];
  SDdata[icount+1]=filesize1[1];
  SDdata[icount+2]=filesize1[2];
  SDdata[icount+3]=filesize1[3];
  SDdata[icount+4]=filesize1[4];
  SDdata[icount+5]=filesize1[5];
  SDdata[icount+6]=filesize1[6];
  SDdata[icount+7]=filesize1[7];
  SDdata[icount+8]=filesize1[8];
  SDdata[icount+9]=filesize1[9];
  SDdata[icount+10]=filesize1[10];
  Mmc_Write_Sector(l, SDdata);

        break;
        }

      }
    }
  }
  filesize1[11]=0x00;
  if (!Mmc_Fat_Init(&PORTC, 2))
  if (Mmc_Fat_Assign(filesize1, 0))
  {
    return 1;
  }
  else
  {
    return  0;
  }
  else
  {
    return 0;
  }


}



int M_File_Del_ln()    //删除第?行   将第?行删除
{
  unsigned int   a=0,b=0,c=0;
  signed long   fsize;
  unsigned char tempfile[14] = "SWAP0001TMP"; // File    names
  unsigned char tempfile2[14]= "           "; // File    names
  unsigned char temp_file_contents[30] = "";

    //////////建立临时文件  将 f点前的数据写入这个临时文件
    a=atoi(filesize1) ; //a传入的  要插入的行序号
//    strcpy(temp_file_contents,file_contents); //保存临时inst的字符串
//    I_Write_Str(filesize1);
    strcpy(tempfile2,filename);
    strcpy(filename,tempfile);
    M_Create_New_File(); //建立 SWAP0001.TMP
//    I_Write_Str(filename);
    if (a>1)
    for (b=1;b<=(a-1);b++)
    {
    asm CLRWDT;
    strcpy(filename,tempfile2);  //恢复文件名
//    I_Write_Str(filename);
    lnum =b;                     //要读的行数
    M_Open_File_Readln()  ;      //读文件的第lnum行到file_contents
    strcpy(filename,tempfile);   //搞临时文件
//    I_Write_Str(filename);
    filesize1[0]=0x33;
    filesize1[1]=0x30;
    filesize1[2]=0x00;
//    I_Write_Str (filesize1);
//    I_Write_Str(file_contents);
    M_Open_File_Append();        //把记录添加到临时文件
    }
    //I_Write_Str ("M1");
    strcpy(filename,tempfile2);  //恢复文件名
    Mmc_Fat_Assign(&filename, 0);
    fsize=  Mmc_Fat_Get_File_Size();
    fsize = ( fsize / 31);  //算有多少行
    //LongToStr((signed long)fsize, filesize1);
    //I_Write_Str (filesize1);
    //I_Write_Str (filename);
    for (b=(a+1);b<=fsize;b++)
    {
    asm CLRWDT;
    strcpy(filename,tempfile2);  //恢复文件名
    //I_Write_Str(filename);
    lnum =b;                     //要读的行数
    M_Open_File_Readln()  ;      //读文件的第lnum行到file_contents
    strcpy(filename,tempfile);   //搞临时文件
    //I_Write_Str(filename);
    filesize1[0]=0x33;
    filesize1[1]=0x30;
    filesize1[2]=0x00;
    //I_Write_Str (filesize1);
    //I_Write_Str(file_contents);
    M_Open_File_Append();        //把记录添加到临时文件
    }



    /////////删除原文件
    strcpy(filename,tempfile2);  //恢复文件名
    M_Delete_File();


    return 1;

}

void interrupt() {             // Interrupt is triggered by the watchdog
  cnt++ ;                      //   timer, since it doesn't get refreshed
  TMR0L  =  96;                //   on time
  INTCON = 0x20;
}//~


void main()
{
  char *text; // this  is  stored   in    RAM
  char wdtime=0;



  usa  = 0;
  usb  = 1;
  T0CON      = 0xC4;           // Enable watchdog timer
  TRISB      =   0;
  PORTB      = 0xFF;
  cnt        =   0;
  TMR0L      =  96;
  INTCON     = 0xA0;
 
  USART_init(57600); // initialize   USART     module
  // strConstCpy(text,  *TESTLOGO,92);
  I_Write_Str("0");

  Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
  while (1)
  {
    asm CLRWDT ;
    if (cnt >= 80)
    {
      if (PORTB.F3==0)
        PORTB.F3=1;
        else
        PORTB.F3=0;
      cnt   = 0;
    }

    PORTB.F4=1;


    if (USART_Data_ready())
    {
      unsigned char c;
      c = USART_Read();
      switch (c)
      {
        case '\n':
          {

          brkstr();
         
          if (strcmp(command1, "CRF") == 0)          //CRF|11112222TXT# @ ^
           //ok
          {
              PORTB.F4=0;
              if (M_Create_New_File2()==1)
              I_Write_Str("1");
              else
              I_Write_Str("0");
            // crfile();
          }
          else if (strcmp(command1, "DELF") == 0)     //DELF|11112222TXT# @ ^
          //ok
          {
            PORTB.F4=0;
            if (M_Test_File_Exist() == 1)
            {
            if (M_Delete_File() == 1)
              I_Write_Str("1");
            }
            else
              I_Write_Str("0");
            // deletefile();
          }
          else if (strcmp(command1, "CRCF") == 0)          //CRCF|11112222TXT# @ ^
          //ok
          {
            PORTB.F4=0;
            if (M_Test_File_Exist() == 1)
              I_Write_Str("1");
            else
              I_Write_Str("0");
            // checkfile();
          }
          else if (strcmp(command1, "READLN") == 0)         //READLN|11112222TXT#5 @ ^  读第5行  //READLN|11112222TXT#0 @ ^    读第1行
          {
            if (M_Test_File_Exist() == 1)
            {
            PORTB.F4=0;
            NeedOutCOM =1;
            lnum=atol(filesize1);
            if (M_Open_File_Readln() == 1)
             {
             NeedOutCOM =0;
             }
            }
            else
              I_Write_Str("0");
            // frase();
          }                //M_File_Del_ln
          else if (strcmp(command1, "INSTLN") == 0)         //INST|11112222TXT#3@111111111111111111111111111116^
          {
            if (M_Test_File_Exist() == 1)
            {
            PORTB.F4=0;
            NeedOutCOM =0;
            if (M_Open_File_Insert() == 1)
              I_Write_Str("1");
            }
            else
              I_Write_Str("0");
            // frase();
          }
         
          else if (strcmp(command1, "DELLN") == 0)         // DELLN|11112222TXT#3@111111111111111111111111111116^
          {
            if (M_Test_File_Exist() == 1)
            {
            PORTB.F4=0;
            NeedOutCOM =0;
            if (M_File_Del_ln() == 1)
              I_Write_Str("1");
            }
            else
              I_Write_Str("0");
            // frase();
          }
          else if (strcmp(command1, "APPLN") == 0)         //APPLN|11112222TXT#30@111111111111111111111111111116^
          {
            if (M_Test_File_Exist() == 1)
            {
            PORTB.F4=0;
            if (M_Open_File_Append() == 1)
              I_Write_Str("1");
            }
            else
              I_Write_Str("0");
            // frase();
          }
//          else if (strcmp(command1, "GETSIZE") == 0)      //GETSIZE|11112222TXT#8@111111123^
//          {
//            if (M_Test_File_Exist() == 1)
//            {
//            if (M_GET_FILE_SIZE() != 1)
////              COM_Write("9\r\n");
////            else
//              I_Write_Str("0");
//            }
//            // frase();
//          }

          else if (strcmp(command1, "RENF") == 0)         //RENF|SWAP0001TMP#11112222TXT@311133333333^
          {

            if (M_Test_File_Exist() == 1)
            {
            PORTB.F4=0;
            if (M_File_COPY()== 1)
              I_Write_Str("1");
            }
            else
              I_Write_Str("0");


          }

          else if (strcmp(command1, "GETDIR") == 0)           //GETDIR| # @ ^   //GETDIR 0 = DISKSIZE GETDIR 1= DISPLAY FILENAME1
          {
            PORTB.F4=0;
            M_GET_DISK_ALL_FILE();
            // COM_Write("A\r\n");
            // frase();
          }
          else
          {
           I_Write_Str (inputStr);
          }
          inputCtr = 0;
          *inputStr = 0;
          break;
          }
        case '\r':
          break;
        default:

          inputStr[inputCtr] = c;
          inputCtr++;
          inputStr[inputCtr] = 0;
      }
    }
  }
} // ~!


Logged
bxac
Newbie
*
 Muted
Offline Offline

Posts: 18

Thank You
-Given: 4
-Receive: 2


« Reply #1 on: November 21, 2007, 02:45:08 14:45 »

There is some code in avrlib to read MMC.

I'm trying to get the FAT16 to work for ATMega amd MMC. Then I will try with SD.
As soon as I get this to work I will post it here.
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC