View previous topic :: View next topic |
Author |
Message |
theasus
Joined: 31 May 2009 Posts: 79
|
SD card question |
Posted: Sat Feb 06, 2010 11:24 am |
|
|
I have writen this code for Read/Write operation from SD CARD.But I have a problem because when I compile this code,it gives one error;
"Data item too big"
This program writes a value(10) to 0.adress and then it reads 0.adress.
How can we fix this code?
Code: |
#include <16F877.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <stdlib.h>
#use fast_io(c)
#use fast_io(c)
#define MMCSD_PIN_SCL PIN_C3 //o
#define MMCSD_PIN_SDI PIN_C4 //i
#define MMCSD_PIN_SDO PIN_C5 //o
#define MMCSD_PIN_SELECT PIN_C2 //o
#include <mmcsd.c>
#include <input.c>
void main(void)
{
BYTE value, cmd;
int16 address;
if (mmcsd_init())
{
printf("Could not init the MMC/SD!!!!");
while(TRUE);
}
while (TRUE);
{
/////////////////////////////////////
value=10;
address = 0;
mmcsd_write_byte(address, value);
mmcsd_flush_buffer();
delay_ms(1000);
//////////////////////////////////////
address=10;
mmcsd_read_byte(address, &value);
printf("\r\nValue: %X\r\n", value);
delay_ms(1000);
///////////////////////////////////
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 06, 2010 11:42 am |
|
|
It would be nice when CCS provided better documentation with the driver code...
Your problem has been discussed many times before on this forum and is caused by the limited RAM capacity of the PIC16 processors. The SD and MMC cards have to be written in blocks of 512 bytes and this is more memory than present in the PIC16.
Reading from the card can be done in single bytes if you want to, so if you only want to read the SD card you can use the PIC16, but if you want to write even a single byte to the memory card you'll have to upgrade to a PIC18 or larger processor. |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sat Feb 06, 2010 12:15 pm |
|
|
Ok I can change my microcontroller to 18f4550. But I have tried this code at Proteus. But I couldn't see anything.
Are these codes true? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Feb 06, 2010 4:03 pm |
|
|
Quote: |
This program writes a value(10) to 0.adress and then it reads 0.adress. | No, your program writes to address 0 and then reads from address 10.
Quote: | But I have tried this code at Proteus. But I couldn't see anything. | Are you sure you have made a correct setup? For example, can you make a LED blink?
What is your compiler's version number? |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Sun Feb 07, 2010 5:36 am |
|
|
My compiler version is 4.068
And here is my new program but I'm not sure Is it true?
Code: |
#include <18F452.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <stdlib.h>
#use fast_io(c)
#use fast_io(c)
#define MMCSD_PIN_SCL PIN_C3 //o
#define MMCSD_PIN_SDI PIN_C4 //i
#define MMCSD_PIN_SDO PIN_C5 //o
#define MMCSD_PIN_SELECT PIN_C2 //o
#include <mmcsd.c>
#include <input.c>
void main(void)
{
BYTE value, cmd;
int16 address;
if (mmcsd_init())
{
printf("Could not init the MMC/SD!!!!");
while(TRUE);
}
while (TRUE);
{
/////////////////////////////////////
value=10;
address = 0;
mmcsd_write_byte(address, value);
mmcsd_flush_buffer();
delay_ms(1000);
//////////////////////////////////////
address=0;
mmcsd_read_byte(address, &value);
printf("\r\nValue: %X\r\n", value);
delay_ms(1000);
///////////////////////////////////
}
} |
|
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE |
Posted: Sun Feb 07, 2010 7:51 am |
|
|
addresses have to start in blocks of 512 bytes,
so first address location is 0, the next is 512..1024 etc.
thanks
arunb |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Tue Feb 09, 2010 2:24 pm |
|
|
Is my program correct for simple Read/Write operation? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 09, 2010 2:48 pm |
|
|
Your code is based on this CCS example file:
Quote: | c:\program files\picc\examples\ex_mmcsd.c |
You have the address declared as an 'int16':
Quote: | void main(void)
{
BYTE value, cmd;
int16 address;
|
But CCS has it declared as an 'int32':
Quote: |
void main(void)
{
BYTE value, cmd;
int32 address;
|
If it doesn't work, post a list of your external hardware connections
between the PIC and the SD card. Include a description of any
external circuits, including pull-up resistors. |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Mon Mar 01, 2010 10:20 am |
|
|
OK thank you. I have no problem with R/W operation to SD card. But I have one more question about capacity of SD card. For example I have a SD card and its capacity is 1GB:
1GB=1024000000 Byte
SD card first adress will be 0 second adress 512 third adress will be 1024. And I can record:
1024000000/512= 2000000 data to my SD card.
Is this calculation true?
Thanks for consideration. |
|
|
collink
Joined: 08 Jan 2010 Posts: 137 Location: Michigan
|
|
Posted: Mon Mar 01, 2010 11:04 am |
|
|
A gigabyte is 2 to the power of 30 bytes which is 1073741824 bytes. You can record this much data to the SD card. However, the card is likely to have a file system and it takes up some of that space.
You can access all of the 1073741824 bytes but you must do so in 512 byte chunks. As in, you read in or write 512 bytes at a time no matter if you actually wanted 1 byte or 512.
theasus wrote: | OK thank you. I have no problem with R/W operation to SD card. But I have one more question about capacity of SD card. For example I have a SD card and its capacity is 1GB:
1GB=1024000000 Byte
SD card first adress will be 0 second adress 512 third adress will be 1024. And I can record:
1024000000/512= 2000000 data to my SD card.
Is this calculation true?
Thanks for consideration. |
|
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Tue Mar 02, 2010 6:25 am |
|
|
collink wrote: | You can access all of the 1073741824 bytes but you must do so in 512 byte chunks. As in, you read in or write 512 bytes at a time no matter if you actually wanted 1 byte or 512. |
Not quite. Writes are 512 bytes at a time but reads do not. You can set the SD/MMC cards embedded controller block size for read operations. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
husam
Joined: 17 Feb 2010 Posts: 10 Location: jordan
|
|
Posted: Tue Mar 02, 2010 6:28 am |
|
|
Hi,
Does anyone have an example on Proteus using FAT & mmcsd library from ccs compiler, because it doesn't work.
thnx |
|
|
theasus
Joined: 31 May 2009 Posts: 79
|
|
Posted: Wed Mar 10, 2010 5:45 am |
|
|
Are these connections true for these codes?
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Mar 10, 2010 7:14 am |
|
|
In real world you want the PIC to run at 3.3V and not at 5V. I guess it will work in Proteus, but in the real world the shown schematic has two effects:
1) The 5V can/will damage the SD card. A resistor voltage divider can solve this.
2) the 3.3V from the card is not high enough for the PIC to be seen as a logic high. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Thu Mar 11, 2010 7:05 am |
|
|
ckielstra wrote: | In real world you want the PIC to run at 3.3V and not at 5V. I guess it will work in Proteus, but in the real world the shown schematic has two effects:
1) The 5V can/will damage the SD card. A resistor voltage divider can solve this.
2) the 3.3V from the card is not high enough for the PIC to be seen as a logic high. |
As ckielstra mentioned, your schematic needs some work. You will find working reference configs on our projects page. Also if this is a new development, you might want to consider using a PIC18F4620. It is pin compatible with the 18F452 but offers more program memory and RAM and is therefore better suited to SD/MMC applications. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
|