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

📄 mmc_test.c

📁 mmc/sd卡的实现源码
💻 C
字号:
/*
 * Project name:
     MMC_Test (Demonstration of MMC Library usage)
 * Copyright:
     (c) MikroElektronika, 2005.
 * Description:
     This project demonstrates usage of the MMC Library routines, to access,
     read to and write from the MMC (MultiMedia Card) storage media. It is to
     be used in conjuntction with the 'MMC Card Terminal' tool.
     Upon initialization, this example stores some data on the MMC card and in-
     forms the user about the activity status. The data can then be read over the
     'MMC terminal' tool.
 * Test configuration:
     MCU:             PIC18F452
     Dev.Board:       EasyPIC2
     Oscillator:      HS, 8.000 MHz
     Ext. Modules:    MMC_SD
     SW:              mikroC v2.0.0.2
 * NOTES:
     - This example works on PIC18 MCUs only.
*/

// if defined, we have a debug messages on PC terminal
#define RS232_debug 1

// universal variables
unsigned int px, k; // universal for loops and other stuff

// Variables for MMC routines
unsigned char data[512]; // Buffer for MMC sector reading/writing
unsigned char data_for_registers[16]; // buffer for CID and CSD registers

unsigned char MMC_SPI(unsigned char out);
unsigned char MMC_Send_Command(unsigned char partial_cmm, unsigned long address, unsigned char CRC);
unsigned char MMC_Init();
unsigned char MMC_Read_Sector(unsigned long sector, char *data);
unsigned char MMC_Write_Sector(unsigned long sector, char *data);
unsigned char MMC_Read_CID(char * data_for_registers);
unsigned char MMC_Read_CSD(char * data_for_registers);




// RS232 communication variables
unsigned char received_character;
unsigned long sector_address;
unsigned char first_byte, second_byte, third_byte, fourth_byte;
unsigned char serial_buffer[2];
unsigned char serial_pointer;

//-------------- Displays byte in hex
void printhex(unsigned char i) {
  unsigned char hi,lo;
  hi = i & 0xF0;               // High nibble
  hi = hi >> 4;
  hi = hi + '0';
  if (hi>'9') hi = hi + 7;
  lo = (i & 0x0F) + '0';       // Low nibble
  if (lo>'9') lo=lo+7;
  USART_Write(hi);
  USART_Write(lo);
}//~

