MMC Crad Problem

General discussion on mikroC.
Post Reply
Author
Message
jony john
Posts: 6
Joined: 29 Apr 2010 09:30

MMC Crad Problem

#1 Post by jony john » 08 Jun 2010 06:08

hi all,,i'm new in PIC..
i have make SD/MMC Card circuit and download the ex. program from extra example easypic4..
i have try in my PC but nothing happen..
can anyone explain because i don't understands what should happen when the program is running..please..

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: MMC Crad Problem

#2 Post by Sobrietytest » 08 Jun 2010 11:05

Can you post the code you're using along with the complete circuit diagram?

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: MMC Crad Problem

#3 Post by tihomir.losic » 08 Jun 2010 16:43

Hello,

first of all, I want to suggest you to download and install new version of mikroC PRO for PIC:
http://www.mikroe.com/eng/downloads/get ... _setup.zip
This development enviorment has wide-range set of features such as:
- Easy-To-Use IDE, very compact and efficient code, wealth of hardware and software libraries,
comprehensive documentation, software simulator, hardware debugger support,
COFF file generation and many more. Numerous ready-made examples that are well explained
will give you good start for your embedded project.

Secondly, your issue...
- Please make sure that MMC card is properly formatted (to FAT16 or just FAT) before testing it on this example!
- The MMC uses hardware SPI for communication, so make sure that it gets properly connected to the port where SPI module is located (PORTC for most PICs)!
- This library is for PIC18 MCUs only!
- This example expects MMC card to be inserted before reset, otherwise, the FAT_ERROR message is displayed.

Best regards,

Losic Tihomir
mikroElektronika [Support team]

jony john
Posts: 6
Joined: 29 Apr 2010 09:30

Re: MMC Crad Problem

#4 Post by jony john » 10 Jun 2010 03:04

Image

Uploaded with ImageShack.us

my code:
#define RS232_debug 1
unsigned int px, k;
unsigned char data[512];
unsigned char data_for_registers[16];

unsigned char received_character;
unsigned long sector_address;
unsigned char first_byte, second_byte, third_byte, fourth_byte;
unsigned char serial_buffer[2];
unsigned char serial_pointer;

void printhex(unsigned char i) {
unsigned char hi,lo;
hi = i & 0xF0;
hi = hi >> 4;
hi = hi + '0';
if (hi>'9') hi = hi + 7;
lo = (i & 0x0F) + '0';
if (lo>'9') lo=lo+7;
USART_Write(hi);
USART_Write(lo);

}

