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 support@ccsinfo.com

pic16f886 mmc read/write help

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



Joined: 22 Apr 2009
Posts: 8

View user's profile Send private message

pic16f886 mmc read/write help
PostPosted: Wed Apr 22, 2009 2:38 am     Reply with quote

MMC initializes fine.
When I do a write and get the response at the end, it turns up as 00000010 | 10000000... for the busy, when the expected is 00000101|000000...

When I do a read after that, I get what I wrote except shifted left one.
For example, if I write 00001010 512times, I will read 00010100 512times.
Or if I write 00000111 512times, I will read 00001110 512times.

It's weird because the responses for all the commands are fine so it's not like the entire dataout is being shifted over 1.

Has anyone else had this problem?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 22, 2009 10:28 am     Reply with quote

Shifted data may mean that you're using the wrong SPI mode.
Post your setup_spi() statement.
gbie



Joined: 22 Apr 2009
Posts: 8

View user's profile Send private message

PostPosted: Wed Apr 22, 2009 11:08 am     Reply with quote

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

SETUP_SPI(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
jamilsalem



Joined: 29 Sep 2008
Posts: 3
Location: Uberlandia-MG-Brasil

View user's profile Send private message Send e-mail

BUG? PCWHD 4.084 and SPI
PostPosted: Wed Apr 22, 2009 4:06 pm     Reply with quote

I am having problems with pcwhd 4.084. I developed a program that read and write sd memory card by hardware. When I initialize the compiler and compile my program, the compiler generate the correct code. In the second time, for the same program, the compiler generate other code, and the program burned not work correctly. Maybe this is a bug of the ccs compiler. By the way, use the following software part to configure the SPI to communicate with a SD memory card.
Code:

#use spi(MASTER,FORCE_HW, BITS=8, stream=MMCSD_SPI)

/////////////////////////////////////////////////////////////
#ifndef SAIDA
#define SAIDA 0
#endif

#ifndef ENTRADA
#define ENTRADA 1
#endif
// Configurações do Usuário - mudar de acordo com o pic escolhido
#byte PORTA_A          =   0xf80          // (PIC16F - 0x05) (PIC18F - 0xf80)
#byte PORTA_B          =   0xf81          // (PIC16F - 0x06) (PIC18F - 0xf81)
#byte PORTA_C          =   0xf82          // (PIC16F - 0x07) (PIC18F - 0xf82)

#byte PIR1             =   0xf9e
#bit  SSPIF            =   PIR1.3

#bit SPI_SCK = PORTA_C.3
#bit SPI_DO  = PORTA_C.5
#bit SPI_DI  = PORTA_C.4

#byte TRIS_PORTA_A     =   0xf92    // (PIC16F - 00x85) (PIC18F - 0xf92)
#byte TRIS_PORTA_B     =   0xf93    // (PIC16F - 0x86) (PIC18F - 0xf93)
#byte TRIS_PORTA_C     =   0xf94    // (PIC16F - 0x87) (PIC18F - 0xf94)

#bit TRIS_SPI_SCK      =   TRIS_PORTA_C.3
#bit TRIS_SPI_SDI      =   TRIS_PORTA_C.4
#bit TRIS_SPI_SDO      =   TRIS_PORTA_C.5
#bit TRIS_SPI_MMC_CS   =   TRIS_PORTA_C.0//TRIS_PORTA_B.0
#bit TRIS_DETECTA_MMC  =   TRIS_PORTA_B.0//TRIS_PORTA_B.4

#bit MMC_CS            =    PORTA_B.2 //PORTA_B.0                                                                                                                                                                           
#bit MMC_DETECT        =    PORTA_B.0 //PORTA_B.4
/////////////////////////////////////////////////////////////
#byte SSPBUF     = 0xfc9     // (PIC16F - 0x13) (PIC18F - 0xfc9)
#byte SSPSTAT    = 0xfc7     // (PIC16F - 0x94) (PIC18F - 0xfc7)
#byte SSPCON1    = 0xfc6     // (PIC16F - 0x14) (PIC18F - 0xfc6)
#byte SSPCON2    = 0xfc5     // (PIC16F - 0x91) (PIC18F - 0xfc5)

/////////////////////////////////////////////////////////////

#bit  BF        = SSPSTAT.0  // flag que indica recebimento ou envio no barramento SPI
#bit CKE        = SSPSTAT.6  // 1 = transmissão de dados quando clock vai de ativo para idle, e 0 o contrário
#bit SMP        = SSPSTAT.7  // em modo master deixar em 1 (amostra no final da msg enviada), em modo slave deixar em 0   
#bit CKP        = SSPCON1.4  // polaridade do clock, (1 clock é ativado em nível baixo, 0 o clock é ativado em nível alto)
#bit SSPEN      = SSPCON1.5  // 1 = spi mode, 0 = io mode

#define SPI_SLAVE_SS_DESABILITADO    0xf5
#define SPI_SLAVE_SS_HABILITADO      0xf4
#define SPI_MASTER_SCK_TMR2          0xf3
#define SPI_MASTER_SCK_DIV_64        0xf2
#define SPI_MASTER_SCK_DIV_16        0xf1
#define SPI_MASTER_SCK_DIV_4         0xf0
/////////////////////////////////////////////////////////////
//
//
....
....
//
//
//   setup_spi(SPI_SS_DISABLED);
   TRIS_SPI_SCK      =   SAIDA;
   TRIS_SPI_SDI      =   ENTRADA;
   TRIS_SPI_SDO      =   SAIDA;
   TRIS_SPI_MMC_CS   =   SAIDA;
//
   MMC_CS   = TRUE;
   SPI_SCK  = TRUE;
   SPI_DI   = TRUE;
   SPI_DO   = FALSE;
//
   mmcsd_deselect();
      SSPEN = FALSE;
      SSPCON1 = SPI_MASTER_SCK_DIV_4;
//      CKE = FALSE;
//      SMP = TRUE;
//      CKP = TRUE;
      CKE = TRUE;
      SMP = FALSE;
      CKP = FALSE;
      SSPIF = FALSE;
      SSPEN = TRUE;
//
//<continue your code here>

Thanks.

jamilsalem@gmail.com
gbie



Joined: 22 Apr 2009
Posts: 8

View user's profile Send private message

PostPosted: Wed Apr 22, 2009 5:01 pm     Reply with quote

problem fixed:
i was sending the wrong data start byte.
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