单片机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 557|回复: 2
收起左侧

PIC单片机 读/写 MMC/SD 卡的简单小程序

[复制链接]
oldspring 发表于 2018-10-26 09:11 | 显示全部楼层 |阅读模式
这里介绍一个简单的PIC单片机 读/写 MMC/SD 卡的小程序,希望大家能够喜欢。
  1. // MMC module connections
  2. sbit Mmc_Chip_Select           at LATC0_bit;  // for writing to output pin always use latch (PIC18 family)
  3. sbit Mmc_Chip_Select_Direction at TRISC0_bit;
  4. // eof MMC module connections

  5. const LINE_LEN = 43;
  6. char err_txt[20]       = "FAT16 not found";
  7. char file_contents[LINE_LEN] = "XX MMC/SD FAT16 library by Anton Rieckertn";
  8. char           filename[14] = "MIKRO00x.TXT";          // File names
  9. unsigned short loop, loop2;
  10. unsigned long  i, size;
  11. char           Buffer[512];

  12. // UART1 write text and new line (carriage return + line feed)
  13. void UART1_Write_Line(char *uart_text) {
  14.   UART1_Write_Text(uart_text);
  15.   UART1_Write(13);
  16.   UART1_Write(10);
  17. }

  18. // Creates new file and writes some data to it
  19. void M_Create_New_File() {
  20.   filename[7] = 'A';
  21.   Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
  22.   Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one
  23.   Mmc_Fat_Rewrite();                        // To clear file and start with new data
  24.   for(loop = 1; loop <= 99; loop++) {
  25.     UART1_Write('.');
  26.     file_contents[0] = loop / 10 + 48;
  27.     file_contents[1] = loop % 10 + 48;
  28.     Mmc_Fat_Write(file_contents, LINE_LEN-1);   // write data to the assigned file
  29.   }
  30. }

  31. // Creates many new files and writes data to them
  32. void M_Create_Multiple_Files() {
  33.   for(loop2 = 'B'; loop2 <= 'Z'; loop2++) {
  34.     UART1_Write(loop2);                  // signal the progress
  35.     filename[7] = loop2;                 // set filename
  36.     Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
  37.     Mmc_Fat_Assign(&filename, 0xA0);     // find existing file or create a new one
  38.     Mmc_Fat_Rewrite();                   // To clear file and start with new data
  39.     for(loop = 1; loop <= 44; loop++) {
  40.       file_contents[0] = loop / 10 + 48;
  41.       file_contents[1] = loop % 10 + 48;
  42.       Mmc_Fat_Write(file_contents, LINE_LEN-1);  // write data to the assigned file
  43.     }
  44.   }
  45. }

  46. // Opens an existing file and rewrites it
  47. void M_Open_File_Rewrite() {
  48.   filename[7] = 'C';
  49.   Mmc_Fat_Assign(&filename, 0);
  50.   Mmc_Fat_Rewrite();
  51.   for(loop = 1; loop <= 55; loop++) {
  52.     file_contents[0] = loop / 10 + 48;
  53.     file_contents[1] = loop % 10 + 48;
  54.     Mmc_Fat_Write(file_contents, LINE_LEN-1);    // write data to the assigned file
  55.   }
  56. }

  57. // Opens an existing file and appends data to it
  58. //               (and alters the date/time stamp)
  59. void M_Open_File_Append() {
  60.    filename[7] = 'B';
  61.    Mmc_Fat_Assign(&filename, 0);
  62.    Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 20, 0);
  63.    Mmc_Fat_Append();                                    // Prepare file for append
  64.    Mmc_Fat_Write(" for mikroElektronika 2010n", 27);   // Write data to assigned file
  65. }

  66. // Opens an existing file, reads data from it and puts it to UART
  67. void M_Open_File_Read() {
  68.   char character;
  69.   
  70.   filename[7] = 'B';
  71.   Mmc_Fat_Assign(&filename, 0);
  72.   Mmc_Fat_Reset(&size);            // To read file, procedure returns size of file
  73.   for (i = 1; i <= size; i++) {
  74.     Mmc_Fat_Read(&character);
  75.     UART1_Write(character);        // Write data to UART
  76.   }
  77. }

  78. // Deletes a file. If file doesn't exist, it will first be created
  79. // and then deleted.
  80. void M_Delete_File() {
  81.   filename[7] = 'F';
  82.   Mmc_Fat_Assign(filename, 0);
  83.   Mmc_Fat_Delete();
  84. }

  85. // Tests whether file exists, and if so sends its creation date
  86. // and file size via UART
  87. void M_Test_File_Exist() {
  88.   unsigned long  fsize;
  89.   unsigned int   year;
  90.   unsigned short month, day, hour, minute;
  91.   unsigned char  outstr[12];

  92.   filename[7] = 'B';       //uncomment this line to search for file that DOES exists
  93. //  filename[7] = 'F';       //uncomment this line to search for file that DOES NOT exist
  94.   if (Mmc_Fat_Assign(filename, 0)) {
  95.     //--- file has been found - get its create date
  96.     Mmc_Fat_Get_File_Date(&year, &month, &day, &hour, &minute);
  97.     UART1_Write_Text(" created: ");
  98.     WordToStr(year, outstr);
  99.     UART1_Write_Text(outstr);
  100.     ByteToStr(month, outstr);
  101.     UART1_Write_Text(outstr);
  102.     WordToStr(day, outstr);
  103.     UART1_Write_Text(outstr);
  104.     WordToStr(hour, outstr);
  105.     UART1_Write_Text(outstr);
  106.     WordToStr(minute, outstr);
  107.     UART1_Write_Text(outstr);
  108.    
  109.     //--- file has been found - get its modified date
  110.     Mmc_Fat_Get_File_Date_Modified(&year, &month, &day, &hour, &minute);
  111.     UART1_Write_Text(" modified: ");
  112.     WordToStr(year, outstr);
  113.     UART1_Write_Text(outstr);
  114.     ByteToStr(month, outstr);
  115.     UART1_Write_Text(outstr);
  116.     WordToStr(day, outstr);
  117.     UART1_Write_Text(outstr);
  118.     WordToStr(hour, outstr);
  119.     UART1_Write_Text(outstr);
  120.     WordToStr(minute, outstr);
  121.     UART1_Write_Text(outstr);
  122.    
  123.     //--- get file size
  124.     fsize = Mmc_Fat_Get_File_Size();
  125.     LongToStr((signed long)fsize, outstr);
  126.     UART1_Write_Line(outstr);
  127.   }
  128.   else {
  129.     //--- file was not found - signal it
  130.     UART1_Write(0x55);
  131.     Delay_ms(1000);
  132.     UART1_Write(0x55);
  133.   }
  134. }


  135. // Tries to create a swap file, whose size will be at least 100
  136. // sectors (see Help for details)
  137. void M_Create_Swap_File() {
  138.   unsigned int i;

  139.   for(i=0; i<512; i++)
  140.     Buffer[i] = i;

  141.   size = Mmc_Fat_Get_Swap_File(5000, "mikroE.txt", 0x20);   // see help on this function for details

  142.   if (size) {
  143.     LongToStr((signed long)size, err_txt);
  144.     UART1_Write_Line(err_txt);

  145.     for(i=0; i<5000; i++) {
  146.       Mmc_Write_Sector(size++, Buffer);
  147.       UART1_Write('.');
  148.     }
  149.   }
  150. }

  151. // Main. Uncomment the function(s) to test the desired operation(s)
  152. void main() {
  153.   #define COMPLETE_EXAMPLE         // comment this line to make simpler/smaller example
  154.   ADCON1 |= 0x0F;                  // Configure AN pins as digital
  155.   CMCON  |= 7;                     // Turn off comparators
  156.   
  157.   // Initialize UART1 module
  158.   UART1_Init(19200);
  159.   Delay_ms(10);

  160.   UART1_Write_Line("PIC-Started"); // PIC present report

  161.   // Initialize SPI1 module
  162.   SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  163.   
  164.   // use fat16 quick format instead of init routine if a formatting is needed
  165.   if (Mmc_Fat_Init() == 0) {
  166.     // reinitialize spi at higher speed
  167.     SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  168.     //--- Test start
  169.     UART1_Write_Line("Test Start.");
  170.     //--- Test routines. Uncomment them one-by-one to test certain features
  171.     M_Create_New_File();
  172.     #ifdef COMPLETE_EXAMPLE
  173.       M_Create_Multiple_Files();
  174.       M_Open_File_Rewrite();
  175.       M_Open_File_Append();
  176.       M_Open_File_Read();
  177.       M_Delete_File();
  178.       M_Test_File_Exist();
  179.       M_Create_Swap_File();
  180.     #endif
  181.     UART1_Write_Line("Test End.");

  182.   }
  183.   else {
  184.     UART1_Write_Line(err_txt); // Note: Mmc_Fat_Init tries to initialize a card more than once.
  185.                                //       If card is not present, initialization may last longer (depending on clock speed)
  186.   }

  187. }

复制代码
详细内容,请参考:http://www.51hei.com/bbs/dpj-138111-1.html

MMC-SD.jpg

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

llllll008 发表于 2018-10-27 11:16 | 显示全部楼层
这是自带库还是自己写的sd卡通信
回复

使用道具 举报

 楼主| oldspring 发表于 2018-10-27 21:35 | 显示全部楼层
这是编译软件自带库的MMC/sd 测试小程序。
详细内容,请参考:http://www.51hei.com/bbs/dpj-138111-1.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|单片机论坛 |51hei电子论坛2群 联系QQ:125739409;技术交流QQ群219535678

Powered by 单片机教程网

快速回复 返回顶部 返回列表