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

Test MMC on 18F won't work.

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



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

Test MMC on 18F won't work.
PostPosted: Mon Oct 24, 2005 2:39 pm     Reply with quote

Below code compiles without errors, but :

Where did I go wrong ?
In the set_tris_b or set_tris_c ?
Or in the #use fast_io(B) #use fast_io(C)
Or in the SPI configuration (20 Mhz clock and div. by 64 ) ?
Or in the #byte and #bit section ?

Or is it hardware failure ?

I receive my 3 beeps and 20x4 dispay is correct.

Thanks for any help !

This is my RS232 output : (don't receive "MMC ON - Init OK" )
Code:

 PIC Online
 Version : MMC Test
 For PIC 18F4550
Start

Write Error in end of write
Read Error

                                                                            �
                                    �


Code:

// VERSION : 0.1.07
// TEST : using the mmc in write and read mode.
// Based on http://www.ccsinfo.com/forum/viewtopic.php?t=23183

#include <18F4550.h>
#fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOCPD,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_E1, rcv=PIN_E0, ERRORS)
#include <LCD4x20D.C>

#use fast_io(B)
#use fast_io(C)

// For the PIC 16F877 see manual on page 20/234
// #byte SSPBUF = 0x13
// #byte SSPCON = 0x14
// #byte SSPSTAT = 0x94
// #bit  BF = SSPSTAT.0
// #bit SMP = SSPSTAT.7
// #bit CKE = SSPSTAT.6
// #bit CKP = SSPCON.4
// #bit SSPM1 = SSPCON.1
// #bit SSPEN = SSPCON.5
// #byte PORTC = 7
// #bit CS = PORTC.2

// For the PIC 18F4550 see manual on page 68/426
#byte SSPBUF = 0xFC9
#byte SSPCON = 0xFC6
#byte SSPSTAT = 0xFC7
#bit  BF = SSPSTAT.0
#bit SMP = SSPSTAT.7
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPM1 = SSPCON.1
#bit SSPEN = SSPCON.5
#byte PORTC = 0xF82
#bit CS = PORTC.2

// -------------------------------------------------
// This is the same as the CCS spi_read() function.
char SPI(char d)
{
SSPBUF=d;
while (!BF);
return SSPBUF;
}
// -------------------------------------------------

// -------------------------------------------------
char Command(char befF, int16 AdrH, int16 AdrL, char befH)
{
SPI(0xFF);
SPI(befF);
SPI(AdrH >> 8);
SPI(AdrH);
SPI(AdrL >> 8);
SPI(AdrL);
SPI(befH);
SPI(0xFF);
return SPI(0xFF);      // Return with the response
}
// -------------------------------------------------

// -------------------------------------------------
char MMC_Init()
{
char i;

// Init SPI
SMP=0; CKE=0; CKP=1; SSPM1=1; SSPEN=1; //SSPM0=1;
CS=1;      // MMC-Disabled

// MMC in SPI Mode -- start and Reset.
for(i=0; i < 10; i++) SPI(0xFF);             // 10*8=80 clocks
CS=0;                                             // MMC-Enabled

// CMD0
if (Command(0x40,0,0,0x95) !=1) goto Error;    // Reset

st:
// CMD1
if (Command(0x41,0,0,0xFF) !=0) goto st ;   // CMD1

return 1;

Error:
return 0;
}
// -------------------------------------------------

void main(void)
{
int16 i;

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
// -------------------------------------------------
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 );
// -------------------------------------------------
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);
setup_low_volt_detect(FALSE);
setup_oscillator(False);

// PIC 16F code
// set_tris_c(0b11010011);    // sck=rc3-0, sdo=rc5-0, CS=rc2-0.
// set_tris_b(0b00000010);

// PIC 18F code
set_tris_c(0b01111011);    // sdo=rc7(pin26)-0, CS=rc2(pin17)-0.
set_tris_b(0b00000001);    // sck=rb1(pin34)-0, sdi=rb0(pin33)-I

// Version output to RS232
printf(" PIC Online\n\r");
printf(" Version : MMC Test\n\r");
printf(" For PIC 18F4550\n\r");

//   Version output to LCD
lcd_init();
lcd_putc("\f**** PIC Online ****\n");
lcd_putc("V0.1.07 Bart De Pauw\n");
lcd_putc("MMC test READ+WRITE\n");
lcd_putc("For : PIC18F4550\n");

// beep 3 times to show circuit is ON
output_HIGH(PIN_A3);
delay_ms(100);
output_LOW(PIN_A3);
delay_ms(100);
output_HIGH(PIN_A3);
delay_ms(100);
output_LOW(PIN_A3);
delay_ms(100);
output_HIGH(PIN_A3);
delay_ms(100);
output_LOW(PIN_A3);

