CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

MMC Card

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
exodia505



Joined: 07 Feb 2012
Posts: 34

View user's profile Send private message

MMC Card
PostPosted: Thu Feb 16, 2012 3:03 pm     Reply with quote

Hello!!
I want to use MMC for simple application , who can give me an example?
Is MMC driver available in ccs ?
I found this code will be helpful?

For the original source, and hundreds more examples of PIC C code, see:
http://www.microchipc.com/sourcecode/#mmc
Code:

int mmc_init();
int mmc_response(unsigned char response);
int mmc_read_block(unsigned long block_number);
int mmc_write_block(unsigned long block_number);
int mmc_get_status();

/************************** MMC Init **************************************/
/* Initialises the MMC into SPI mode and sets block size, returns 0 on success */

int mmc_init()
{
int i;

SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_4 | SPI_SS_DISABLED);

*0x94 |= 0x40;                          // set CKE = 1 - clock idle low
*0x14 &= 0xEF;                          // set CKP = 0 - data valid on rising edge

OUTPUT_HIGH(PIN_C2);                    // set SS = 1 (off)

for(i=0;i<10;i++)                       // initialise the MMC card into SPI mode by sending clks on
{
        SPI_WRITE(0xFF);
}

OUTPUT_LOW(PIN_C2);                     // set SS = 0 (on) tells card to go to spi mode when it receives reset

SPI_WRITE(0x40);                        // send reset command
SPI_WRITE(0x00);                        // all the arguments are 0x00 for the reset command
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x95);                        // precalculated checksum as we are still in MMC mode

puts("Sent go to SPI\n\r");

if(mmc_response(0x01)==1) return 1;     // if = 1 then there was a timeout waiting for 0x01 from the mmc

puts("Got response from MMC\n\r");

i = 0;

while((i < 255) && (mmc_response(0x00)==1))     // must keep sending command if response
{
        SPI_WRITE(0x41);                // send mmc command one to bring out of idle state
        SPI_WRITE(0x00);                // all the arguments are 0x00 for command one
        SPI_WRITE(0x00);
        SPI_WRITE(0x00);
        SPI_WRITE(0x00);
        SPI_WRITE(0xFF);                // checksum is no longer required but we always send 0xFF
        i++;
}
if(i >= 254) return 1;                   // if >= 254 then there was a timeout waiting for 0x00 from the mmc

puts("Got out of idle response from MMC\n\r");

OUTPUT_HIGH(PIN_C2);                    // set SS = 1 (off)

SPI_WRITE(0xFF);                        // extra clocks to allow mmc to finish off what it is doing

OUTPUT_LOW(PIN_C2);                     // set SS = 0 (on)

        SPI_WRITE(0x50);                // send mmc command one to bring out of idle state
        SPI_WRITE(0x00);
        SPI_WRITE(0x00);
        SPI_WRITE(0x02);                // high block length bits - 512 bytes
        SPI_WRITE(0x00);                // low block length bits
        SPI_WRITE(0xFF);                // checksum is no longer required but we always send 0xFF

if((mmc_response(0x00))==1) return 1;
OUTPUT_HIGH(PIN_C2);            // set SS = 1 (off)
puts("Got set block length response from MMC\n\r");
return 0;
}

/************************** MMC Get Status **************************************/
/* Get the status register of the MMC, for debugging purposes */

int mmc_get_status()
{

OUTPUT_LOW(PIN_C2);                     // set SS = 0 (on)

        SPI_WRITE(0x58);                // send mmc command one to bring out of idle state
        SPI_WRITE(0x00);
        SPI_WRITE(0x00);
        SPI_WRITE(0x00);                //
        SPI_WRITE(0x00);                // always zero as mulitples of 512
        SPI_WRITE(0xFF);                // checksum is no longer required but we always send 0xFF

OUTPUT_HIGH(PIN_C2);                    // set SS = 1 (off)
return 0;
}

/************************** MMC Write Block **************************************/

int mmc_write_block(unsigned long block_number)
{
unsigned long i;
unsigned long varh,varl;

varl=((block_number&0x003F)<<9);
varh=((block_number&0xFFC0)>>7);

puts("Write block\n\r");                // block size has been set in mmc_init()

OUTPUT_LOW(PIN_C2);                     // set SS = 0 (on)

        SPI_WRITE(0x58);                // send mmc write block
        SPI_WRITE(HIGH(varh));
        SPI_WRITE(LOW(varh));
        SPI_WRITE(HIGH(varl));
        SPI_WRITE(0x00);                // always zero as mulitples of 512
        SPI_WRITE(0xFF);                // checksum is no longer required but we always send 0xFF

if((mmc_response(0x00))==1) return 1;
puts("Got response to write block\n\r");

SPI_WRITE(0xFE);                        // send data token

for(i=0;i<512;i++)
{

SPI_WRITE(i2c_eeprom_read(HIGH(i),LOW(i)));     // send data

}

SPI_WRITE(0xFF);                        // dummy CRC
SPI_WRITE(0xFF);

if((SPI_READ(0xFF)&0x0F)!=0x05) return 1;

puts("Got data response to write block\n\r");

OUTPUT_HIGH(PIN_C2);                    // set SS = 1 (off)
return 0;
}



/************************** MMC Read Block **************************************/
/**** Reads a 512 Byte block from the MMC and outputs each byte to RS232 ****/

