Please I need a code example for using SPI to receive data from an ADC connected to a PIC18. I have connected ltc1291 to pic18 but the reading is always 0. anyone can help or had a functional example to start with?
Disconnect the ADC and try a loopback test. That will show if your SPI code is working.
Leon
Also, there’s a PIC forum here that might give you more/better answers:
this is a clever idea but how to test it on SPI, do you mean connecting SDI to SDO of the PIC18F4550, I’m using pin 33 for SDI/MISO and the 26 for SDO/MOSI and 34 for SCK.
I have tried ReadSPI() function or content of SSPBUF register and the result is always 0.
By the way is any mcu configuration has impact on the performance or op of spi? I tested the output of the spi on display and it works fine but my issue is receiving.
Yes, just connect MISO to MOSI for a loopback test. You should be able to read back what you have written to the port if the software is working. I often use a scope as well.
I also use a 74HC595 with LEDs on the outputs for testing, the '595 is SPI-compatible. I made a PCB for it.
Leon
I think I have code wrong, if I do below then low variable comes with value 9 with loopback and without so something is wrong.
SSPBUF should be cleared then tested with received byte, I do not know how?
SPI_CS = 0; //Chip Select
SSPBUF = 0x09;
while(!(SSPSTATbits.BF)) // wait until data sent
low = SSPBUF;
SPI_CS = 1;
low will of course be 9, so how to clear buffer then try to receive the original value?
Why not use the SPI library? Here’s some of my code that uses it:
// initialise 18F4520
void init(void)
{
// run internal oscillator at 8 MHz
OSCCON = OSCCON | 0x70;
while (OSCCONbits.IOFS == 0)
;
PORTA = 0x00;
ADCON1 = 0x0F; // set up PORTA to be digital I/Os
TRISA = 0x02; // PORTA<7.2,0> outputs PORTA<1> input
TRISCbits.TRISC3 = 0; // SDO output
TRISCbits.TRISC5 = 0; // SCK output
TRISCbits.TRISC2 = 0; // CSN output
TRISCbits.TRISC1 = 0; // CE output
TRISBbits.TRISB0 = 1; // IRQ input
OpenSPI(SPI_FOSC_16, MODE_00, SMPMID); //open SPI1
OpenTimer0( TIMER_INT_OFF &
T0_16BIT &
T0_SOURCE_INT &
T0_PS_1_256 );
}
unsigned char spi_Send_Read(unsigned char byte)
{
SSPBUF = byte;
while(!DataRdySPI())
;
return SSPBUF;
}
You just have to include spi.h.
See the peripheral libraries documentation.
Leon
Thank you Leon I have tried the function spi_Send_Read without using the init() because the pic freezes when it used and I’m using
OpenSPI(SPI_FOSC_64, MODE_00, SMPEN); //open SPI
but the result is same, I even hooked both display panel & the LTC1219 chip though spi wires, you know only CS has to be seperate for each device and the display panel works fine while the ADC returning back once shows 8 and another receive 85 (the dummy value sent is 0x55 which is 85 decimal)
I changed the test voltage on the input channels of ADC and nothing changed in the result. :?
By the way how to change this entry to the proper catogery on the forum, I noticed i’m posting at AVR Prog…
SPI is very very nasty and for no clear reason started working as supposed to be but my external ADC is 12bit and all what I can read is byte, how I can convert it to int???
I appreciate any help please.
You probably have to read two bytes.
Leon
This is what it turns out to be; I’m working on that.No one on the web mentioned that the dummy value to be sent out is not dummy each IC might requires special starting word to start the adc on some channel.
Thank you Leon for your help, now I got it to function properly.