|
|
View previous topic :: View next topic |
Author |
Message |
jjacob
Joined: 08 Mar 2008 Posts: 54 Location: PORTUGAL (PORTO)
|
SPI problems while connecting PIC18F452 to MCP3208 ADC |
Posted: Thu Aug 28, 2008 12:46 pm |
|
|
Hi,
i'm trying to read 8 analog signals. So i found MCP3208 who has a SPI interface. I've never worked with SPI and can't get the right values of ADC inputs. I've looked in forum but i couldn't find out the solution.
My version of PCH Compiler is 4.057.
My problem is that the only result that i can get i 0 (zero) in the three readings of SPI.
I've connected the Vref of MCP3208 to Vcc (5v) and i force (with an ampop for low output impedance) 2.5V in CH0.
This is my code :
Code: | #include <18F452.h>
// -----------------------------
// ICs interconnections
// -----------------------------
// PIC Descr MCP3208 Descr
// -----------------------------
// 24 SDO 11 SDI
// 23 SDI 12 SDO
// 18 SCK 13 CLK
// 17 RC2 10 ~CS/SHDN
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#use spi(MASTER, MODE=3, BITS=8, FORCE_HW, ENABLE=PIN_C2)
#use spi(MASTER, MODE=1, BITS=8, FORCE_HW, ENABLE=PIN_C2)
//#define CS PIN_C2
int8 read1, read2, read3;
void main(void)
{
// If i don't delay here, the printf doesn't print....
delay_ms(1);
printf("\r\nTesting Serial\r\n");
delay_ms(1000);
while(true)
{
// Select CH0 (send 6 + 0 + X) (pag. 15 of MCP3208 datasheet)
spi_write(6);
spi_write(0);
spi_write(0);
//while(!spi_data_is_in());
read1 = spi_read();
//while(!spi_data_is_in());
read2 = spi_read();
//while(!spi_data_is_in());
read3 = spi_read();
printf("\r\nFirst : %d, Second : %d, Third : %d\r\n", read1, read2, read3);
delay_ms(100);
}
}
|
I've tested with MODE=3...
Can anybody help me to get the right values of MCP3208.
Thank you in advance.
Jacob |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 28, 2008 1:00 pm |
|
|
I suggest that you don't use the "#use spi()" method. Just use the
setup_spi() function. We know that it works.
Read my post in this thread about how to setup up the correct mode,
and especially, how to use the spi_read() statement to read a byte.
http://www.ccsinfo.com/forum/viewtopic.php?t=35566
Also, I notice that you're using inline code for your driver. I suggest
that you duplicate the functions in the CCS driver (which uses software
SPI), and change the "guts" of each function to use hardware SPI.
Here's the driver location:
Quote: | c:\program files\picc\drivers\mcp3208.c |
|
|
|
jjacob
Joined: 08 Mar 2008 Posts: 54 Location: PORTUGAL (PORTO)
|
|
Posted: Thu Aug 28, 2008 4:45 pm |
|
|
Thank you PCM programmer.
I'm quite new with CCS and i should have looked to the drivers...
I will try to change from 'Software' to 'Hardware' SPI but i'm not an expert...
If i change :
Code: | #define MCP3208_CLK PIN_B0
#define MCP3208_DOUT PIN_B1
#define MCP3208_DIN PIN_B2
#define MCP3208_CS PIN_B3 |
to the Hardware pins wouldn't work ? (Sorry if this is a basic question ...)
Somewhere in this forum, you said that if we use the hardware pins in the software implementation it would not work... something like this ...
I've read the files that you said... but for the moment is not easy to change the code 'guts'... but i'll try
Thank you.
Jacob |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 28, 2008 5:06 pm |
|
|
The hardware SPI pins (SCLK, SDO, SDI) are specified in the PIC data
sheet. The only pin that you can assign is the \CS signal.
If you are translating a software driver to hardware, then just delete
the #define statements for CLK, DOUT and DIN. You don't get to
decide on them. They are not even referenced in the hardware SPI
driver code. The pin assignments are implicit. When you put in
the line "spi_write(0x55);", you are inherently specifying that the
data (0x55) will go out on the SDO pin of the PIC. |
|
|
|
|
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
|