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

📄 mmc_test.ppas

📁 mmc/sd卡的实现源码
💻 PPAS
字号:
{*
 * 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 on PORTC
     SW:              mikroPascal v3.0.0.3
 * NOTES:
     - This example works on PIC18 MCUs only.
     - In order to establish communication with MMC Terminal tool, the board
       must be connected to the RS232 port on PC.
 *}
program MMC_Test;

var data                : array[512] of byte;
    serial_buffer       : array[2] of byte;
    data_for_registers  : array[16] of byte;
    received_character, serial_pointer               : byte;
    first_byte, second_byte, third_byte, fourth_byte : byte;
    i, px, k       : word;
    sector_address : longint;
    we : byte;

procedure printhex(ix : byte);
var hix, lox : byte;
begin
  hix := ix and 0xF0;               // High nibble
  hix := hix shr 4;
  hix := hix + $30;
  if hix > $39  then hix:= hix + 7;
  lox := (ix and 0x0F) + $30;       // Low nibble
  if lox > $39  then lox:= lox + 7;
  USART_Write(hix);
  USART_Write(lox);
end;

procedure USART_Out(var text : array[40] of char);
var im, jm: byte;
begin
  im := 0;
  jm := text[0];
  while jm <> 0 do
  begin
    USART_Write(jm);
    Inc(im);
    jm := text[im];
  end;
end;

//-------------- main
begin
  PORTC :=0;
  USART_Init(19200);
  TRISC := $D3;
  USART_Out('PIC-Started'); // see if PIC is there
  USART_Write(13);  //
  USART_Write(10);  //

  // Beffore all, we must initialise a MMC card
  i := MMC_Init(PORTC,2);

  if i = 0 then
    USART_Out('MMC Init-OK')     // write if card is plug-in
  else
    USART_Out('MMC Init-Error'); // there is no card

  USART_Write(13);
  USART_Write(10);

  for i:=0 to 511 do
    data[i] := 'r';              // Fill MMC buffer with same characters

  i := MMC_Write_Sector(55, data);

  if i = 0 then
    Usart_Out('Write-OK')
  else
    Usart_Out('Write-Error');
           
  USART_Write(13);
  USART_Write(10);
  
  // Reading of CID and CSD register on MMC card.....
  i := MMC_Read_CID(data_for_registers);
  if i = 0 then
    begin
      for k:=0 to 15 do
      begin
        printhex(data_for_registers[k]);
        if (k <> 15) then USART_Write('-');
      end;
      USART_Write(13);
    end
  else
  Usart_Out('CID-Error');

  USART_Write(13);

  i := MMC_Read_CSD(data_for_registers);
  if i = 0 then
    begin
      for k:=0 to 15 do
      begin
        printhex(data_for_registers[k]);
        if (k <> 15) then USART_Write('-');
      end;
      USART_Write(13);
      USART_Write(10);
    end
  else
    Usart_Out('CSD-Error');

  // Variables initialisation
  serial_pointer := 0;
  Lcd_Cmd(LCD_SECOND_ROW);

  // MAIN loop
  while TRUE do
  begin
    if USART_Data_Ready<>0 then
    begin
      serial_buffer[serial_pointer] := USART_Read();    // Get the received character
      inc(serial_pointer);
      if (serial_pointer >=2) then
      begin
        serial_pointer := 0;
        // geting 4 byte to colect longint value for long address!
        if (serial_buffer[0] = 'S') then first_byte  := serial_buffer[1];
        if (serial_buffer[0] = 's') then second_byte := serial_buffer[1];
        if (serial_buffer[0] = 'E') then third_byte  := serial_buffer[1];
        if (serial_buffer[0] = 'e') then fourth_byte := serial_buffer[1];
        if (serial_buffer[0] = 'R') then
        if (serial_buffer[1] = 'r') then
          begin
            sector_address := (first_byte shl 24) or (second_byte shl 16) or
                              (third_byte shl 8)  or fourth_byte;

            i := MMC_Read_Sector(sector_address,data);
            if i = 0 then
              begin
                for k:=0 to 511 do
                begin
                  printhex(data[k]);
                  USART_Write(' ');
                  if ((k+1) mod 16)=0 then
                    begin
                      USART_Write(' ');
                      for px:=(k-15) to k do
                      begin
                        we:=data[px];
                        if (we > 33) and (we < 126) then
                          USART_Write(data[px])
                        else
                          USART_Write('.');
                      end;
                      USART_Write(13);
                    end;
                end;
                USART_Write(13);
                USART_Write(10);
              end
            else
              USART_Out('Rd-Error');
               
            USART_Write(13);
            USART_Write(10);
            end;

        if serial_buffer[0] = 'W' then
          if serial_buffer[1] = 'w' then
            begin
              // generisemo 32-bitnu adresu sektora od 4 primljena bajta

              sector_address := (first_byte shl 24) or (second_byte shl 16) or
                                (third_byte shl 8)  or fourth_byte;

              for k:=0 to 511 do
                data[k] := received_character;                   // fill RAM buffeer with receiving characters
              i := MMC_Write_Sector(sector_address, data);       // write buffer to MMC
              if (i = 0) then  USART_Out('Wr-OK');
              if (i = 1) then  USART_Out('Wr-Sending-Error');
              if (i = 2) then  USART_Out('Wr-Writing-Error');
              USART_Write(13);
              USART_Write(10);
            end;
        if serial_buffer[0] = 'C' then
          received_character := serial_buffer[1];
      end;
    end;
  end;
end.

⌨️ 快捷键说明

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