Revision: 1290
Author: [email protected]
Date: Fri Sep  4 21:05:08 2009
Log: [No log message]
http://code.google.com/p/jallib/source/detail?r=1290

Added:
  /trunk/sample/16f877_sd_card.jal

=======================================
--- /dev/null
+++ /trunk/sample/16f877_sd_card.jal    Fri Sep  4 21:05:08 2009
@@ -0,0 +1,96 @@
+-- Title: Library for communicating with SD memory cards
+-- Author: Matthew Schinkel - borntechi.com, copyright (c) 2009, all  
rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4l
+--
+-- 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
+--
+
+-- compiler dependant device definitions
+include 16f877
+;include 16f877a
+;include 18f452
+
+pragma target OSC HS               -- HS crystal or resonator
+pragma target clock 20_000_000     -- oscillator frequency
+--
+pragma target WDT  disabled
+pragma target LVP  disabled
+
+;const bit enhanced_flash_eeprom = true
+
+include delay           -- delay library
+
+-- setup uart for communication
+;const usart_hw_serial     = true    -- true = rs232, false = spi
+const serial_hw_baudrate  = 38400    -- set the baudrate
+include serial_hardware
+serial_hw_init()
+
+-- pin assignments, choose the pin your led is connected to
+alias     led                is pin_c0           -- the led pin
+alias     led_direction      is pin_c0_direction -- the pin's directional  
select bit
+
+include delay                 -- include the delay library
+
+-- setup spi
+include spi_master_hw         -- includes the spi library
+spi_init(SPI_MODE_11,SPI_RATE_FOSC_16)
+-- define spi inputs/outputs
+pin_sdi_direction = input    -- spi input
+pin_sdo_direction = output   -- spi output
+pin_sck_direction = output   -- spi clock
+-- spi chip select pin
+ALIAS chip_select_direction   is pin_SS_direction
+ALIAS chip_select             is pin_SS
+chip_select_direction = output    -- chip select/slave select pin
+chip_select = low -- start chip slect low
+
+include sd_card -- include sd card library
+sd_init()       -- initalize the sd card
+
+------------------------------------------------------------
+-- START of PROGRAM
+------------------------------------------------------------
+
+_usec_delay(100_000)                  -- wait for power to settle
+
+var byte low_byte, high_byte          -- vars for sending and recieving  
data
+
+-- read the boot sector (sector 0)
+sd_start_read(0)                    -- get sd card ready for read at  
sector 233
+for 256 loop                          -- read 1 sector (256 words)
+  sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write (low_byte)          -- send byte via serial port
+  serial_hw_write (high_byte)         -- send byte via serial port
+end loop
+sd_stop_read()                        -- tell sd card you are done reading
+
+-- write (0x66, 0x22) to sector 20 over and over.
+low_byte = 0x66                       -- set low byte to write
+high_byte = 0x22                      -- set high byte to write
+
+sd_start_write(20)                    -- get sd card ready for write
+for 256 + 128 loop                    -- write 1 sector + 1/2 sector
+  sd_write_data(low_byte, high_byte)  -- write data to the card
+end loop
+sd_write_to_sector_end(0x00)          -- 2nd sector is not done, so  
finnish it
+                                      -- sectors must be completed during  
write
+
+sd_stop_write()                       -- tell sd card you are done writing
+
+-- read the data back, should get (0x66, 0x22) over and over.
+sd_start_read(20)                     -- get sd card ready for read at  
sector 20
+for 512 loop                          -- read 2 sectors (512 words)
+  sd_read_data (low_byte, high_byte)  -- read 2 bytes of data
+  serial_hw_write (low_byte)          -- send byte via serial port
+  serial_hw_write (high_byte)         -- send byte via serial port
+end loop
+sd_stop_read()                        -- tell sd card you are done reading

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to