I need help. I'm trying to communicate with the PIC18F4520 with SD card.
I am
My problem is in their initial cards, functions Mmc_Init () and Mmc_Fat_Init () does not work.
I use the MicroSD card of microElektronika with EasyPIC-6.
I really do not know what is the problem , if anyone has any ideas I would be thankful.
Here is my code
Code: Select all
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_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 TRISB0_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 LATC0_bit;
sbit Mmc_Chip_Select_Direction at TRISC0_bit;
// eof MMC module connections
unsigned int year;
unsigned short month, day, hours, mins, seconds;
char fileContents[15] = {'S','D','F','A','T','1','6','t','e','s','t','i','n','g','\n'};
unsigned short initSD() {
unsigned char s1, s[3];
while(s1){
s1 = Mmc_Init();
ByteToHex(s1, s);
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,s); Delay_ms(1000);
}
s1 = Mmc_Fat_Init();
ByteToHex(s1, s);
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,s); Delay_ms(1000);
if(s1 == 0xFF) {
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"SD not detected "); Delay_ms(1000);
return 1;
}
if(s1 == 0x01) {
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"FAT16 error"); Delay_ms(1000);
return 2;
}
if(s1 == 0x00) {
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"init complt"); Delay_ms(1000);
return 0;
}
}
void createNewFile(char *filename) {
Mmc_Fat_Set_File_Date(year,month,day,hours,mins,seconds); // Set file date & time info
Mmc_Fat_Assign(filename, 0xA0); // Find existing file or create a new one
Mmc_Fat_Rewrite(); // To clear file and start with new data
Mmc_Fat_Write(fileContents, 14); // write data to the assigned file
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"file Created"); Delay_ms(1000);
}
void main() {
char filename[14] = "Test0001.TXT";
short sd = 0;
year = 2010;
month = 10;
day = 27;
hours = 15;
mins = 10;
seconds = 10;
ADCON1 = 0x0F; // Set AN pins to Digital I/O
CMCON = 0X07; // Disable comparators
TRISE = 0xFF; // Configure PORTC as input
PORTE = 0x00; // Initial PORTC value
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"start"); Delay_ms(1000);
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
Delay_ms(500);
while(1) {
sd = initSD();
if(sd == 0) {
break;
}
Delay_ms(200);
}
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV16, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // reinitialize spi at higher speed
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"test start"); Delay_ms(1000);
createNewFile(filename);
Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,"test completed"); Delay_ms(1000);
}
}