// MMC module connections

sbit Mmc_Chip_Select at LATC0_bit; // for writing to output pin always use latch (PIC18 family)

sbit Mmc_Chip_Select_Direction at TRISC0_bit;

// eof MMC module connections


char stringData[512] = "";

unsigned long i, size, j = 0;

char Buffer[512] = "";

char CSD[16] = "";

char CID[16] = "";


// Write string with soft uart

void writeString(char stringData[])

{

// Get string length

j = strlen(stringData);

for(i = 0; i<j; i++)

{

Soft_UART_Write(stringData[i]);

}

Soft_UART_Write(0x0D);

Soft_UART_Write(0x0A);

}


// Main. Uncomment the function(s) to test the desired operation(s)

void main() {

//#define COMPLETE_EXAMPLE // comment this line to make simpler/smaller example

ADCON1 |= 0x0F; // Configure AN pins as digital

CMCON |= 7; // Turn off comparators


// Initialize UART1 module

// UART1_Init(19200);

TRISB.B7 = 1;

TRISB.B6 = 0;

TRISC.B0 = 0;

TRISB.B0 = 1;

TRISB.B1 = 0;

TRISC.B7 = 0;

Soft_UART_Init(&PORTB, 6, 7, 19200, 0);

Delay_ms(10);


// Initialize SPI1 module

SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);

writeString("SPI started, low speed mode");

j = Mmc_Init();

if(j == 0)

{

writeString("Mmc_Init terminated successfully (0)");

}

else

{

writeString("Mmc_Init() terminated unsuccessfully (not 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);

writeString("SPI re-initialized into high speed mode. ");

j = Mmc_Read_Cid(CID);

if(j == 0)

{

writeString("Mmc_Read_Cid terminated successfully (0):");

writeString(CID);

}

else

{

writeString("Mmc_Read_Cid terminated unsuccessfully (not 0)");

}

j = Mmc_Read_Csd(CSD);

if(j == 0)

{

writeString("Mmc_Read_Csd terminated successfully (0):");

writeString(CSD);

}

else

{

writeString("Mmc_Read_Csd terminated unsuccessfully (not 0)");

}


writeString("Read sector address 5859375 to prove that it toggled upon toggling of _MCLR:");

Mmc_Read_Sector(5859375, Buffer);

writeString(Buffer);

writeString("Dumping boot sector and first file structure sector (0, 1)");

Mmc_Read_Sector(0, Buffer);

writeString(Buffer);

Mmc_Read_Sector(1, Buffer);

writeString(Buffer);



Delay_ms(100); // Make a break in hex stream

writeString("End of dumping file information sector.");


writeString("Writing message to sector...(toggle _MCLR to see message change)");

writeString("");

Mmc_Read_Sector(5859375, Buffer);

Mmc_Write_Sector(5859375, "Goodbye, world!");

if(Buffer[0] == 71)

{

Mmc_Write_Sector(5859375, "Hello, world!");

}

}