puts("Start\n\r");
if(MMC_Init())
   puts("MMC ON - Init OK\n\r");       // MMC Init OK

if (Command(0x58,0,512,0xFF) !=0) puts("Write error in begin of write"); // Write in 512 Byte-Mode
SPI(0xFF);
SPI(0xFF);
SPI(0xFE);

SPI("Begin\n\r");   // 7 characters
for(i=0; i < 500; i++)   // Was 512, but used 12 for text
   {   SPI('M');   }
SPI("\n\rEnd");   // 5 characters


SPI(255);       // Send two bytes of 0xFF at the end
SPI(255);
i=SPI(0xFF);
i &=0b00011111;
if (i != 0b00000101) puts("Write Error in end of write");
while(SPI(0xFF) !=0xFF); // Wait for end of Busy condition

// Read in 512 Byte-Mode
if (Command(0x51,0,512,0xFF) !=0) puts("Read Error ");
while(SPI(0xFF) != 0xFE);
for(i=0; i < 512; i++)
   {    putc(SPI(0xFF));   }       // Send data
SPI(0xFF);     // Send two bytes of 0xFF at the end
SPI(0xFF);

while(1);       // The program stops here.
}
[/code]
_________________
I like Skype (www.skype.com), my username is BplotM
ckielstra



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

View user's profile Send private message

PostPosted: Mon Oct 24, 2005 3:38 pm     Reply with quote

Can you give us more info? Right now you don't give us a lot to go on.

Do you see the output of the debug texts on the serial port?

Are you using MMC or SD cards?

What does your electronics schema look like? Which pins of the PIC have you connected to which pins of the MMC? Did you provide the required pull-up resistors on the data and clock lines? etc.
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Tue Oct 25, 2005 8:05 am     Reply with quote

Hello ckielstra,

Please find my remarks between your text.

ckielstra wrote:
Can you give us more info? Right now you don't give us a lot to go on.

Do you see the output of the debug texts on the serial port?.


Please look again to my first post. See RS232 output (in code window)


Quote:
Are you using MMC or SD cards?.


A real MMC card 8Mb

Quote:
What does your electronics schema look like? Which pins of the PIC have you connected to which pins of the MMC? Did you provide the required pull-up resistors on the data and clock lines? etc.


Please see set_tris_b and set_tris_c section, used pins are mentioned as \\text

Basically, my schema is this from the http://www.cc5x.de/MMC/ site. I know that the data out from the mmc (sdi on pic) without converter from 3.3v to 5v can be the problem, but as it worked with others, I tryed without. Also this schema is for a 16F877. So for my 18F4550 I needed to use other pins.

Hope this is more info to you.

Best greetings.
_________________
I like Skype (www.skype.com), my username is BplotM
ckielstra



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

View user's profile Send private message

PostPosted: Tue Oct 25, 2005 6:22 pm     Reply with quote

The code of the original site is a nice starting point but needs a lot of work as the errors and time outs are handled poorly.

I modified the code by including my own Command function which has better time out handling. In case of errors during the write I also added an extra command for retrieving and printing the MMC status which might give us a clue as to what is going on.

Code:
// TEST : using the mmc in write and read mode.
// Based on http://www.ccsinfo.com/forum/viewtopic.php?t=23183

#include <18F4550.h>
#fuses HS,NOWDT,NOPUT,NOLVP,NOBROWNOUT,NOCPD,NOWRT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_E1, rcv=PIN_E0, ERRORS)

#use fast_io(B)
#use fast_io(C)

// For the PIC 16F877 see manual on page 20/234
// #byte SSPBUF = 0x13
// #byte SSPCON = 0x14
// #byte SSPSTAT = 0x94
// #bit  BF = SSPSTAT.0
// #bit SMP = SSPSTAT.7
// #bit CKE = SSPSTAT.6
// #bit CKP = SSPCON.4
// #bit SSPM1 = SSPCON.1
// #bit SSPEN = SSPCON.5
// #byte PORTC = 7
// #bit CS = PORTC.2

// For the PIC 18F4550 see manual on page 68/426
#byte SSPBUF = 0xFC9
#byte SSPCON = 0xFC6
#byte SSPSTAT = 0xFC7
#bit  BF = SSPSTAT.0
#bit SMP = SSPSTAT.7
#bit CKE = SSPSTAT.6
#bit CKP = SSPCON.4
#bit SSPM1 = SSPCON.1
#bit SSPEN = SSPCON.5
#byte PORTC = 0xF82
#bit CS = PORTC.2


