Rickooo
Member level 3
Hi, I want to use SD Card. I am using MPLAB X IDE and XC8 compiler. I am using PIC18F4520 and 16MHz crystal. I am following https://openlabpro.com/guide/raw-sd-readwrite-using-pic-18f4550/ article to initialize the SD Card.
Code:
When I run this code and look on terminal, I observed that I didnt receive the 0x01 reponse and hence SD is not initialize. I am usind SD module. 4GB Sd card. Volt level:
SCK: 0.1V
MOSI: 3.9-4.1V
CLK: 0.09 - 0.1V
MISO: 3.9V - 4.1V
What do you think, whats the problem? Thanks
Code:
Code:
void dummy_clock(int n){
LATCbits.LATC2 = 1; // set CS high
// send 80 clocks (10 bytes) to wake up SD card
// load dummy values into buffer
for(int i = 0; i < n; i++){
SDdata[i] = 0xFF;
}
spi_send(SDdata, n);
// set CS low
LATCbits.LATC2 = 0;
}
//send bus clocks at instances where the buffer of the microcontroller is empty during the execution time.
void proceed(){
LATCbits.LATC2 = 1; // set CS high
spi_single_send(0xFF);
LATCbits.LATC2 = 0;
}
unsigned char response()
{
LATCbits.LATC2 = 0; //CS low
buff = spi_single_receive();
return buff;
}
void SDcard_init(void){
// send debug message
LATCbits.LATC5 = 1;
UART_Write_Text("Starting SD card initialization...\n");
// dummy clock
do{
dummy_clock(10);
UART_Write_Text("Sending CMD0");
// transmit command 0
SDcommand[0] = 0x40; // start bit | host bit | command
SDcommand[1] = 0x00; // no arguments
SDcommand[2] = 0x00;
SDcommand[3] = 0x00;
SDcommand[4] = 0x00;
SDcommand[5] = 0x95; // pre-calculated CRC
spi_send(SDcommand, 6);
UART_Write_Text("Sent CMD0 Command");
proceed();
do{
buff = response();
count++;
}while((buff!=0X01) && (count<10) );
count = 0;
}while(buff!=0X01);
UART_Write_Text("CMD0 success!\n"); //Now Card enter in SPI mode
When I run this code and look on terminal, I observed that I didnt receive the 0x01 reponse and hence SD is not initialize. I am usind SD module. 4GB Sd card. Volt level:
SCK: 0.1V
MOSI: 3.9-4.1V
CLK: 0.09 - 0.1V
MISO: 3.9V - 4.1V
What do you think, whats the problem? Thanks