View previous topic :: View next topic |
Author |
Message |
sjharris
Joined: 11 May 2006 Posts: 78
|
PIC to SD Card |
Posted: Tue Jun 28, 2011 9:19 am |
|
|
Hello all,
I am trying to add an SD Card to my PIC design. I currently have a board with the correct voltage levels connected to to SD Card (3.3V) which are connected to pins C3,C4,C5 and B3 (I am using 18f8722)
I am using the standard mmc_spi library that comes with CCS but I must be doing something wrong. The pins are defined in code and I am probing on the pins (attached image is snapshot)
I am just sending out the init() command every 1 second to try and get a reponse. I get nothing back from the card. Using an SDHC 4GB formated to FAT32.
Code and pictures attached
Code: |
/********************************************************************
*
* Author:- SJH
* Date:- 16 May 2011
* Version:- 1
*
* PIC:- 18F8722
*
*********************************************************************
* Scope:-
*********************************************************************
*
* Changes:-
*
*********************************************************************
*
* To Do:-
*
*********************************************************************
*
*
********************************************************************/
/*
SD Card PIC
------- ---
CS RB3
CLK RC3
DO RC5
DI RC4
*/
//*******************************************************************
// Includes
//*******************************************************************
#define MMC_CLK PIN_C3
#define MMC_DI PIN_C4
#define MMC_DO PIN_C5
#define MMC_CS PIN_B3
#include "D:\Projects\Tests\test.h"
#include "D:\Projects\Tests\flexLCD.c"
#include <mmc_spi.c>
//*******************************************************************
// Global Variables
//*******************************************************************
//*******************************************************************
// Interrupt Routines
//*******************************************************************
//*******************************************************************
// Functions
//*******************************************************************
//*******************************************************************
// CCS PIC Init
//*******************************************************************
CCS_Init()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_spi2(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_timer_4(T4_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_low_volt_detect(FALSE);
}
//*******************************************************************
// Other Init
//*******************************************************************
//*******************************************************************
// Main
//*******************************************************************
void main()
{
int response;
int attempt;
char temp_string[20];
attempt = 48;
response = 0;
CCS_Init(); //Initialise PIC
lcd_init(); //Initialise LCD
response = 1;
lcd_putc("SD Write test"); //Write on lcd
output_high(PIN_F0);
while (response !=0)
{
response = mmc_init();
delay_ms(1000);
}
output_low(PIN_F0);
lcd_putc('\f'); //Clear LCD Screen
delay_ms(1000); //Wait for 1 second
while(1)
{
output_high(PIN_F1);
delay_ms(1000);
output_low(PIN_F1);
delay_ms(1000);
}
}
|
and picture
The voltages a reading at 5V as I probed on the PIC side of the SD Card (I have tested the 3.3V and it does come out at 3.3V)
If you need any more screen shots let me know.
What I would like to know is if anyone has sucessfully managed to interface to SD Card (I see that there are a lot of discussions on here but not really much "Here is a .zip file of all the libs you need etc, etc" Does anyone have such a thing to get me started??
Thanks
sjharris |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jun 28, 2011 9:44 am |
|
|
You do realise you need voltage level translation between the SD card and the PIC, unless you run the PIC at 3.3v?.
On the output lines, simple resistive dividers, but doing the other way you need a logic gate that translates from 3.3v logic levels to 5v levels.
The 8722, will _require_ this. Alternative is to use the 18LF8722, at 16MHz max, off 3.3v.
Best Wishes |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Wed Jun 29, 2011 1:36 am |
|
|
Granted,
A level conversion would be required, but have probed on the output (from SD Card) line and there is not even 3.3 V there, so level conversion will not work.
Do my scope output even look correct? Without even doing this before I do not relly know what to look for.
Will keep checking my board and check signals are going to correct pins, I will post the data sheet for my SD Card holder for pin out later.
Cheers
sjharris |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Jun 29, 2011 4:04 am |
|
|
No.
You _need_ the level conversion in the other direction (resistive dividers), or you risk destroying the SD card. Also have you got the pullup resistors to 3.3v on the lines. Some cards work without these, but most require them.
Best Wishes |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Wed Jun 29, 2011 3:35 pm |
|
|
Nt quite sure I follow you 100%, are you saying I need the level conversion from 3.3V SD card to 5V pic before the card will transmit anything? I am not getting anything from the card (3.3V from the SD) tested with scope.
Also, Where would I need the pull ups? I am asking as I am using the reference design in the "SD card projects using the pic microcontroller" book. There are no pull ups on any lines just resistor dividers.
Also is there anything that I am missing from my code that would stop the SD card from working? Or is it purely hardware issues? As I said before, I havnt really done this before so I annoy sure what parts are wrong, if the code is wrong the hw won't work and vice versa. It would be good to k ow if I at least have the code correct or not? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Jun 29, 2011 4:48 pm |
|
|
First you MUST get the hardware right !! As Ttelmah said, you MUST have proper level translators between the PIC and the SD card.
Good chance you've blown the card. Easy test ...insert into a PC and see what happens.
There is NO point in looking at the code until proper hardware is built. Either add the interface chips or replace the PIC with the LF ( low voltage ) version.
AFTER that, then cut code and see what happens. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jun 29, 2011 7:50 pm |
|
|
Here is a link to an on-line version the mentioned book. Figure 9.17 shows the schematic, which is again one of the many flawed designs we have seen here on this forum before.
This schematic only has voltage dividers from the 5V PIC to the 3.3V SD card. Problem with this schematic is that there is no voltage conversion the other way around, the SDO line to the PIC will be out of specs.
Also the pull up resistors on the data lines are missing. Most likely this is why you get no signal from the card on the scope. The card starts up in MMC bus Mode where the DO line is driven by an Open-Collector output. Without the pull-up resistor there will only be a floating voltage. Only after the Reset command is received by the SD card it will switch to a push-pull output.
For some people the schematic works, but this is luck. It is a shame this schematic was publicized in a printed book. Just a quick glance at the rest of the book gives me the impression the book is poor quality.
Fix the hardware by inserting a proper level converter for the DO line from the memory card to the PIC. Or beter, replace your PIC by a low voltage (LF) version. Than you can get rid of all the glue logic (voltage dividers).
Note: My guess is that the LF-chips are just selected chips from the normal F-type. For a hobby project you could try to run your normal PIC processor (non-LF version) at 3.3V, get rid of the voltage dividers and add 100k pull up on the two data lines. Clock frequency should be kept low, max. 16MHz. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Wed Jun 29, 2011 8:17 pm |
|
|
Here you will find some operational reference designs for integrating an SD card with a PIC.
http://www.brushelectronics.com/index.php?page=projects
As already mentions, if you do not have a pullup resistor to VDD of the card (not the PIC) on DO of the card it is unlikely you will be able to communicate with the card. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Thu Jun 30, 2011 3:07 am |
|
|
Hi,
Pull up added to DO line (from SD Card). Data now seen on pic pin (but no response yet) probably difference in voltage, would a Transistor switch work, I understand the SPI bus can operate at 30MHz?
I am going to board design two that will incorporate these changes but I would just like to get this working before I do and I am trying with stuff that is lying around the lab. I ahve used FET's as 5V switches before but not switching that fast, hopefully they will work :-~ ??
Cheers
Steve |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jun 30, 2011 5:58 am |
|
|
Quote: | Pull up added to DO line (from SD Card). Data now seen on pic pin (but no response yet) probably difference in voltage, would a Transistor switch work, | Yes, a Voltage Follower design (a.k.a. common-drain amplifier) should work with most common FETs. In case of doubt study the datasheets for your FET.
Quote: | I understand the SPI bus can operate at 30MHz? | SD cards can be clocked up to 25MHz in Standard-speed mode, or up to 50 MHz in High-speed mode. The limiting factor here is the PIC which clocks the SPI bus at Fosc/4. The fastest PIC model at 64MHz then results in a SPI speed of 16MHz. |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Mon Jul 04, 2011 4:20 am |
|
|
Have used an OR gate to raise the level back to 5V for the PIC. This is the output from the OR gate when I send the mmc_spi init command (DO line on top for reference)
Note that the DI line (on bottom) is now +5V to interface to PIC.
The code does not want to break out of the loop, i.e. the response from the mmc_init() call is not returning a zero (i.e. non zero = error).
Any ideas what to check next? Does the response look correct from the SD Card?, the output from the SD Card is identicle only 3.3V and not 5V.
Cheers
zxsteve |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Jul 04, 2011 4:52 pm |
|
|
The SD card is sending a response, this is positive.
For more details I would enable the MMC_DEBUG define in mmc_spi.c so you get more debug info on your LCD screen. Post the results. |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Thu Aug 11, 2011 5:13 am |
|
|
Success,
I have successfully ( I think ) written the the SD Card. Problem now is how to get the data off the card. I would like to write some custom VB code to read the raw data from the card. Does anyone know how to do this, or failing that a program that will enable me to read the data in?
Cheers |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Thu Aug 11, 2011 5:23 am |
|
|
Well the easiest way is to use 'hexedit' or some other PC program to read the SD drive.You're not the first one wanting to do this, so there's probably 100s of example programs out there in dozens of programming languages.
or...
you could program a 2nd PIC to read the SD drive and send the data to the PC via RS-232 or USB.
I'd do the 2nd option as it's PIC related, you've alrady cut 95% of the code and it's a bit interesting. |
|
|
sjharris
Joined: 11 May 2006 Posts: 78
|
|
Posted: Thu Aug 11, 2011 7:37 am |
|
|
I have just checked using Hexedit. The bytes appear as they should !!
But taking this one step further I have formatted the card, created a text file and found the start sector for the file. Multiply by 512 bytes (sector size = 512) and hey presto I have now edited the file in the PIC.
But unfortunately the file does not appear changed in notepad on the PC (i.e. the bytes do not appear) but they do appear in hexedit. What else do I need to change in the file on the SD card to get the file to show the changes in Notepad?
This is OK:-
Code: |
Original file contents:- 12345678
PIC writes:- Hello123
New file contents:- Hello123
|
But fails on this:-
Code: |
Original file contents:- 123
PIC writes:- Hello123
New file contents:- Hel
|
Where are the rest of the bytes not displayed? (PS They do show up in Hexedit)
Cheers |
|
|
|