View previous topic :: View next topic |
Author |
Message |
regall
Joined: 15 Jan 2005 Posts: 6 Location: Turkey
|
pic 18f252 spi clock problem |
Posted: Wed Feb 16, 2005 4:20 am |
|
|
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
|
hi |
Posted: Thu Feb 17, 2005 8:33 am |
|
|
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
|
|
Posted: Thu Feb 17, 2005 3:14 pm |
|
|
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
|
|
Posted: Fri Feb 18, 2005 8:19 am |
|
|
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 |
Posted: Tue Nov 01, 2005 7:04 pm |
|
|
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); |
|
|
|
|