Easy PIC6+Micro SD

General discussion on mikroC.
Post Reply
Author
Message
Taroot
Posts: 14
Joined: 27 Nov 2010 09:08

Easy PIC6+Micro SD

#1 Post by Taroot » 22 Jan 2011 16:20

Hello.
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);
    }

}
Easy Pic6,Serial Ethernet Board, EasyBee3 Board, microSD Card Board,
RTC Board, Digital Potentiometer Board, Distance Meter Board,
Light to Frequency 2 Board, Motion Sensor Board

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Easy PIC6+Micro SD

#2 Post by slavisa.zlatanovic » 24 Jan 2011 10:59

Hi!

Did you try the MMC examples distributed with the mikroC PRO for PIC compiler?
...\Mikroelektronika\mikroC PRO for PIC\Examples\Extra Boards\MMC\
If you have problems running these examples, please, send me photos of your development board and microSD Card board.
I'll then inspect your connections and hardware settings.
slavisa.zlatanovic@mikroe.com
Best regards
Slavisa

Taroot
Posts: 14
Joined: 27 Nov 2010 09:08

Re: Easy PIC6+Micro SD

#3 Post by Taroot » 25 Jan 2011 08:11

i tried to run mmc example.but same problem.
As you said i mail you pictures of my hardware. Could be MicroSD Board hardware problem. How can i check the Board. Any suggestion.
Easy Pic6,Serial Ethernet Board, EasyBee3 Board, microSD Card Board,
RTC Board, Digital Potentiometer Board, Distance Meter Board,
Light to Frequency 2 Board, Motion Sensor Board

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Easy PIC6+Micro SD

#4 Post by slavisa.zlatanovic » 26 Jan 2011 11:27

Hi!

Just to confirm that I've received your email. We'll continue this interaction via email.
Best regards
Slavisa

Post Reply

Return to “mikroC General”