Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

[SOLVED] sd card interfacing with pic 18f4520 or with pic18f46k22 in MPLAB X XC8

Status
Not open for further replies.

Valdez85

Junior Member level 1
Joined
Jan 18, 2015
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
125
Hi guys, i have problems with interface my sd card with pic18f4520 or with pic18f46k22.

I saw many things and try many codes and i did my code to using sd libraries from microchip but never put the sd card working 100%.

Can someone help me? I can post the codes, versions of the programs, material, etc

Dont forget, im using mplab x and the compiler is XC8
 

Can you share the code and the datasheets/application note(s) for interfacing SD card with PIC18XXXX type microcontrollers,if you are using them?
 

hello,


I tested successfully Petite FatFs , with 18F46k22
Hardware is a 2Go SDCARD formateed in 512 bytes sectors FAT32
File name less than 12 chars ex: NAME0001.TXT
Storage to SDCARD need to fill first a buffer of 512 bytes ..
Files management is OK.

google with Petite Fatfs

and My Page about it
with all sources but in C18 MPLAB
 

Hi all,

I tryed many things to interface my PIC with SD Card but all without sucess.

I tryed the code from this site “ **broken link removed** “ and the SD Card works more or less with the PIC18F4520 at the start of my project, sometimes work, sometimes doesnt, but the hardware was wrong so i changed the hardware and now iam sure its ok but the SD Card stoped work.

I tryed to use the libraries from microchip (microchip_solutions_v2013-06-15), but never work because XC8 libraries to MDD is based on C18 and this work with HI TECH and not with XC8. I folowed the book of Dogan Ibrahim “PIC Microcontroller Projects in C. Basic to Advanced “ and he explain how to interface the PIC with SD Card but he used the “microchip_solutions_v2013-06-15 ” so when i compile the program i had errors.

I tryed to do my work based on this site “https://jonas.haksberg.net/ “ but the project of Jonas is based on C18 so i got almost the same errors.

I attach 1 zip with all my tests and codes and datasheets from my material, inside of the zip exist 2 zips, 1 of them had the code based on first i said, and the other is using the libraries of microchip following the book of Dogan Ibrahim.

paulfjujo i can see your project but you used the C18 so i go get the same errors that i have with the code i did based on microchip libraries :S

Im using this material:
MPLAB X v2.26
XC8 v1.33
PIC18F46K22 or PICF4520
MicroSD Card 2Gb Transcend
Breakout Board for microSD
Max232(Serial Port)
PicKit3
RealTerm
 

Attachments

  • ALL.rar
    16.3 MB · Views: 213
Last edited by a moderator:

hello,


I tested successfully Petite FatFs , with 18F46k22
Hardware is a 2Go SDCARD formateed in 512 bytes sectors FAT32
File name less than 12 chars ex: NAME0001.TXT
Storage to SDCARD need to fill first a buffer of 512 bytes ..
Files management is OK.

google with Petite Fatfs

and My Page about it
with all sources but in C18 MPLAB

I can see your project and your things but i guess i go have the same errors like using the microchip libraries because is based on C18 and im using XC8 :S
 

maybe your problem isn't software, but hardware. Are you sure that you have well connected the SD to the microcontroller? Have you used the pull up resistor?
 

if you have the same SDCARD interface as mine, don't forget that there are not pinout for
"presence of SD Card " and "Write Protect switch satus"

you also need to adapt FCLK_SLOW and FCLK_FAST (SPI spped) to your own FOSC..

Did you prepare the SDCARD?
Create file with a dummy caractere ( i add 12 files of 360ko, filled with space=0x20) on my SDCARD
under text editor win8

In my EDFinfo application,wich uses SDCARD storage, y added a Tracer inside PetieFat functions
to find out where the program block.
I used UART2 to printout evolution..

Do you have other hardware connected to the same pins as SDCARD ? (MISO,MOSI,SCLK,RST) ?

