Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

need to strore data to sd card using pic16f877

Status
Not open for further replies.

piskot

Newbie level 6
Joined
Aug 3, 2007
Messages
11
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,339
fsr0l basic

hi! can anyone help me? i need to store data to an sd card using pic16f877. do i need card reader?im new to this please help me with the schematic.
 

x_zoli

Advanced Member level 4
Joined
Jul 22, 2004
Messages
105
Helped
9
Reputation
18
Reaction score
3
Trophy points
1,298
Location
Brasov, Romania
Activity points
786
sd card reader pic16f877a interface

The SD card has an SPI like interface, check out the application note on mass storage devices from microchip's website. Before you start with this project, you also have to study how to store data on sd cards - it is done one sector at a time, like on hdds, so you also need a buffer to store the data for at least one sector.
 

un-2000

Member level 2
Joined
Feb 18, 2002
Messages
45
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Location
colombia
Activity points
231
sd card sector

see you
proton pluss compiller manual
download manual
the command is:

download the compiler
and user guide to
sonsivri.com
see "the power is back"
see rego post "compilers and proton"

_________________________________________
Syntax

Variable = CF_READ

Overview

Read data from a Compact Flash card.

Operators
Variable - a user defined variable, of type bit, byte, byte array, word, word array, dword, or float that will hold the dta read from the Compact Flash card.
Example

' Read 16-bit values from 20 sectors in a compact flash card and display serially

Device = 16F877 ' We'll use a 14-bit core device
XTAL = 4

HSERIAL_BAUD = 9600 ' Set baud rate for USART serial coms
HSERIAL_RCSTA = %10010000 ' Enable serial port and continuous receive
HSERIAL_TXSTA = %00100100 ' Enable transmit and asynchronous mode
'-----------------------------------------------------------------------------------
' CF Card Declarations
CF_DTPORT = PORTD ' Assign the CF data port to PORTD
CF_ADPORT = PORTE ' Assign the CF address port to PORTE
CF_WEPIN = PORTC.5 ' Assign the CF WE pin to PORTC.5
CF_CE1PIN = PORTC.0 ' Assign the CF CE1 pin to PORTC.0
CF_RDYPIN = PORTC.4 ' Assign the CF RDY_BSY pin to PORTC.4
CF_OEPIN = PORTC.1 ' Assign the CF OE pin to PORTC.1
CF_RSTPIN = PORTC.3 ' Assign the CF RESET pin to PORTC.3
CF_CD1PIN = PORTA.5 ' Assign the CF CD1 pin to PORTA.5
CF_ADPORT_MASK = False ' No masking of address data required
CF_READ_WRITE_INLINE = False ' Use subroutines for CF_READ/CFWRITE

Symbol CF_CD1 = PORTA.5 ' Alias the CD1 pin to PORTA.5
'-----------------------------------------------------------------------------------
' Variable Declarations
Dim DATA_IO as Word ' Words read from CF card
Dim BUFFER_SIZE as Word ' Internal counter of bytes in sector (i.e.512)
Dim SECTOR_NUMBER as Dword ' Sector of interest

'-----------------------------------------------------------------------------------
' Main Program Starts Here
Delayms 100
All_Digital = True
CF_Init ' Initialise the CF card's IO lines
While CF_CD1 = 1 : Wend ' Is the Card inserted?
'-----------------------------------------------------------------------------------
' READ 8-bit values from sector 0 to sector 20 and display serially In columns and rows format
READ_CF:
SECTOR_NUMBER = 0 ' Start at sector 0
' Set up the CF card for reading 1 sector at a time in LBA mode
CF_Sector SECTOR_NUMBER,READ,1
Repeat ' Form a loop for the sectors
BUFFER_SIZE = 1
Hserout ["SECTOR ",Dec SECTOR_NUMBER,13]
Repeat ' Form a loop for words in sector
DATA_IO = CF_Read ' Read a Word from the CF card
Hserout [HEX4 DATA_IO," "] ' Display it in Hexadecimal
If BUFFER_SIZE // 32 = 0 Then Hserout [13] ' Check if row finished
Inc BUFFER_SIZE ' Move up a word
Until BUFFER_SIZE > 256 ' Until all words are read
Hserout [Rep "-"\95,13] ' Draw a line under each sector
Inc SECTOR_NUMBER ' Move up to the next sector
' And set up the CF card for reading in LBA mode
CF_Sector SECTOR_NUMBER,READ
Until SECTOR_NUMBER > 20 ' Until all sectors are read
Stop

