Toll-Free Customer Support 24/7

Micro SD Memory Card interface for 3.3V MCU

/*
* Project name:
Micro SD Memory Card interface for 3.3V MCU
* Copyright
(c) Researchdesignlab.com
* Test configuration:
MCU: PIC16F877A
Dev.Board: PIC
Oscillator: 20.0 MHz
Software: mikroC PRO for PIC v 4.6

*/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB6_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


// MMC module connections
sbit Mmc_Chip_Select at LATA5_bit; // for writing to output pin always use latch (PIC18 family)
sbit Mmc_Chip_Select_Direction at TRISA5_bit;


void main() {

unsigned int error;
unsigned long size; // stores size of file
char character, character1,character2,character3,character4;

Delay_ms(1000);
TRISD.F0 = 0; // simply LED
PORTD.F0 = 0;
TRISC.F3 = 0; // CLOCK OUTPUT
TRISC.F5 = 0; // DATA OUT
TRISA.F5 = 0; // SLAVE SELECTION PIN
TRISC.F4 = 1; // DATA IN
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64,_SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW,_SPI_LOW_2_HIGH);
//PORTA.F5 = 1; // SLAVE DISABLED INTITIALLY

error = Mmc_Fat_Init();
// use fat16 quick format instead of init routane if a formatting is needed
while (error != 0){ // keep initializing MMC FAT until it is done
error = Mmc_Fat_Init();
}

SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // once initialized
// increase the speed

error = Mmc_Fat_Assign("DATALOG1.TXT",0xA0); // assign or create a new file
while (error != 1) // keep trying until the file is created
{
error = Mmc_Fat_Assign("DATALOG1.TXT",0xA0);
}

 

Mmc_Fat_Rewrite(); // open the last assigned file for writing purpose
Mmc_Fat_Write("ABCD",4); // write the actual data and number of bytes to be written


Mmc_Fat_Reset(size); // opens the last assigned file for reading, the the size of the filed is stored
// in the variable

Mmc_Fat_Read(&character); // &character points to the first character in the data written in the file, and
character1 = character; // the data character is stored in the variable, therefore, here, character1 = A

Mmc_Fat_Read(&character); // once a data character is read, the next read command points to the next data character and
character2 = character; // and the value is again stored in the variable 'character', which is reassigned to character2
Mmc_Fat_Read(&character);
character3 = character;
Mmc_Fat_Read(&character);
character4 = character;

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display


Lcd_Cmd(_LCD_CLEAR);
Lcd_Chr_Cp(character1);
Lcd_Chr_Cp(character2);
Lcd_Chr_Cp(character3);
Lcd_Chr_Cp(character4);

}

Back to top