// Command codes
#define MMC_CMD(x)         (0x40 + x)    // A command always starts with the same two start bits.
#define GO_IDLE_STATE     MMC_CMD(0)    // Reset card to Idle State
#define SEND_OP_COND      MMC_CMD(1)    // Activates the card's initialization process.
#define SEND_STATUS       MMC_CMD(13)   // Asks the card to send its status register
#define READ_SINGLE_BLOCK MMC_CMD(17)   // Reads a block of the size selected with SET_BLOCKLEN
#define WRITE_BLOCK       MMC_CMD(24)   // Writes a block of the size selected with SET_BLOCKLEN

#define MMC_TIME_OUT      0xFF          // Just a long time out value
#define TIMER_EXPIRED     0

// -------------------------------------------------
// This is the same as the CCS spi_read() function.
char SPI(char d)
{
  SSPBUF=d;
  while (!BF);
  return SSPBUF;
}

//-----------------------------------------------------------------------------
// Send a command with parameters to the MultiMediaCard
//-----------------------------------------------------------------------------
int8 Command(int8 Cmd, int8 Param1, int8 Param2, int8 Param3, int8 Param4)
{
  int8 Response;
  int8 TimeOutCounter;

  // Allow at least 8 dummy clocks between commands
  spi_read(0xFF);   // note that the first bit is undefined when card is busy (high impedance state)

  // check for the card not being in busy state.
  TimeOutCounter = MMC_TIME_OUT;
  do
  {
    Response = spi_read(0xFF);
  }
  while ((Response != 0xFF) && (--TimeOutCounter != TIMER_EXPIRED));
  if (TimeOutCounter == TIMER_EXPIRED)
  {
    puts("Time out; card busy.\r\n");
    return 0xFF;
  }

  // Send command to the card
  spi_write(Cmd);         // Command
  spi_write(Param1);      // Parameter 1
  spi_write(Param2);      // Parameter 2
  spi_write(Param3);      // Parameter 3
  spi_write(Param4);        // Parameter 4
  spi_write(0x95);        // CRC (including End Bit)
                          // Hard coded the CRC to 0x95 because CRC is always
                          // don't care except for CMD0 which uses 0x95.

  // Wait for response
  TimeOutCounter = MMC_TIME_OUT;
  do
  {
    Response = spi_read(0xFF);
  }
  while ((Response == 0xFF) && (--TimeOutCounter != TIMER_EXPIRED));

  return Response;
}

// -------------------------------------------------
char MMC_Init()
{
  char i;

  // Init SPI
  SMP=0; CKE=0; CKP=1; SSPM1=1; SSPEN=1; //SSPM0=1;
  CS=1;      // MMC-Disabled

  // MMC in SPI Mode -- start and Reset.
  for(i=0; i < 10; i++) SPI(0xFF);            // 10*8=80 clocks
  CS=0;                                       // MMC-Enabled

  // CMD0
  if (Command(GO_IDLE_STATE, 0,0,0,0) != 1)
    goto Error; // Reset

st:
  // CMD1
  if (Command(SEND_OP_COND, 0,0,0,0) != 0)
    goto st ;   // CMD1

  return 1;

Error:
  return 0;
}

// -------------------------------------------------
void main(void)
{
  int16 i;

  setup_adc_ports(NO_ANALOGS);

  // PIC 16F code
  // set_tris_c(0b11010011);    // sck=rc3-0, sdo=rc5-0, CS=rc2-0.
  // set_tris_b(0b00000010);

  // PIC 18F code
  set_tris_c(0b01111011);       // sdo=rc7(pin26)-0, CS=rc2(pin17)-0.
  set_tris_b(0b00000001);       // sck=rb1(pin34)-0, sdi=rb0(pin33)-I

  puts("Start\n\r");
  if(MMC_Init())
    puts("MMC ON - Init OK\n\r");       // MMC Init OK

  // -------------------------------------------------
  // Write block
  if (Command(WRITE_BLOCK, 0,0,2,0) != 0)
    puts("Write error in begin of write"); // Write in 512 Byte-Mode
  else
  {
    SPI(0xFF);              // Wait a minimum of 8 clock pulses
    SPI(0xFE);              // Write the Start Block Token (0xFE)

    SPI("Begin\n\r");       // 7 characters
    for(i=0; i < 500; i++)  // Was 512, but used 12 for text
      SPI('M');
    SPI("\n\rEnd");         // 5 characters


    SPI(0xFF);              // Write the 16-bit CRC at the end (don't cares)
    SPI(0xFF);

    i = SPI(0xFF);          // Get data response token
    i &= 0b00011111;
    if (i != 0b00000101)
    {
      i = Command(SEND_STATUS, 0,0,0,0);
      printf("Write Error in end of write (status: 0x%X)\n", i);
    }
    while(SPI(0xFF) != 0xFF)
      ; // Wait for end of Busy condition
  }

  // -------------------------------------------------
  // Read in 512 Byte-Mode
  if (Command(READ_SINGLE_BLOCK, 0,0,2,0) !=0)
    puts("Read Error ");
  else
  {
    while(SPI(0xFF) != 0xFE);
    for(i=0; i < 512; i++)
      putc(SPI(0xFF));       // Send data
    SPI(0xFF);     // Send two bytes of 0xFF at the end
    SPI(0xFF);
  }
 
  while(1);       // The program stops here.
}


