spi interface with accelerometer and pic 18f448 problem

hi i am new user to microcontroller and accelerometer adxl345 i have a great problem in interfacing the spi of picadxl345 and pic 18f448 as i want to write to an addresses of registers inside the accelerometer with special data to be configured to read displacemmment “after double integeration” i have a great problem in in how to differentiate between the data and adress as in the accelerometer datasheet there are 2 bit in begining of address (1 for read ,o for write ) and other bit used for multiple bit and then 6 bit address and data 8 bit what i want to know if i can make both by write spi after each other only and after them what to start new transmission ? and other i dont know if i set bit 1 for read i enter as example char x=readspi() only after it ?

last question and i am sorry for long question is to configure uart from output of spi to be at out put i want to see an example of this case please because really i am in great troouble as my graduation project presentation in less than 2 weeks
#include <p18f448.h>
#include <usart.h>
#include <delays.h>
#include<stdlib.h>
#include<stdio.h>
#include <spi.h>
#define SPI_CS LATBbits.LATB0
char x,y[2],o;
int m;
void delay (void)
{
int i;
for (i = 0; i < 3500; i++)
;
}
void delay1 (void)
{
int i;
for (i = 0; i < 1000; i++)
;
}
void delay2 (void)
{
int i;
for( i=0;i<=366;i++);

}
void main (void)
{
OpenSPI(SPI_FOSC_16, MODE_00, SMPEND);
OpenUSART(USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,92);

TRISB=0x00;
while(1){LATBbits.LATB0=0b0;
WriteSPI(0x6D); // adress of accelerometer
WriteSPI(0x01); //data
Delay10KTCYx( 50 );
LATBbits.LATB0=0b1;
Delay10KTCYx( 20 );
LATBbits.LATB0=0b0;
WriteSPI(0xED);//to enable write bit
o=ReadSPI();
LATBbits.LATB0=0b1;

m=o; // to convert to integer
itoa(m,y);//to be read by uart
putrsUSART(“id”);
putrsUSART(" “);
putsUSART( y);
putrsUSART(” ");
}}

i dont know problem of program as i think the code of read spi ao write spi is wrong

There are so many weird quirks with SPI that it will be almost impossible to find a perfect solution on the Internet.

I suggest that you get four things to solve your problem: an oscilloscope (preferably a digital one, as you can “pause” these), the accelerometer datasheet (especially the SPI timing diagrams), the microcontroller datasheet, and patience.

Try sending a sample data packet over the SPI bus. Hook up the SEL, CLK, MISO and MOSI lines to the oscilloscope. Watch the data packet being sent on the scope, and see if it looks like the timing diagrams. It could be something as simple as the SEL line goes active a half CLK cycle after the MOSI line starts sending data, throwing everything off. Or it could be much more complicated.

Debugging is a critical engineering skill, and it’s one that can only be learned through lots of experience.

Also, you might have a better chance of getting a response by posting in the SPI forum on Microchip; a bigger community of specifically PIC users means you’ll have a better chance of finding someone who’s had experience doing something similar with SPI on PIC, much less on the 18f448.

–Ryan

thanks artdutra04 you make feel that i h as no hope as really i tired from spi interface and it’s problems :slight_smile: :slight_smile: i will make the topic in spi forum thanks RyanCahoon