kideltn
Joined: 08 Jun 2010 Posts: 13
|
Need helping about unstable phenomenon when comm SDcard |
Posted: Thu Jul 08, 2010 7:35 am |
|
|
I'm trying debug to confirm the hardware communication SD card and 18f4550 is true.
I used voltage source 3,3 V for both PIC and SD card (I tested blinking led with above fabric.The circuit worked)
Then I carried out installation SD connected 18F4550.
A portion my schematic :
I directly connected the pins SPI together !
My code (I used CCS):
Code: |
#include "basicsd.h"
#define CS PIN_B2
#define SCK PIN_B1
#define SDO PIN_C7
#define SDI PIN_B0
#use spi( DI=SDI, DO=SDO, CLK=SCK, BITS=8,MODE=3 , stream=sspi)
#define _DEBUG
#ifdef _DEBUG
#define dbg(str) printf(str)
#endif
// Prototypes
void command(int8, int16, int16, int8);
int mmc_init();
int i;
// Implementations
// Send 6-byte Command to MMC
void command(int8 cmd, int16 adh, int16 adl, int8 crc)
{
spi_xfer(sspi,cmd);
spi_xfer(sspi,make8(adh,1));
spi_xfer(sspi,make8(adh,0));
spi_xfer(sspi,make8(adl,1));
spi_xfer(sspi,make8(adh,0));
spi_xfer(sspi,crc);
}
// Initialize the Card
int mmc_init()
{
int16 i;
// Unselect Card and Send some Dummy Clocks
output_high(CS);
for(i= 0; i< 10; i++)
{
spi_xfer(sspi,0xEF);
}
// Select Card
output_low(CS);
// Send Command 0x40: Wake Up the Card
command(0x40, 0, 0, 0x95);
for(i= 0; i< 100; i++)
{
if(spi_xfer()== 0x01)
{
break;
}
}
if(i>= 100) // Check for Time-out Error
{
return(1);
}
dbg("CMD40 OK (R1= 0x01)\n\r");
// Send Command 0x41: Start Initialize
command(0x41, 0, 0, 0xFF);
for(i= 0; i< 1000; i++)
{
if(spi_xfer()== 0x00)
{
break;
}
}
if(i>= 1000) // Check for Time-out Error
{
return(1);
}
dbg("CMD41 OK (R1= 0x00)\n\r");
// Return OK
return 0;
}
void main()
{
//TRISD =0b00100010;
printf("> MMC Test by DEBUGGER...\n\r");
printf("> Start MMC Init...\n\r");
/*while(1)
{
for(i=0;i<5;i++)
{
printf("test");
delay_ms(200);
}
i=fgetc(GPS);
if(i=='c') printf("\ni =10");
}*/
if(mmc_init()== 0)
{
printf("> MMC Init Ok...\r\n");
}
else
{
printf("> Continue Debug ...!\r\n");
}
while(1);
} |
The above code well work Proteus simulation !
When I run on real circuit, the circuit was unstable.
At power up, result can be fail or success, very unstable !
I have spent many time to debug but still fail ! (Hic, it is sad,i have failed many times when comm it...)
Thanks for reading and hope get helping ! |
|