Hot to use PIC18f47k42 with an SD Card

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
Patrick46
Posts: 2
Joined: 21 Aug 2019 15:24

Hot to use PIC18f47k42 with an SD Card

#1 Post by Patrick46 » 21 Aug 2019 15:34

Hi to all,

I want to write and read an SD Card connected to the PIC18F47K42.
I have read the datasheets and now I know that this PIC uses the Peripheral pin select so I worte this code:

Code: Select all

// MMC module connections
sfr sbit Mmc_Chip_Select at RD0_bit;
sfr sbit Mmc_Chip_Select_Direction at TRISD0_bit;

void main() {
   TRISC3_bit = 0; // Output
   TRISC5_bit = 0; // Output
   TRISD0_bit = 0; // Output
   TRISC4_bit = 1; // Input
   
   Unlock_IOLOCK();
   // Output
   RC3PPS = 0b011110; // SCK
   RC5PPS = 0b011111; // SDO
   RD0PPS = 0b100000; // SS
   // Input
   SPI1SDIPPS = 0b10100;
   Lock_IOLOCK();

   SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
   if (Mmc_Fat_Init() == 0) {
      // reinitialize spi at higher speed
      SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
   }
}
My PIC is connected to the SD Card using this pin:
PIN 23 SDI
PIN 18 SCK
PIN 24 SDO
PIN 19 CS

This code doesn't work and it blocks when SPI1_Init_Advanced is called.
Some idea?

Thank you.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Hot to use PIC18f47k42 with an SD Card

#2 Post by stefan.filipovic » 22 Aug 2019 11:22

Hi,

Welcome to MikroE forum.

According to the MCU datasheet:
The ANSELx bits default to the Analog mode after Reset. To use any pins as digital general-purpose or peripheral inputs, the corresponding ANSEL bits must be initialized to ‘0’ by user software.

Have you tried to map pins using our PPS Library, as below?

Code: Select all

ANSELC = 0;

Unlock_IOLOCK();
  
PPS_Mapping_NoLock(_RC4, _INPUT, _SDI1);   // Sets pin RC4 to be Input, and maps SDI1 Input to it
PPS_Mapping_NoLock(_RC3, _OUTPUT, _SCK1);  // Sets pin RC3 to be Output, and maps SCK1 Output to it
PPS_Mapping_NoLock(_RC5, _OUTPUT, _SDO1);  // Sets pin RC5 to be Output, and maps SDO1 Output to it

Lock_IOLOCK();

    //Configures and initializes SPI Remappable module.
SPI1_Remappable_Init_Advanced(_SPI_REMAPPABLE_MASTER_OSC_DIV64, _SPI_REMAPPABLE_DATA_SAMPLE_MIDDLE, _SPI_REMAPPABLE_CLK_IDLE_LOW, _SPI_REMAPPABLE_LOW_2_HIGH);
Delay_ms(500);
  // use fat16 quick format instead of init routine if a formatting is needed
if (Mmc_Fat_Init() == 0) {
    // reinitialize spi at higher speed
    SPI1_Remappable_Init_Advanced(_SPI_REMAPPABLE_MASTER_OSC_DIV16, _SPI_REMAPPABLE_DATA_SAMPLE_MIDDLE, _SPI_REMAPPABLE_CLK_IDLE_LOW, _SPI_REMAPPABLE_LOW_2_HIGH);
}
Kind regards,
Stefan Filipović

Patrick46
Posts: 2
Joined: 21 Aug 2019 15:24

Re: Hot to use PIC18f47k42 with an SD Card

#3 Post by Patrick46 » 22 Aug 2019 16:12

Hi,

thank you for the answer.
I tried how you suggested and now it works.

Thank you.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Hot to use PIC18f47k42 with an SD Card

#4 Post by stefan.filipovic » 23 Aug 2019 09:28

Hi,

You're welcome.

Kind regards,
Stefan Filipović

Post Reply

Return to “PIC PRO Compilers”