Because, two days ago,I fund a litle bug ,not really a bug if Only SDCARD is connected
but i have another device on RC3 and RC4 to drive a DS1302 RTC
(My RTC DS1302 is driven by simili spi bit bang, not Hardware spi)
and i discover that SDCARD free the SPI bus, only if a default is encountered, else don't release the bus..
So my clock was disturbed when Succesfully strore of data on SDCard
and was OK, when probleme on SDCARD.
I just added
SSPCON1bits.SSPEN=0; when Storage on SDCARD is finish.
to solve this probleme.

nota: i added 2 pull up resistor of 2,7K on SDCARD pins
because internal resistor on SDCARD are very high value : 50K
 

" maybe your problem isn't software, but hardware. Are you sure that you have well connected the SD to the microcontroller? Have you used the pull up resistor? "

This time the hardware is ok.

The next two images are my circuit.





Its possible that im not appling the necessary voltage in the pins for the SD card (CS, DI, DO and SLK) or the clock frequency is wrong.

" paulfjujo " for now i just connect the SD Card to the PIC, but i have all my work donne and its a data logger, just miss the SD Card, i have the RTCC, pH and temperature sensors working with the pic. I guess i havent problem with use the RTCC and SD Card with PIC18F46K22 because i can use SPI1 and I2C2 or SPI2 and I2C1, but with PICF4520 i had that problem that you said.
 
" paulfjujo " i forgot to ask, why you used so high frequency 10MHz * 4 (PLL).

In all examples i saw they always used high frequencys like you did or 20MHz * 4 or 16MHz * 4, we cant use 8MHz for example?
 

This time the hardware is ok.

The next two images are my circuit.
Strictly speaking, the hardware isn't fully O.K. The SPI input is of the ST pin type with a minimal high level 0f 0.8*Vdd (4.0 V) according to datasheet. It's not guranteed to work with a 3.3V source without level translation. This isn't necessarily the reason why your design doesn't work, but it could be and should be corrected.
 

hello,

my breackout sdcard is powered by 5V, my Pic also,
and i don't use voltage divider to drive the sdcard.

i allready came back to Q=20Mhz as FOSC !
 

" FvM " i said that because i can initiate the sd card but cant write.

" paulfjujo " if i wanna try your example with my hardware and C18 what i need to change? Now i just wanna be sure that my hardware is ok so i prefere try your code and see what happens.
 

hello

here is "Petite Fat Fs" test with 18F46K22 C18 Mplab

Check first Hardwareprofil.h
then Timer2 init at 1mS with your FOSC.
SDCARD must be initialised in FAT32 sector size of 512 bytes
Linker file modified to support sector_buffer of 512 bytes

View attachment _18F46k22_SDCard.zip
 

" paulfjujo " what version of C18 you used?

i try to compile your project and i got this:

" This version of MPLAB C18 does not support the extended mode
and will not perform all optimizations. To purchase a full
copy of MPLAB C18, please contact your local distributor or
visit buy.microchip.com.

WARNING: This version of MPLAB C18 does not support procedural abstraction. Procedural abstraction will not be run.

C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\pff.c:213:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\pff.c:213:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\pff.c:301:Warning [2055] expression is always false
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\pff.c:545:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\pff.c:818:Warning [2065] static function 'mem_cmp' never referenced
MPLAB C18 3.47 (feature limited)
Copyright 2000-2011 Microchip Technology Inc.
This version of MPLAB C18 does not support the extended mode
and will not perform all optimizations. To purchase a full
copy of MPLAB C18, please contact your local distributor or
visit buy.microchip.com.

WARNING: This version of MPLAB C18 does not support procedural abstraction. Procedural abstraction will not be run.

MPLAB C18 3.47 (feature limited)
Copyright 2000-2011 Microchip Technology Inc.
This version of MPLAB C18 does not support the extended mode
and will not perform all optimizations. To purchase a full
copy of MPLAB C18, please contact your local distributor or
visit buy.microchip.com.

WARNING: This version of MPLAB C18 does not support procedural abstraction. Procedural abstraction will not be run.

