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

📄 sd_test.pbas

📁 mmc/sd卡的实现源码
💻 PBAS
字号:
' *
' * 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:              mikroBasic v2.0.0.1
' * NOTES:
'     - This example works on PIC18 MCUs only.
'*
program SD_test

dim data                 as  byte[512]
    serial_buffer        as  byte[2]
    data_for_registers   as  byte[16]
    received_character, serial_pointer                as  byte
    first_byte, second_byte, third_byte, fourth_byte  as  byte
    i, px, k        as  word
    sector_address  as  longint
    we  as  byte

sub procedure printhex(dim ix  as  byte)
dim hix, lox  as  byte
  hix = ix and 0xF0               ' High nibble
  hix = hix >> 4
  hix = hix + $30
  if hix > $39  then
  hix= hix + 7
  end if
  lox = (ix and 0x0F) + $30       ' Low nibble
  if lox > $39  then
  lox= lox + 7
  end if
  USART_Write(hix)
  USART_Write(lox)
end sub

sub procedure USART_Out(dim byref text as char[40])
dim im, jm as byte
  im = 0
  jm = text[0]
  while jm <> 0
       USART_Write(jm)
       inc(im)
       jm = text[im]
  wend
end sub

sub procedure USART_Out_Ln(dim byref text as char[40])
dim im, jm as  byte

  im = 0
  jm = text[0]
  while jm <> 0
       USART_Write(jm)
       inc(im)
       jm = text[im]
  wend
  USART_Write(13)  ' LF
  USART_Write(10)  ' CR
end sub

main:
  PORTC =0
  USART_Init(19200)
  TRISC = $D3
  USART_Out_Ln("PIC-Started") ' see if PIC is there

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

  if i = 0 then
    USART_Out_Ln("SD Init-OK")     ' write if card is plug-in
  else
    USART_Out_Ln("SD Init-Error") ' there is no card
  end if

  for i = 0 to 511
   data[i] = "r" ' Fill SD buffer with same characters
  next i
  i = SD_Write_Sector(55, data)

  if i = 0 then  Usart_Out_Ln("Write-OK")
           else  Usart_Out_Ln("Write-Error")
           end if
  ' Reading of CID and CSD register on SD card.....

    i = SD_Read_CID(data_for_registers)
    if i = 0 then
              for k=0 to 15
                printhex(data_for_registers[k])
                if (k <> 15) then USART_Write("-")
                end if
              next k
              USART_Write(13)
       else
          Usart_Out_Ln("CID-Error")
       end if
    USART_Write(13)

    i = SD_Read_CSD(data_for_registers)
    if i = 0 then
          for k=0 to 15
              printhex(data_for_registers[k])
              if (k <> 15) then USART_Write("-")
              end if
          next k  
          USART_Write(13)
          USART_Write(10)

    else
      Usart_Out("CSD-Error")
    end if
  ' Variables initialisation
  serial_pointer = 0
  LCD_Cmd(LCD_SECOND_ROW)

  ' MAIN loop
  while true
    if USART_Data_Ready<>0 then
      serial_buffer[serial_pointer] = USART_Read()    ' Get the received character
      inc(serial_pointer)
      if (serial_pointer >=2) then
        serial_pointer = 0
        ' geting 4 byte to colect longint value for long address!
        if (serial_buffer[0] = "S") then first_byte  = serial_buffer[1]
        end if
        if (serial_buffer[0] = "s") then second_byte = serial_buffer[1]
        end if
        if (serial_buffer[0] = "E") then third_byte  = serial_buffer[1]
        end if
        if (serial_buffer[0] = "e") then fourth_byte = serial_buffer[1]
        end if
        if (serial_buffer[0] = "R") then
          if (serial_buffer[1] = "r") then
            sector_address = (first_byte << 24) or (second_byte << 16) or
                              (third_byte << 8)  or fourth_byte
            i = SD_Read_Sector(sector_address,data)
            if i = 0 then
              for k=0 to 511
                printhex(data[k])
                USART_Write(" ")
                if word((k+1) mod 16)=0 then
                  USART_Write(" ")
                  for px=(k-15) to k
                    we=data[px]
                    if (we > 33) and (we < 126) then
                      USART_Write(data[px])
                    else
                      USART_Write(".")
                    end if
                  next px
                  USART_Write(13)
                end if
                next k
              USART_Write(13)
              USART_Write(10)
            else
               USART_Out("Rd-Error")
            end if   
            USART_Write(13)
            USART_Write(10)
          end if
        end if
          
        if serial_buffer[0] = "W" then
          if serial_buffer[1] = "w" then
            'generate 32-bit sector address
            sector_address = (first_byte << 24) or (second_byte << 16) or
                              (third_byte << 8)  or fourth_byte
            for k=0 to 511
              data[k] = received_character  ' fill RAM buffeer with receiving characters
            next k
            i = SD_Write_Sector(sector_address, data)       ' write buffer to SD
            if (i = 0) then  USART_Out_Ln("Wr-OK")
            end if
            if (i = 1) then  USART_Out_Ln("Wr-Sending-Error")
            end if
            if (i = 2) then  USART_Out_Ln("Wr-Writing-Error")
            end if
          end if
        end if
        if serial_buffer[0] = "C" then received_character = serial_buffer[1]
        end if
        
      end if
    end if
 wend
  
  
end.

⌨️ 快捷键说明

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