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

pic 18f252 spi clock problem

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



Joined: 15 Jan 2005
Posts: 6
Location: Turkey

View user's profile Send private message MSN Messenger

pic 18f252 spi clock problem
PostPosted: Wed Feb 16, 2005 4:20 am     Reply with quote

i am using 18f252 for spi comminication with a chip. but i am not getting good responses . for example i must get 3ch for an ACK but i am getting 7f and followed by 9e. it is happening at all commands when i want ACK.
and another responses are also different. cristal is 8MHz. what is the problem?

#fuses HS,NOWDT
#use delay (clock=8000000)

setup_spi(SPI_MASTER | SPI_H_TO_L)

spi_write(0x00); //reset command
delay_ms(1);
data=spi_read();
printf("The response=%x\n\r",data);
EdWaugh



Joined: 07 Dec 2004
Posts: 127
Location: Southampton, UK

View user's profile Send private message

hi
PostPosted: Thu Feb 17, 2005 8:33 am     Reply with quote

I'm not clear exactly what your problem is or what you mean by good responses? Do you mean your device isn't telling you what you expect?

I think also with spi_read(0xff) you need to pass a FF value as this is the clocks or something.

You might also need to poll your device and continually send the reset command until it starts to respond.

I've had a problem with the spi on a 18f252 where if I try div_64 the clock goes completely wrong and much too slow! It doesn't seem to matter what my crystal is and code that works fine at lower divisions gets broken. Anyone have an experience of that?

cheers

ed
Ttelmah
Guest







PostPosted: Thu Feb 17, 2005 3:14 pm     Reply with quote

This is down to how SPI works. When you send a byte with SPI, a byte is _simultaneously_ clocked in the other direction. Your code at present, clocks a byte in the 'transmit' direction, and reads the byte that was returned in _this_ transaction, not the following byte (which would be the acknowledge).
If you 'send' a byte with the 'read' command (as another poster has suggested), _this_ byte is clocked out, and the byte that is then received is returned (which is what you want), or you can 'write' a second dummy value, and then read the return.
Code:

#fuses HS,NOWDT
#use delay (clock=8000000)

setup_spi(SPI_MASTER | SPI_H_TO_L)

spi_write(0x00); //reset command
spi_read(); //throw away the already 'read' data
//the delay here is to allow time for a return to be generated.
delay_ms(1);
data=spi_read(0);
//Now when you clock out a byte, the required return is received
printf("The response=%x\n\r",data);
 


You could also use a second 'write', followed by a read (instead of adding an 'outgoing' byte to the read routine).

Best Wishes
regall



Joined: 15 Jan 2005
Posts: 6
Location: Turkey

View user's profile Send private message MSN Messenger

PostPosted: Fri Feb 18, 2005 8:19 am     Reply with quote

that is clear now. i am gettin only one ACK code now. and the unwanted data before ACK is clocked out. in addition the problem of wrong ACK code is solved by changing the protocol.
Guest








Re: pic 18f252 spi clock problem
PostPosted: Tue Nov 01, 2005 7:04 pm     Reply with quote

regall wrote:
i am using 18f252 for spi comminication with a chip. but i am not getting good responses . for example i must get 3ch for an ACK but i am getting 7f and followed by 9e. it is happening at all commands when i want ACK.
and another responses are also different. cristal is 8MHz. what is the problem?

#fuses HS,NOWDT
#use delay (clock=8000000)

setup_spi(SPI_MASTER | SPI_H_TO_L)

spi_write(0x00); //reset command
delay_ms(1);
data=spi_read();
printf("The response=%x\n\r",data);
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