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

📄 sd_test.ppas

📁 mmc/sd卡的实现源码
💻 PPAS
字号:
{*
 * Project name:
     SD_Test (Demonstration of SD Library usage)
 * Copyright:
     (c) MikroElektronika, 2005.
 * Description:
     This project demonstrates usage of the SD Library routines, to access,
     read to and write from the SD (Secure Dicital) card storage media.
     Upon initialization, this example stores some data on the SD card and in-
     forms the user about the activity status.
 * 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.
 *}
program SD_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;

begin

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

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

  if i = 0 then
    USART_Out('SD Init-OK')
  else
    USART_Out('SD Init-Error');


  USART_Write(13);
  USART_Write(10);

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

  i := SD_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 SD card.....
    i := SD_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 := SD_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;

        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 := SD_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
              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;
              i := SD_Write_Sector(sector_address, data);
              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 + -