|
|
View previous topic :: View next topic |
Author |
Message |
SBS
Joined: 15 Aug 2005 Posts: 18
|
SPI Clock not working? |
Posted: Sun Oct 23, 2005 10:22 am |
|
|
Hi,
I'm trying to set up an SPI port to communicate with an ADC. It doesn't appear to be working.
I am putting an oscillscope on the DO pin from the microcontroller and I am seeing a digital signal. However, when I put the oscilloscope on the clock pin, I see nothing.
Is there anything wrong with my code? The CCS help manual doesn't give ver much information about how the setup_spi command works.
Any suggestions?
Thanks,
Sal
#include <18F4620.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define ADC_SELECT PIN_B1
#define ADC_DI PIN_C5
#define ADC_DO PIN_C4
#define ADC_CLK PIN_C3
void main() {
long int speed1;
long int speed2;
long int tempspeed;
BYTE data;
char display;
speed1 = 100;
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
while (TRUE) {
printf(" _ ");
output_high(PIN_B0);
delay_ms(speed1);
output_low(PIN_B0);
delay_ms(speed1);
data = 0xFA;
spi_write(0x40);
data = spi_read();
}
} |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun Oct 23, 2005 10:39 am |
|
|
Where are you supplying the ADC with a chip select?
You may have to set the I/O direction to enable the SPI PINS. I do this by default but maybe the compiler does it automatically. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
SBS
Joined: 15 Aug 2005 Posts: 18
|
|
Posted: Sun Oct 23, 2005 11:10 am |
|
|
Hi,
My ADC has its chip select tied low (active low).
I realized that I had my oscilloscope hooked up to the wrong signal. The ADC still isn't working yet, but now I have a better idea of what is going on.
With the code I posted, I am getting the correct digital output and the correct clock output pulses on the SPI_WRITE command. However, whenever I call SPI_READ, I do not see any clock pulses.
Any suggestions?
Thanks,
Sal |
|
|
Ttelmah Guest
|
|
Posted: Sun Oct 23, 2005 2:38 pm |
|
|
You won't see clock pulses with a normal read.
SPI, is a two way bus. When you write a byte, a byte is _simultaneously_ read back. the 'read' command, just retrieves this byte (though see below). Remember also that the byte read back, is the _last_ one presented by the slave. So if you have a slave that responds with a byte from a command, you have to go:
Code: |
spi_write(COMMAND);
//At this point a garbage byte will have been read
//Check the data sheet for how long the slave needs to prepare
//the byte to return.
spi_write(DUMMY);
//Assuming the slave does not allw another transfer in this direction at
//the same time - clocks out the value 'dummy'.
rval=spi_read();
//Here you receive the byte that was clocked when the 'dummy'
//byte was written.
|
CCS, allows you to use the 'shortcut', of 'spi_read(DUMMY)', which clocks out the dummy value, and reads the byte returned. Unless a value is passed to 'read', no clock will occur however, ad the last byte transfered will be returned.
Best Wishes |
|
|
SBS
Joined: 15 Aug 2005 Posts: 18
|
|
Posted: Sun Oct 23, 2005 4:24 pm |
|
|
Hi,
Thanks for your help. I've almost got it working now. I can write to and read from my ADC. However, my ADC is not responding yet.
I've tried both SPI_L_TO_H and SPI_H_TO_L. Looking at the SPI registers I see that the SPI modes that this function sets are:
SPI_L_TO_H
CPK=0
CPE=0
SPI_H_TO_L
CPK=1
CPE=1
Looking in the PIC manual (PIC18F4620) I have 4 supported SPI modes. The setup_spi command only appears to be able to utilize two of them.
What I need is the mode of:
CPK=0
CPE=1
This exactly matches the timing diagram in my ADC manual. Can I set up this SPI MODE with the CCS command? Or do I need to directly set the registers to get this functionality?
Thanks,
Sal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|