int mmc_read_block(unsigned long block_number)
{
unsigned long i;
unsigned long varh,varl;

varl=((block_number&0x003F)<<9);
varh=((block_number&0xFFC0)>>7);

OUTPUT_LOW(PIN_C2);                     // set SS = 0 (on)

        SPI_WRITE(0x51);                // send mmc read single block command
        SPI_WRITE(HIGH(varh));      // arguments are address
        SPI_WRITE(LOW(varh));
        SPI_WRITE(HIGH(varl));
        SPI_WRITE(0x00);
        SPI_WRITE(0xFF);                // checksum is no longer required but we always send 0xFF

if((mmc_response(0x00))==1) return 1;   // if mmc_response returns 1 then we failed to get a 0x00 response (affirmative)

puts("Got response to read block command\n\r");

if((mmc_response(0xFE))==1) return 1;   // wait for data token

puts("Got data token\n\r");

        for(i=0;i<512;i++)
        {
                putc(SPI_READ(0xFF));               // we should now receive 512 bytes
        }

SPI_READ(0xFF);                 // CRC bytes that are not needed
SPI_READ(0xFF);

OUTPUT_HIGH(PIN_C2);            // set SS = 1 (off)
SPI_WRITE(0xFF);                // give mmc the clocks it needs to finish off

puts("\n\rEnd of read block\n\r");

return 0;
}

/************************** MMC get response **************************************/
/**** Repeatedly reads the MMC until we get the response we want or timeout ****/

int mmc_response(unsigned char response)
{
        unsigned long count = 0xFFFF;           // 16bit repeat, it may be possible to shrink this to 8 bit but there is not much point

        while(SPI_READ(0xFF) != response && --count > 0);

        if(count==0) return 1;                  // loop was exited due to timeout
        else return 0;                          // loop was exited before timeout
}

Thanks
_________________
NOway!!
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 16, 2012 3:34 pm     Reply with quote

First you MUST get the hardware correct.
Post your PIC information processor type.

It MUST be an L version(eg. 16LF887( if you want to connect direct to the MMC card

If you try to use an F version (eg. 16F887) you WILL destroy the MMC card...

Code is simple, but you MUST use the correct hardware.
exodia505



Joined: 07 Feb 2012
Posts: 34

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 5:26 am     Reply with quote

first of all , thanks
i want to use pic 16f877 , will i find difficulties? you mean needs 3.3 volts ?

i think this circuit will work (ofc its 16f873a) just need to change it to 877


what about code above ?is it good to use it or there is another one better ?[/img]
_________________
NOway!!
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 6:48 am     Reply with quote

That circuit will never work !!

Really, read the data sheets and see what are the required logic levels for the 16F877 PIC.

That PIC is 5 volt device, ALL MMC cards are 3 volts.

Hint. look at the charts/tables for min Vih for the PIC then ask if you can get that out of a 3.3volt MMC card.

if maxVohMMC < minVihPIC then 'can't be done'.

Simple math,no it's actually very simple arithmetic..
exodia505



Joined: 07 Feb 2012
Posts: 34

View user's profile Send private message

PostPosted: Fri Feb 17, 2012 10:35 am     Reply with quote

ok ok ok , let's back to software(code)
can you post a simple example of writting data (read from adc ) in MMC
using this function (mmc_write_block(address, size,*ptr);
!!!!!!!!!!!!!!!!!!
_________________
NOway!!
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Feb 17, 2012 1:11 pm     Reply with quote

You are wasting your time using this processor.

Did you read the Kludge at the link you posted about using an EEPROM to buffer the writes for this processor?

Look for a pin compatible processor offering more RAM such as the PIC18F4620 to avoid this need for this messy and very very very slow KLUDGE.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!


Last edited by asmallri on Sat Feb 18, 2012 3:41 am; edited 1 time in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

Re: MMC Card
PostPosted: Fri Feb 17, 2012 3:18 pm     Reply with quote

exodia505 wrote:
I want to use MMC for simple application , who can give me an example?
Is MMC driver available in ccs ?
RTFM.
Page 341, "Example programs" is a description of all the example programs that come with the CCS compiler. It even includes an mmc example.

Also have a look at the Code Library section of this forum, you will find two drivers there.

Quote:
I found this code will be helpful?

For the original source, and hundreds more examples of PIC C code, see:
http://www.microchipc.com/sourcecode/#mmc
This website is full of examples for the High Tech compiler. This is a bad starting point as it requires knowledge of both the High Tech and CCS compiler to convert the programs. We are not going to do it for you, especially when CCS programs are ready available.

Quote:
ok ok ok , let's back to software(code)
Without working hardware you will never be able to test the software.

One more important note on the hardware: Please, make it easier to yourself by using a PIC18 processor instead of PIC16. Writing to the MMC happens in blocks of 512 bytes and most PIC16's don't have this much RAM. The more RAM the better. Workarounds are possible for some situations, but difficult and not recommended for you as a beginner.
exodia505



Joined: 07 Feb 2012
Posts: 34

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:18 am     Reply with quote

Thanks !!
i'm working with pic 18f452 , it works well
_________________
NOway!!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Feb 18, 2012 8:51 am     Reply with quote

The PIC18F452 doesn't run on 3V !!!
The PIC18LF452 does.

Save yourself a lot of trouble and have your whole circuit run on the same voltage as the MMC card.

Besides that, the PIC18F452 is old and not recommended for new designs. Many newer PIC18 processors exist that will run directly on 3V and are cheaper than the PIC18F452.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group