void main()
{
unsigned int i;

PORTC = 0;
#ifdef RS232_debug
USART_Init(19200);
#endif

Delay_ms(10);

TRISC = 0b11010011;

#ifdef RS232_debug
USART_Write('P');
USART_Write('I');
USART_Write('C');
USART_Write('-');
USART_Write('S');
USART_Write('t');
USART_Write('a');
USART_Write('r');
USART_Write('t');
USART_Write('e');
USART_Write('d');
USART_Write(13);
USART_Write(10);
#endif


Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
i = MMC_Init(&PORTC,2);
#ifdef RS232_debug
if(i == 0)
{
USART_Write('M');
USART_Write('M');
USART_Write('C');
USART_Write('I');
USART_Write('n');
USART_Write('i');
USART_Write('t');
USART_Write('-');
USART_Write('O');
USART_Write('K');
USART_Write(13);
USART_Write(10);
}
if(i)
{
USART_Write('M');
USART_Write('M');
USART_Write('C');
USART_Write('I');
USART_Write('n');
USART_Write('i');
USART_Write('t');
USART_Write('-');
USART_Write('E');
USART_Write('r');
USART_Write('r');
USART_Write(13);
USART_Write(10);
}
#endif

for(i=0; i<=511; i++) data = 'E';
i = MMC_Write_Sector(55, data);

#ifdef RS232_debug
if(i == 0)
{
Usart_Write('W');
Usart_Write('r');
Usart_Write('i');
Usart_Write('t');
Usart_Write('e');
Usart_Write('-');
Usart_Write('O');
Usart_Write('K');
}
else
{
Usart_Write('W');
Usart_Write('r');
Usart_Write('i');
Usart_Write('t');
Usart_Write('e');
Usart_Write('-');
Usart_Write('E');
Usart_Write('r');
Usart_Write('r');
}
USART_Write(13);
USART_Write(10);
#endif

#ifdef RS232_debug
i = MMC_Read_CID(data_for_registers);
if(i == 0)
{
for(k=0; k<=15; k++)
{
printhex(data_for_registers[k]);
if(k!=15) USART_Write('-');
}
USART_Write(13);
}
else
{
Usart_Write('C');
Usart_Write('I');
Usart_Write('D');
Usart_Write('-');
Usart_Write('E');
Usart_Write('r');
Usart_Write('r');
}
i == MMC_Read_CSD(data_for_registers);
if(i == 0)
{
for(k=0; k<=15; k++)
{
printhex(data_for_registers[k]);
if(k!=15) USART_Write('-');
}
USART_Write(13);
USART_Write(10);
}
else
{
Usart_Write('C');
Usart_Write('S');
Usart_Write('D');
Usart_Write('-');
Usart_Write('E');
Usart_Write('r');
Usart_Write('r');
}
#endif

serial_pointer = 0;

// MAIN loop
while(1)
{
if (USART_Data_Ready())
{
serial_buffer[serial_pointer] = USART_Read();
serial_pointer++;
if(serial_pointer>1)
{
serial_pointer = 0;
if(serial_buffer[0] == 'S') first_byte = serial_buffer[1];
if(serial_buffer[0] == 's') second_byte = serial_buffer[1];
if(serial_buffer[0] == 'E') third_byte = serial_buffer[1];
if(serial_buffer[0] == 'e') fourth_byte = serial_buffer[1];
if(serial_buffer[0] == 'R') // Command: Read memmory
{
if(serial_buffer[1] == 'r')
{
sector_address = ((long)first_byte << 24) + ((long)second_byte << 16) +
((long)third_byte << 8 ) + ((long)fourth_byte);
i = MMC_Read_Sector(sector_address,data);
//USART_Write(0x30 + i); //
if(i == 0)
{
for(k=0; k<512; k++)
{ //USART_Write(data[k]);
printhex(data[k]);
USART_Write(' ');
if(((k+1) % 16)==0)
{
USART_Write(' ');
//printhex(k);
for(px=(k-15); px<=k; px++)
{
if((data[px]>33) && (data[px]<126))
{
USART_Write(data[px]);
}
else
{
USART_Write('.');
}
}
USART_Write(13);
}
}
USART_Write(13);
USART_Write(10);
}
else
{
USART_Write('R');
USART_Write('d');
USART_Write('-');
USART_Write('E');
USART_Write('r');
USART_Write('r');
USART_Write(13);
USART_Write(10);
}
}
}
if(serial_buffer[0] == 'W')
{
if(serial_buffer[1] == 'w')
{
sector_address = ((long)first_byte << 24) + ((long)second_byte << 16) +
((long)third_byte << 8 ) + ((long)fourth_byte);
for(k=0; k<512; k++) data[k] = received_character;
i = MMC_Write_Sector(sector_address, data);
if(i != 0)
{
USART_Write('W');
USART_Write('r');
USART_Write('-');
USART_Write('E');
USART_Write('r');
USART_Write('r');
USART_Write(13);
USART_Write(10);
}
else
{
USART_Write('W');
USART_Write('r');
USART_Write('-');
USART_Write('O');
USART_Write('K');
USART_Write(13);
USART_Write(10);
}
}
}
if(serial_buffer[0] == 'C')
{
received_character = serial_buffer[1];
}
}
}
}
}

jony john
Posts: 6
Joined: 29 Apr 2010 09:30

Re: MMC Crad Problem

#5 Post by jony john » 10 Jun 2010 04:16

for this project i use :
PIC18F452 (IN MIKRO C PRO EXAMPLE,IT USE PIC18F4520)
Example program i use MMC_Test.c

if i change the setting from PIC18F452 to PIC18F452, then i compile using example, it will show error..
CMCON |= 7; // Turn off comparators
so, i disable this command and it can compile with PIC18F452.

8Mhz
512MB MMC Apacer
FAT (NOT FAT16)
compiler mikro C PRO (before this i use mikroC)

For MMC holder i buy at farnell
http://my.farnell.com/multicomp/sdbmf-0 ... tt=9186212

Result in MMC Card Terminal (in mikroC):
PIC_Started
MMC_Init Err
Write_Err

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

Re: MMC Crad Problem

#6 Post by Sobrietytest » 10 Jun 2010 11:25

It's going to take a bit of time to fathom out your code but I have a couple of initial observations...

1. You are initialising the SPI with Fosc/16 = 500kHz, this is too fast for the MMC initialisation which should be <420kHz. Try Fosc/64, initialise the MMC and then put in a small delay (e.g. Delay_ms(20)) before you test the result. If i then equals zero you can then reinitialise the SPI at a faster speed. If it still doesn't work you need to recheck your hardware or try a different card, some cards simply don't work with SPI and you may have to try a different brand.

2. Have a look at the following piece of code...

for(i=0; i<=511; i++) data = 'E';
i = MMC_Write_Sector(55, data);


Use different variables for the loop counter and the MMC write return result, using the same variable will only lead to problems. Remember that the read/write functions will never work unless the MMC_Init has been successful.

Now that you're using MikroC PRO have a look at UART1_Write_Text, this will make your program significantly shorter and much easier to read!

Post Reply

Return to “mikroC General”