Notes

The amount of bytes read from the Compact Card depends on the variable type used as the assignment. i.e. the variable before the equals operator: -

A BIT type variable will read 1 byte from the Compact Flash card.
A BYTE type variable will also read 1 byte from the Compact Flash card.
A WORD type variable will read 2 bytes from the Compact Flash card Least Significant Byte First (LSB).
A DWORD type variable will read 4 bytes from the Compact Flash card Least Significant Byte First (LSB).
A FLOAT type variable will also read 4 bytes from the Compact Flash card in the correct format for a floating point variable.

Accessing Compact Flash memory is not the same as conventional memory. There is no mechanism for choosing the address of the data in question. You can only choose the sector then sequentially read the data from the card. In essence, the sector is the equivalent of the address in a conventional piece of memory, but instead of containing 1 byte of data, it contains 512 bytes.

Once the sector is chosen using the CF_Sector command, any amount of the 512 bytes available can be read from the card. Once a read has been accomplished, the Compact Flash card automatically increments to the next byte in the sector ready for another read. So that a simple loop as shown below will read all the bytes in a sector:

BUFFER_SIZE = 0
Repeat ' Form a loop for bytes in sector
DATA_IO = CF_Read ' Read a Byte from the CF card
Inc BUFFER_SIZE ' Increment the byte counter
Until BUFFER_SIZE = 512 ' Until all Bytes are read

In order to extract a specific piece of data from a sector, a similar loop can be used, but with a condition attached that will drop out at the correct position: -

BUFFER_SIZE = 0
While 1 = 1 ' Form an infinite loop
DATA_IO = CF_Read ' Read a Byte from the CF card
If BUFFER_SIZE = 20 Then Break ' Exit when correct position reached
Inc BUFFER_SIZE ' Increment the byte counter
Wend ' Close the loop

The snippet above will exit the loop when the 20th byte has been read from the card.

Of course Arrays can also be loaded from a Compact Flash card in a similar way, but remember, the maximum size of an array in PROTON BASIC is 256 elements. The snippets below show two possible methods of loading an array with the data read from a Compact Flash card.

Dim AR1[256] as Byte ' Create a 256 element array
Dim BUFFER_SIZE as Word ' Internal counter of bytes in sector
BUFFER_SIZE = 0
Repeat ' Form a loop for bytes in sector
AR1[BUFFER_SIZE] = CF_Read ' Read a Byte from the CF card
Inc BUFFER_SIZE ' Increment the byte counter
Until BUFFER_SIZE = 256 ' Until all Bytes are read

Large arrays such as the one above are best suited to the 16-bit core devices. Not only because they generally have more RAM, but because their RAM is accessed more linearly and there are no BANK boundaries when using arrays. Also, by accessing some low level registers in a 16-bit core device it is possible to efficiently place all 512 bytes from a sector into 2 arrays:

Device = 18F452 ' Choose a 16-bit core device
Dim AR1[256] as Byte ' Create a 256 element array
Dim AR2[256] as Byte ' Create another 256 element array
Dim BUFFER_SIZE as Word ' Internal counter of bytes in sector
Dim FSR0 as FSR0L.Word ' Combine FSR0L/H as a 16-bit register
FSR0 = Varptr(AR1) ' Get the address of AR1 into FSR0L/H
Repeat ' Form a loop for bytes in sector
POSTINC0 = CF_Read ' Read a Byte from the CF card and place
' directly into memory, then increment to the next address in PIC RAM
Inc BUFFER_SIZE ' Increment the byte counter
Until BUFFER_SIZE = 512 ' Until all Bytes are read

When the above loop is finished, arrays AR1 and AR2 will hold the data read from the Compact Flash card’s sector. Of course you will need to pad out the snippets with the appropriate declares and the CF_Sector command.

See Also

CF_Init
CF_Sector (for a suitable circuit)
CF_Write (for declares
 
Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top