-- Title: Library for communicating with SD memory cards -- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all rights reserved. -- Adapted-by: -- Compiler: >=2.4n -- -- This file is part of jallib (http://jallib.googlecode.com) -- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) -- -- Description: this library provides functions for SD memory cards. -- -- Sources: -- SanDisk Secure Digital Card - http://www.cs.ucr.edu/~amitra/sdcard/ProdManualSDCardv1.9.pdf -- How to use MMC/SDC - http://forums.parallax.com/forums/attach.aspx?a=32012 -- include 16f877a -- target PICmicro -- -- This program assumes that a 20 MHz resonator or crystal -- is connected to pins OSC1 and OSC2. pragma target clock 20_000_000 -- oscillator frequency -- configuration memory settings (fuses) pragma target OSC HS -- HS crystal or resonator pragma target WDT disabled -- no watchdog pragma target LVP disabled -- no Low Voltage Programming -- enable_digital_io() -- disable all analog pins if any _usec_delay (100_000) -- wait for power to stablilize include delay -- setup uart for communication const serial_hw_baudrate = 115200 -- set the baudrate include serial_hardware serial_hw_init() -- some aliases so it is easy to change from serial hw to serial sw. alias serial_write is serial_hw_write alias serial_read is serial_hw_read alias serial_data is serial_hw_data alias serial_data_available is serial_hw_data_available include print include spi_master_hw -- includes the spi library -- define spi inputs/outputs pin_sdi_direction = input -- spi input pin_sdo_direction = output -- spi output pin_sck_direction = output -- spi clock -- spi_init(SPI_MODE_11,SPI_RATE_FOSC_4) -- init spi, choose mode and speed alias spi_master is spi_master_hw -- setup the sd card pins alias sd_chip_select is pin_SS alias sd_chip_select_direction is pin_SS_direction sd_chip_select = high sd_chip_select_direction = output -- -- setup the sd card library alias sd_force_spi_mode is spi_master_hw_set_mode_11 -- always set spi mode to 1,1 ;const bit SD_EXTRA_SPEED = TRUE const bit SD_ALWAYS_SET_SPI_MODE = TRUE const bit SD_DELAY_AFTER_WRITE = TRUE include sd_card -- include the sd card ide hard disk library sd_init() -- initialize startup settings ------------------------------------------------------------ -- START of PROGRAM ------------------------------------------------------------ _usec_delay(100_000) -- wait for power to settle -- procedure for sending 80 "-----------------" via serial port procedure seperator() is serial_data = 13 serial_data = 10 const byte str3[] = "--------------------------------------------------------------------------------" print_string(serial_data, str3) print_crlf(serial_data) end procedure -- Send something to the serial port seperator() -- send "----" via serial port var byte start_string[] = "SD CARD SAMPLE STARTED" print_string(serial_data,start_string) -- variables for the sample var word step1 var byte volatile data seperator() -- seperate the examples with "----" -------------------------------------------------------------------------------- -- EXAMPLE #1 read sector 0 (the boot sector) & sector 1, one byte at a time -------------------------------------------------------------------------------- sd_start_read(0) -- get sd card ready for read at sector 0 for 512 * 2 loop -- read 2 sectors (512 * 2 bytes) data = sd_data_byte -- read 1 bytes of data serial_write(data) -- send byte via serial port end loop sd_stop_read() -- tell sd card you are done reading seperator() -- seperate the examples with "----" _usec_delay(500_000) -- a small delay -------------------------------------------------------------------------------- -- EXAMPLE #2 write to a sector 1 byte at a time -------------------------------------------------------------------------------- sd_start_write(20) -- get sd card ready for write at sector 20 for 512 + 256 loop -- loop 1 sector + 1 half sector (512 + 256 bytes) sd_data_byte = "A" -- write 1 bytes of data end loop sd_stop_write() -- tell sd card you are done reading -- -- read the data back sd_start_read(20) -- get sd card ready for read at sector 20 for 512 + 256 loop -- loop 1 sector + 1 half sector (512 + 256 bytes) data = sd_data_byte -- read 1 bytes of data serial_write(data) -- send byte via serial port end loop sd_stop_read() -- tell sd card you are done reading seperator() -- seperate the examples with "----" _usec_delay(500_000) -- a small delay ; -------------------------------------------------------------------------------- ; -- EXAMPLE #3 write to 2 sectors using a sector buffer at a address ; -- user friendly and fastest. ; -------------------------------------------------------------------------------- ; -- fill the sector buffer with data ; for 512 using step1 loop -- loop till the end of the sector buffer ; sd_sector_buffer[step1] = "B" -- set each byte of data ; end loop ; -- write the sector buffer to sector 20 ; sd_write_sector_address(20) ; for 512 using step1 loop -- loop till the end of the sector buffer ; sd_sector_buffer[step1] = "C" -- set each byte of data ; end loop ; -- write the sector buffer to sector 21 ; sd_write_sector_address(21) ; -- ; -- read back the same sectors ; -- read sector 20 into the sector buffer ; sd_read_sector_address(20) ; -- now send it to the serial port ; for 512 using step1 loop -- loop till the end of the sector buffer ; serial_write (sd_sector_buffer[step1]) -- send each byte via serial port ; end loop ; -- read sector 21 into the sector buffer ; sd_read_sector_address(21) ; -- now send it to the serial port ; for 512 using step1 loop -- loop till the end of the sector buffer ; serial_write (sd_sector_buffer[step1]) -- send each byte via serial port ; end loop ; ; seperator() -- seperate the examples with "----" ; _usec_delay(500_000) -- a small delay ; -------------------------------------------------------------------------------- ; -- EXAMPLE #4 write to 2 sectors using a sector buffer. About the same speed as ; -- example 3. ; -------------------------------------------------------------------------------- ; -- get sd card ready for write at sector 20 ; sd_start_write(20) ; -- fill the sector buffer with data ; for 512 using step1 loop -- loop till the end of the sector buffer ; sd_sector_buffer[step1] = "D" -- set each byte of data ; end loop ; -- write the sector buffer to the sd card ; sd_write_sector() ; -- fill the sector buffer with new data ; for 512 using step1 loop -- loop till the end of the sector buffer ; sd_sector_buffer[step1] = "E" -- set each byte of data ; end loop ; -- write the sector buffer to the sd card ; sd_write_sector() -- write the buffer to the sd card ; -- tell sd card you are done writing ; sd_stop_write() ; -- ; -- read back both of the same sectors ; -- get sd card ready for read at sector 20 ; sd_start_read(20) ; -- read the sector into the sector buffer ; sd_read_sector() ; -- now send it to the serial port ; for 512 using step1 loop -- loop till the end of the sector buffer ; serial_write(sd_sector_buffer[step1]) -- send each byte via serial port ; end loop ; -- read the next sector into the sector buffer ; sd_read_sector() ; -- now send it to the serial port ; for 512 using step1 loop -- loop till the end of the sector buffer ; serial_write(sd_sector_buffer[step1]) -- send each byte via serial port ; end loop ; sd_stop_read() -- tell sd card you are done reading -------THE END!--------