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

Problem with function interface spi in CCS

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



Joined: 08 Jun 2010
Posts: 13

View user's profile Send private message Yahoo Messenger

Problem with function interface spi in CCS
PostPosted: Tue Jul 06, 2010 10:38 am     Reply with quote

I'm debugging sdcard interface is to problem with spi function in CCS.
My schematic:(18f4550+sdcard)

I used spi solfware:
Code:
#define CS   PIN_D3
#define SCK  PIN_D6
#define SDO  PIN_D5
#define SDI  PIN_D4
#use spi( DI=SDI, DO=SDO, CLK=SCK, BITS=8, stream=sspi)

And the error occurs as above(at the connection between RD5 and D0 has the error:the logic contention)
I thought the pins PORTD was multiplexed so i added
Code:
TRISD =0b00100010;//D0 and TXD is input

but still not successful.
Please help me ...!
Thanks a lot !
kideltn



Joined: 08 Jun 2010
Posts: 13

View user's profile Send private message Yahoo Messenger

PostPosted: Tue Jul 06, 2010 11:03 am     Reply with quote

My code:
Code:



#include "basicsd.h"

#define CS   PIN_D3
#define SCK  PIN_D6
#define SDO  PIN_D5
#define SDI  PIN_D4
#use spi( DI=SDI, DO=SDO, CLK=SCK, BITS=8, stream=sspi)

#define _DEBUG

#ifdef _DEBUG
#define dbg(str) printf(str)
#endif

// Prototypes
void command(int8, int16, int16, int8);
int mmc_init();


// Implementations

// Send 6-byte Command to MMC
void command(int8 cmd, int16 adh, int16 adl, int8 crc)
{
   spi_xfer(sspi,cmd);
   spi_xfer(sspi,make8(adh,1));
   spi_xfer(sspi,make8(adh,0));
   spi_xfer(sspi,make8(adl,1));
   spi_xfer(sspi,make8(adh,0));
   spi_xfer(sspi,crc);
}

// Initialize the Card
int mmc_init()
{
   int16 i;
   
   
 

   // Unselect Card and Send some Dummy Clocks
   output_high(CS);
   for(i= 0; i< 10; i++)   
   {
      spi_xfer(sspi,0xEF);
   }

   // Select Card
   output_low(CS);

   // Send Command 0x40: Wake Up the Card
   command(0x40, 0, 0, 0x95);
   for(i= 0; i< 100; i++)
   {
      if(spi_xfer()== 0x01)
      {
         break;
      }
   }

   if(i>= 100) // Check for Time-out Error
   {
      return(1);
   }
   dbg("CMD40 OK (R1= 0x01)\n\r");

   // Send Command 0x41: Start Initialize
   command(0x41, 0, 0, 0xFF);
   for(i= 0; i< 1000; i++)
   {
      if(spi_xfer()== 0x00)
      {
         break;
      }
   }
   if(i>= 1000) // Check for Time-out Error             
   {
      return(1);
   }
   dbg("CMD41 OK (R1= 0x00)\n\r");

   // Return OK
   return 0;
}


void main()
{
   printf("> MMC Test by DEBUGGER...\n\r");
   printf("> Start MMC Init...\n\r");
   if(mmc_init()== 0)
   {
      printf("> MMC Init Ok...\r\n");
   }
   else
   {
      printf("> failure.continue debug!\r\n");
   }
 
   while(1);

}

My above code tested success with pic 16f877a (with the same pins)
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jul 06, 2010 11:46 am     Reply with quote

SPI DO is an output at the processor.
ckielstra



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

View user's profile Send private message

PostPosted: Tue Jul 06, 2010 12:27 pm     Reply with quote

And the other common issues:
- The no pull-up resistors on the DO and DO lines are missing.
- You have not specified the SPI mode in the setup of '#use SPI'. MMC and SD cards can be used in either SPI-mode 0 or 3.
- Your PIC should be running at 3.3V, not 5V (you might be lucky if it works in Proteus, but it will kill real hardware).
- Post your compiler version (the SPI_xfer routines are known to be broken in older v4 compiler versions).
kideltn



Joined: 08 Jun 2010
Posts: 13

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Jul 07, 2010 5:34 am     Reply with quote

Thanks every one very much !
I have solved it! (error is confused DO and DI).
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