Jump to content

Recommended Posts

Hi,

 

I try to establish communication between pic18f4550 and SPI compatible memory.

 

When checking with scope, data are corrected written but the reading stucks on "while(bf == 0)".

 

I have probably made something wrong. I would really appreciate any comment about the following SPI routines :

 

uchar SpiWrite(uchar value){

uchar tmp ;

sspbuf = value ;

while(bf == 0) ; /*wait for transmission completed.*/

tmp = sspbuf ; /*clear bf*/

if(wcol){ /*in case of bus collision.*/

wcol = 0 ;

return 0 ; /*-> bus collision problem*/

}

else return 1 ; /*writing OK*/

}

/*____________________________________________________________________________*/

uchar SpiRead(void){

uchar tmp ;

while(bf == 0) ; /*wait for data received*/

tmp = sspbuf ;

return tmp ;

}

 

Thank you for helping.

Share this post


Link to post
Share on other sites

Dear nic_steph

 

Data can be read when a command was written to read data ie you send the command to read something, and right after it was sent the answer should be available.

 

 

uchar SpiRead(uchar value){
uchar tmp;
sspbuf = readcom; /*you put in here the command to read from certain register*/
while(bf == 0); /*wait for transmission completed and answer received*/
tmp = sspbuf; /*answer goes into tmp*/
}

 

Depending on the device you might have to send quite a few commands before a read, but as soon as the final read command was sent and BF is set then the answer should be in sspbuf and can be read,

 

I am struggling with something similar so I hope that at least this little of what think I know is right.

Share this post


Link to post
Share on other sites
Hi,

 

I try to establish communication between pic18f4550 and SPI compatible memory.

 

When checking with scope, data are corrected written but the reading stucks on "while(bf == 0)".

 

I have probably made something wrong. I would really appreciate any comment about the following SPI routines :

 

uchar SpiWrite(uchar value){

  uchar tmp ;

  sspbuf = value ;

  while(bf == 0) ; /*wait for transmission completed.*/

  tmp = sspbuf ; /*clear bf*/

  if(wcol){ /*in case of bus collision.*/

    wcol = 0 ;

    return 0 ; /*-> bus collision problem*/

  }

  else return 1 ; /*writing OK*/

}

/*____________________________________________________________________________*/

uchar SpiRead(void){

  uchar tmp ;

  while(bf == 0) ; /*wait for data received*/

  tmp = sspbuf ;

  return tmp ;

}

 

Thank you for helping.

 

I know a very little about SPI access, which I feel should share with you. please point out if I am wrong.

actually , when you read from SPI device(I used SPI E2PROM) the device will not generate clock to send data. the micro-controller (master) will have to simulate the clock line and data will be clocked in the sspbuf. now to simulate the clock I have sent dummy data(0x00). this transmits dummy data as well as simulates the clock to get the data from the chip.

 

please read the routine which works, may not be appropriate for you, but you will get some clues at least.

 

/***************************** read routine start *************************/

void SPI_Read(unsigned char No_Of_Bytes)

{

unsigned char i = 0,l;

Test_Buffer = 0;

fsr = 0x38;

CS = 0xFF;

 

trisc = 0x10;

asm

{

nop

}

Send_Command(Write_Disable); //WRDI 0x04

CS = 0xFF;

asm

{

nop

}

Send_Command(Read_Memory_Array); //READ 0x03

asm

{

nop

nop

nop

nop

}

sspbuf = 0x02; //Hi address

asm

{

nop

nop

nop

nop

nop

nop

nop

}

sspbuf = 0x10; //Lo_Address;

asm

{

nop

nop

nop

nop

nop

nop

}

while(i < No_Of_Bytes)

{

trisc = 0x30; //C.5 - input,C.4 - input

sspbuf = 0x00;

 

asm

{

nop

nop

nop

nop

nop

}

while(!(sspstat & 0x01))

{

;

}

indf = sspbuf ;

fsr++;

i++;

}

CS = 0xFF;

trisc = 0x10;

 

}

 

/***************************** read routine over *************************/

 

 

Hope this will be of some help.

 

 

Regards,

 

P B Raju

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...