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.

[PIC] Problem with SD card initialization with PIC18F452 using mikroC

Status
Not open for further replies.

ultrasonic.1991

Member level 3
Joined
Nov 6, 2015
Messages
60
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
554
I'm trying to write a text file to an SD card with a PIC18F452 using mikroC. I'm using a mikroC library named Mmc_FAT16 and I formatted the SD card to FAT, but I can't initialize the SD card.

I tried reading the error function and it returns 0xFF which means MMC/SD card was not detected

My code is as follows:
C:
void usart_sendstring (char *s);
void usart_sendchar(char chr);
void usart_init(void);
sbit Mmc_Chip_Select at LATB4_bit;
sbit Mmc_Chip_Select_Direction at TRISB4_bit;

unsigned long size;
char character[10];
unsigned char i;
char err;
short fhandle;

void main()
{
    usart_init();
    usart_sendstring("start");
    SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
    err=Mmc_Fat_Init();

    if (err == 0)
    {
       usart_sendstring("sd card initialized successfully");
    }
    else if (err != 0)
    {
       usart_sendstring("error in initializing sd card");
       usart_sendchar(err);
    }

    Mmc_Fat_Assign("MIKRO007.TXT", 0xA0);
    Mmc_Fat_Append();
    fhandle = Mmc_Fat_Open("MIKRO007.TXT.TXT", FILE_WRITE, 0x01);

    Mmc_Fat_Write("Hello world", 255);
    Mmc_Fat_Reset(&size);
        
    for (i=0; i<11; i++);
    {
       Mmc_Fat_Read(&character[i]);
       usart_sendstring(character);
    }
    Mmc_Fat_Close();
 }

 void usart_init(void)
 {
    TXSTA |= (1<<5);  // Transmit Enable bit
    TXSTA |= (1<<2);  // baudrate generator to high speed
    TRISC |=(1<<7);   // congifure rx and tx
    TRISC &=~(1<<6);
    RCSTA |= (1<<7);  // serial enable bit
    RCSTA |= (1<<4);  // Enabling the reception by setting bit CREN
    SPBRG = 129;      // baudrate 9600
}

void usart_sendchar(char chr)
{
    while (!(TXSTA &(1<<1)));
    TXREG  =chr;
}

void usart_sendstring (char *s)
{
    while (*s)
    {
        usart_sendchar(*s);
        s++;
    }
}

I also tried measuring the voltage of the pins; the voltage of the SCK pin shows 0 V.

What might be the problem here?
 

The initialization sequence correponds to mikroC manual, how does the hardware look like? Presume you connected the SPI1 hardware pins and use correct SD card voltage levels?
 

The initialization sequence correponds to mikroC manual, how does the hardware look like? Presume you connected the SPI1 hardware pins and use correct SD card voltage levels?
I connected SDI of PIC to SDO of the sd card (MISO) and SDO of PIC to SDI of the sd card (MOSI) and sck to sck ( pic18f452 has only one spi) also cs is connected to b4. the supply pin of the PIC and sd card are both connected to 3.3 volts the grounds are connected together.
 

Hi
I connected SDI of PIC to SDO of the sd card (MISO) and SDO of PIC to SDI of the sd card (MOSI)
... that´s why I like the MOSI and MISO naming. It´s true for master and slave.
MOSI is MOSI and MISO is MISO. Nothing to swap. No confusion on signal names.

Klaus
 

I connected SDI of PIC to SDO of the sd card (MISO) and SDO of PIC to SDI of the sd card (MOSI) and sck to sck ( pic18f452 has only one spi) also cs is connected to b4. the supply pin of the PIC and sd card are both connected to 3.3 volts the grounds are connected together.
Sounds correct, but there are no pins named SDI or SDO at SD card. Do you mind to show a schematic or tell the SD card pin numbers?
 

Sounds correct, but there are no pins named SDI or SDO at SD card. Do you mind to show a schematic or tell the SD card pin numbers?
I used this type of adaptor
 

Attachments

  • SD-Card-Adapter-arduino-esp8266-esp32-direct-connection - Copy.jpg
    SD-Card-Adapter-arduino-esp8266-esp32-direct-connection - Copy.jpg
    10.9 KB · Views: 131

How are you measuring the voltages - you need a scope and not a DVM.
I can't see where you control the \CS\ line. In dislike (intensely but that is another story) MMC as it hides so much from you - in this case you really need to check the MMC generated code to make sure that it is driving the \CS\ signal correctly.
For example, you define the \CS\ line as LATB4 but I can't see where you tell the MMC code that, or even where you set the TRISB4 to '0' (the default is '1' for 1nput).
Don't forget that most Microchip SPI modules (and this is certainly true for these very old MCUs) so not drive the \CS\ (or \SS\ as they call it) line when in master mode and normal (not framing mode) operation.
Susan
 

There are some parts of your code that do not make much sense at first sight. You are assigning the file "MIKRO007.TXT" somewhere, and making access of the file"MIKRO007.TXT.TXT" elsewhere. In addition, the function Mmc_Fat_Open try to open the file in the WRITE MODE but with the atttribute READ ONLY, is it correct ? Try to print the file fhandle; this could give you an indication of the problem.
 

How are you measuring the voltages - you need a scope and not a DVM.
I can't see where you control the \CS\ line. In dislike (intensely but that is another story) MMC as it hides so much from you - in this case you really need to check the MMC generated code to make sure that it is driving the \CS\ signal correctly.
For example, you define the \CS\ line as LATB4 but I can't see where you tell the MMC code that, or even where you set the TRISB4 to '0' (the default is '1' for 1nput).
Don't forget that most Microchip SPI modules (and this is certainly true for these very old MCUs) so not drive the \CS\ (or \SS\ as they call it) line when in master mode and normal (not framing mode) operation.
Susan
I initialized the cs line and cs is controlled by the mikroc library I think. I don't have access to the functions of the mikroc library. about your last line, i think you made a typo because it is not clear what you mean. another thing that is noteworthy to mention is that the cs should be configured output as i know because we are changing it not reading it.
 

Hello,

i tested successfully "Petite Fat" and MikroC FAT32 library with this SDcard Sandisk 2Go
with a 18F26K22 and this Hardware connection :
SPI cloclk div 64 ....
No "Presence Carte" info on SDCARD => Ignored !
SDCARD preformated with PC computer with sector of 512 bytes. FAT32 in my case ...
File name must not exceed 8 chars long.

Sdcard_Pinout_PIC18.jpg
sd-card-.jpg
 
Last edited:

I initialized the cs line and cs is controlled by the mikroc library I think. I don't have access to the functions of the mikroc library. about your last line, i think you made a typo because it is not clear what you mean. another thing that is noteworthy to mention is that the cs should be configured output as i know because we are changing it not reading it.
Control of the \CS\ line is critical - if you don't get it right then you will not get your communication working. Don't just 'think' that the Mikroc library controls the \CS\ line - look at the documentation (for a start it will tell you how to tell the library which GPIO pin you are using - that is one of the things I mentioned earlier - if it controls the line at all). As each SPI slave device can specify separately how it expects the \CS\ line to be used, it is always best to control this yourself in your code.
I don't understand your comment about making the \CS] pin an output - that is EXACTLY what I said you need to so (set the TRIS bit to 0) but we could not see that in your code. The default for the TRIS bits is 'Input' so you must set it to output yourself.
And there is no typo in my previous last line. What do you think is wrong? (If you don't understand it then I suggest you go back to the data sheet and read up on the SPI module - it's called the MSSP module in those old chips.
Susa)n
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top