Please run the above code and post the result code for the write error.
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 2:06 pm     Reply with quote

Hello ckielstra,

Thanks for the effort you putted in this topic.
Have bad news for you.
I only receive "Start" on my hyperterminal, no more no less.

Can neverwhere find the SETUP_SPI command in your code, is this correct ?

Will MMC run with a 20Mhz PIC clock ?
_________________
I like Skype (www.skype.com), my username is BplotM
ckielstra



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

View user's profile Send private message

PostPosted: Wed Oct 26, 2005 4:21 pm     Reply with quote

Quote:
I only receive "Start" on my hyperterminal, no more no less.
I didn't notice before, but in your original posting the message for a correct MMC setup wasn't showing either.

Quote:
Can neverwhere find the SETUP_SPI command in your code, is this correct ?
Correct. In MMC_Init the SPI module is configured. I didn't check the setup, but in your code the SPI module was configured twice which for sure will give trouble so I removed one setup.

Quote:
Will MMC run with a 20Mhz PIC clock ?
20MHz is no problem for MMC, actually the maximum SPI clock = CPU clock speed / 4 = 5MHz.
During startup the maximum speed should be limited to 400kHz, this slow-down is one of the things missing from this code but my guess is that most cards won't mind.

The fact that the message for a correct MMC init is missing makes me think you have a hardware problem.
Please double check your hardware. Make sure the SDO of the PIC is connected to the DATA_IN of the MMC and vice versa (a cross connection) and that all pull up resistors are in place.
If possible have your PIC run at the same voltage as the MMC, this requires an PIC18LFxxxx version.
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Thu Oct 27, 2005 9:38 am     Reply with quote

Thanks again.

Will start some measurements with the osciloscoop on the pins of the PIC and MMC.

Will keep everybody informed when I find something.
_________________
I like Skype (www.skype.com), my username is BplotM
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Thu Oct 27, 2005 2:42 pm     Reply with quote

Hello ckielstra,

Did you try your code at your site ?

Where did you define SET_BLOCKLEN ?

Thanks.
_________________
I like Skype (www.skype.com), my username is BplotM
Bart



Joined: 12 Jul 2005
Posts: 49

View user's profile Send private message

PostPosted: Thu Oct 27, 2005 3:20 pm     Reply with quote

STUPID STUPID STUPID of me !!

It was a "create your own hardware" problem.
I am using a floppy drive like connector (less long than the real floppy connector), like mentioned on a lot of sites.

Problem was that the MMC was to deep inserted in the connector, contacts where no longer making contact.

My original code is working fine.

PS : ckielstra : can't manage to let it work with yours. I think something is missing. Thanks anyway for your help.
_________________
I like Skype (www.skype.com), my username is BplotM
baasbrits
Guest







saving a sensor's data on a mmc card by using the pic16f8550
PostPosted: Tue Sep 19, 2006 10:27 am     Reply with quote

Hey I am busy with a project where I want to save the data of g-force sensors on as mmc card by using the pic16f877/ pic18f4550 can anybody please help me
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: Tue Sep 19, 2006 11:10 am     Reply with quote

Unless you are going to add additional memory, forget about the 16F. The MMC and SD write block size if 512 bytes. You MUST write a 512 bytes block. Therefore you need either a PIC with more memory than a PIC16F877 or you need to add the external memory.

If you are planning to use a FAT file system then I suggest using a PIC with 4K RAM. I suggest the PIC18F4620 variants and above are the realistic minimum for general purpose applications that require the ability to write to SD cards with the FAT file system.

If you are looking for reference hardware design schematics interfacing to an SD/MMC card check out my projects page on my web site.
_________________
Regards, Andrew

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







PostPosted: Thu Sep 28, 2006 6:28 am     Reply with quote

Post deleted by Admin. There are more civil ways of discussing your 'out of the box' ideas.
fox
Guest







PostPosted: Sun Oct 08, 2006 8:12 am     Reply with quote

Foxabilo wrote:
Post deleted by Admin. There are more civil ways of discussing your 'out of the box' ideas.


Touched a nerve I see.

Ohh well I'll keep my corrections to errors to myself Smile
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