C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:184:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:188:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:313:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:315:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:320:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:320:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:325:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:400:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:401:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:408:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:411:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:417:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:420:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:422:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:427:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:427:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:431:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:444:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:449:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:454:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:459:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:462:Warning [2058] call of function without prototype
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:472:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:472:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:473:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:477:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:479:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:491:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:493:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:494:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:496:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:497:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:498:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:500:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:501:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:502:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:504:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:505:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:506:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:508:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:509:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:510:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:512:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:513:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:514:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:516:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:517:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:518:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:520:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:521:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:522:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:524:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:525:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:527:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:529:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:531:Warning [2058] call of function without prototype
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:537:Warning [2054] suspicious pointer conversion
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:540:Warning [2066] type qualifier mismatch in assignment
C:\Users\Valdez\Documents\MPLABX\testecartaoFR.X\testecartaoFR.c:543:Warning [2054] suspicious pointer conversion
"C:\Program Files (x86)\Microchip\C18\v3.47\bin\mplink.exe" "18f46k22_4bank512.lkr" -p18f46k22 -w -m"dist/default/production/testecartaoFR.X.production.map" -z__MPLAB_BUILD=1 -u_CRUNTIME -l "C:\Program Files (x86)\Microchip\C18\v3.47\bin"\\..\\lib -o dist/default/production/testecartaoFR.X.production.cof build/default/production/testecartaoFR.o build/default/production/diskio.o build/default/production/pff.o build/default/production/pff_.o
MPLINK 5.00, LINKER
Device Database Version 1.17
Copyright (c) 1998-2013 Microchip Technology Inc.
Error - symbol 'pf_mount' has multiple definitions.
Errors : 1

nbproject/Makefile-default.mk:167: recipe for target 'dist/default/production/testecartaoFR.X.production.hex' failed
make[2]: *** [dist/default/production/testecartaoFR.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make[2]: Leaving directory 'C:/Users/Valdez/Documents/MPLABX/testecartaoFR.X'
make: *** [.build-impl] Error 2
nbproject/Makefile-default.mk:78: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/Valdez/Documents/MPLABX/testecartaoFR.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 1s) "

My version of C18 is 3.47.




In this image show, what i did with the libraries.
 
Last edited by a moderator:

Hi guys, i saw one thing i guess is very important, if i try format my sd card in fat 16 with 512bytes its not possible.

In fat 16 i just can format the sd card in 32kbytes or 64kbytes but if i try format in fat 32 i can format the sd card with 512bytes.

So the question is, i had one code that works more or less, but works with fat 16 and 512bytes so if i try to format my sd card in fat 32 with 512bytes this never go work right?
 

You are talking about cluster size. It's increasing with card storage capacity under FAT16, because FAT 16 counts only 64k clusters. Main disadvantage is the inefficient storage of small files. Sector size is still 512 byte.
 

hello


C18 version 3.45
MPLAB IDE 8.92, not MPLABX !
and FAT32 with sector 512 bytes
 

Hi guys,

It's possible to write and read data on SD card without creating a file? I did my library in order to have the interface between my SD Card and PIC, in order to do that I just add SD Card protocol and SPI protocol and now I want to test this, to add FAT protocol, to my library.

If it's possible test, how can I do that?
Just need to connect the pic with SD card like the circuit that I attached in the past?

Best regards,

Álvaro Ramos
 

Hey guys,

Someone knows why i cant get the response from sd card when i send command 0?

I attached my code.
 

Attachments

  • cartao2.rar
    1 KB · Views: 114

Hi again,

This code of sd card works fine for me with mplax and compiler xc8.

I dont know why, but creates 3 files in sd card, i go do some more tests and if i fix the problem i post the changed code.

And one more thing, i dont know why but the code works with sandisk cards, i try more 4 diferent disks and never worked and the resistors between the pic and sd card are 1k and 2k.
 

Attachments

  • cartao.X.rar
    506.4 KB · Views: 174

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top