void main() {
  unsigned int i;

  PORTC = 0;
  #ifdef RS232_debug
    USART_Init(19200);
  #endif

  TRISC = 0b11010011;

  #ifdef RS232_debug
    USART_Write('P'); // Report PIC presence
    USART_Write('I'); //
    USART_Write('C'); //
    USART_Write('-'); //
    USART_Write('S'); //
    USART_Write('t'); //
    USART_Write('a'); //
    USART_Write('r'); //
    USART_Write('t'); //
    USART_Write('e'); //
    USART_Write('d'); //
    USART_Write(13);  //
    USART_Write(10);  //
  #endif

  // Before all, we must initialise a MMC card
  i = MMC_Init();

  #ifdef RS232_debug
    if(i == 1) {
      USART_Write('M'); //Report MMC card presence
      USART_Write('M');
      USART_Write('C');
      USART_Write('I');
      USART_Write('n');
      USART_Write('i');
      USART_Write('t');
      USART_Write('-');
      USART_Write('O');
      USART_Write('K');
      USART_Write(13);
      USART_Write(10);
    }
    if(i == 0) {
      USART_Write('M'); // Report Init error(s)
      USART_Write('M');
      USART_Write('C');
      USART_Write('I');
      USART_Write('n');
      USART_Write('i');
      USART_Write('t');
      USART_Write('-');
      USART_Write('E');
      USART_Write('r');
      USART_Write('r');
      USART_Write(13);
      USART_Write(10);
    }
  #endif

  for(i=0; i<=511; i++) data[i] = 'E'; // Fill MMC buffer with same characters
  i = MMC_Write_Sector(55, data);

  #ifdef RS232_debug
    if(i == 0)
    {
      Usart_Write('U');
      Usart_Write('p');
      Usart_Write('i');
      Usart_Write('s');
      Usart_Write('-');
      Usart_Write('O');
      Usart_Write('K');
    }
    else  // Write incorrect....
    {
      Usart_Write('U');
      Usart_Write('p');
      Usart_Write('i');
      Usart_Write('s');
      Usart_Write('-');
      Usart_Write('E');
      Usart_Write('r');
      Usart_Write('r');
    }
    USART_Write(13);
    USART_Write(10);
  #endif

  // Reading of CID and CSD register on MMC card.....
  #ifdef RS232_debug
    i = MMC_Read_CID(data_for_registers);
    if(i == 0) {
      for(k=0; k<=15; k++) {
        printhex(data_for_registers[k]);
        if(k!=15) USART_Write('-');
      }
      USART_Write(13);
    }
    else {
      Usart_Write('C');
      Usart_Write('I');
      Usart_Write('D');
      Usart_Write('-');
      Usart_Write('E');
      Usart_Write('r');
      Usart_Write('r');
    }
    i == MMC_Read_CSD(data_for_registers);
    if(i == 0) {
      for(k=0; k<=15; k++) {
        printhex(data_for_registers[k]);
        if(k!=15) USART_Write('-');
      }
      USART_Write(13);
      USART_Write(10);
    }
    else {
      Usart_Write('C');
      Usart_Write('S');
      Usart_Write('D');
      Usart_Write('-');
      Usart_Write('E');
      Usart_Write('r');
      Usart_Write('r');
    }
  #endif

  // Variables initialisation
  serial_pointer = 0;

  // MAIN loop
  while(1)
  {
    if (USART_Data_Ready())
    {
      serial_buffer[serial_pointer] = USART_Read();    // Get the received character
      serial_pointer++;
      if(serial_pointer>1)
      {
        serial_pointer = 0;
        // get 4 bytes to generate (long) address
        if(serial_buffer[0] == 'S') first_byte  = serial_buffer[1];
        if(serial_buffer[0] == 's') second_byte = serial_buffer[1];
        if(serial_buffer[0] == 'E') third_byte  = serial_buffer[1];
        if(serial_buffer[0] == 'e') fourth_byte = serial_buffer[1];
        if(serial_buffer[0] == 'R') // Read memory command
        {
          if(serial_buffer[1] == 'r')
          {
            sector_address = ((long)first_byte << 24) + ((long)second_byte << 16) +
                             ((long)third_byte << 8)  + ((long)fourth_byte);
            i = MMC_Read_Sector(sector_address,data);
            if(i == 0)
            {
              for(k=0; k<512; k++)
              {
                printhex(data[k]);
                USART_Write(' ');
                if(((k+1) % 16)==0)
                {
                  USART_Write(' ');
                  for(px=(k-15); px<=k; px++)
                  {
                    if((data[px]>33) && (data[px]<126))
                    {
                      USART_Write(data[px]);
                    }
                    else
                    {
                      USART_Write('.');
                    }
                  }
                  USART_Write(13);
                }
              }
              USART_Write(13);  //
              USART_Write(10);
            }
            else
            {
              USART_Write('R');  //
              USART_Write('d');
              USART_Write('-');
              USART_Write('E');
              USART_Write('r');
              USART_Write('r'); // Report error
              USART_Write(13);  //
              USART_Write(10);  //
            }
          }
        }
        if(serial_buffer[0] == 'W') // Write command
        {
          if(serial_buffer[1] == 'w')
          {
            //generate 32-bit sector address
            sector_address = ((long)first_byte << 24) + ((long)second_byte << 16) +
                             ((long)third_byte << 8)  + ((long)fourth_byte);
            for(k=0; k<512; k++) data[k] = received_character;// fill RAM buffer with character received
            i = MMC_Write_Sector(sector_address, data); // ...and write it down to MMC
            if(i != 0)
            {
              // Report Write Error
              USART_Write('W');
              USART_Write('r');
              USART_Write('-');
              USART_Write('E');
              USART_Write('r');
              USART_Write('r');
              USART_Write(13);
              USART_Write(10);
            }
            else
            {
              USART_Write('W');  // Write OK
              USART_Write('r');
              USART_Write('-');
              USART_Write('O');
              USART_Write('K');
              USART_Write(13);  //
              USART_Write(10);  //
            }
          }
        }
        if(serial_buffer[0] == 'C')
        {
          received_character = serial_buffer[1];
        }
      }
    }
  }
}

⌨️ 快捷键说明

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