View previous topic :: View next topic |
Author |
Message |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
CCS SPI functions |
Posted: Wed Sep 19, 2012 3:31 am |
|
|
Hello to everyone.
I'm new using CCS SPI library and i have one question:
after using setup_spi, spi is ready to use?
no more settings?
I´m using pic16f886 to read ADXL345 values but get nothing. I put a scope at pin14 (RC3/SCK/SCL) and don't see clock activity.
I'm using CCS demo with MPLAB plugin.
There is my code:
Code: |
#include "../h/ring.h"
#use delay(clock=8000000)
#include "../h/fuses.h"
#use rs232(baud=1200, xmit=PIN_B6, rcv=PIN_B7)
void init(){
setup_oscillator(OSC_8MHZ);
SET_TRIS_B(0x82);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);//
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_64 );//
SET_TRIS_C(0x00); //
spi_write(0x31); //send the write instruction
//spi_write(0x01); //address of a register to test reading and writing //
spi_write(0x2D); //send the write instruction//
spi_write(0x08); //address of a register to test reading and writing
}
void main (){
init();
printf( "teste");
char x0;
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
set_timer1(0);
for(;;){
OUTPUT_LOW(PIN_C0);
delay_us(5);
x0= spi_read(0x32); //send the write instructio;
OUTPUT_HIGH(PIN_C0);
printf( "valor %c\r\n", x0);
delay_ms(1000);
}
}
|
To read adxl345, is enough using write and read spi functions?
Regards,
Luis Loureiro
Last edited by lloureiro on Wed Sep 19, 2012 5:42 am; edited 2 times in total |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Wed Sep 19, 2012 5:21 am |
|
|
welcome !!
You should always tell us the PIC type and the compiler version and post a small, compilable program that shows the problem as it's near impossible to decide where or what your problem is. Could be a compiler bug (rare), PIC variant (not all PICs are the same) or hardware (testing wrong pin, miswired,..), etc.
Also, check the examples folder that CCS supplies, to see how they setup/use the SPI module of the PICs. They've supplied LOTS of great examples you can run/use/modify !!
You can search this forum as well, using 'SPI' to see hits of similar SPI problems. Odds are good you're NOT the first one having the 'problem'!!
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
not the firts and not the last... |
Posted: Wed Sep 19, 2012 5:44 am |
|
|
...but i searched i didn't found a problem like my problem
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Sep 19, 2012 7:31 pm |
|
|
Code: |
for(;;){
OUTPUT_LOW(PIN_C0);
delay_us(5);
x0= spi_read(0x32); //send the write instructio;
OUTPUT_HIGH(PIN_C0);
printf( "valor %c\r\n", x0);
delay_ms(1000); |
the above is what you want to use as your accelerometer driver???
and you have read and UNDERSTAND the datasheet for the part?
as in where is your management code for the NOT CS line of the ADXL ??
( as follows ??)
Code: |
spi_write(0x31); //send the write instruction
//spi_write(0x01); //address of a register to test reading and writing //
spi_write(0x2D); //send the write instruction//
spi_write(0x08); //address of
|
you are sure you are handling the right pins etc etc?? |
|
![](templates/subSilver/images/spacer.gif) |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
|
Posted: Thu Sep 20, 2012 1:51 am |
|
|
Hello,
I change my code, and now it is:
//init
Code: | #define ACC1_SELECT PIN_C0
#define ADXL345_ADDR_WRITE 0xA6
#define ADXL345_ADDR_READ 0xA7
|
Code: | ...
setup_oscillator(OSC_8MHZ);
SET_TRIS_B(0x82);
SET_TRIS_C(0x10);
setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_4 );
accelerometer_init2();
.... |
where mode 3 is: Code: | #define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
and the init method:
Code: | void accelerometer_init2(void)
{
output_low(ACC1_SELECT);
spi_write(ADXL345_ADDR_WRITE);
spi_write(ADXL345_POWER_CTL); ´
spi_write(0x00);
spi_write(ADXL345_ADDR_WRITE);
spi_write(ADXL345_POWER_CTL);
spi_write(0x08);
spi_write(ADXL345_ADDR_WRITE);
spi_write(0x31);
spi_write(0x00);
output_high(ACC1_SELECT);
} |
and i run a selt test that exists on device:
Code: |
int16 x_test(void)
{
unsigned char dummy=0;
unsigned char xh=0;
unsigned char xl=0;
int16 xo=0;
output_low(ACC1_SELECT);
spi_write(ADXL345_ADDR_WRITE);
spi_write(0x31);
spi_write(0x80);
output_high(ACC1_SELECT);
output_low(ACC1_SELECT);
spi_write(ADXL345_ADDR_WRITE);
spi_write(ADXL345_DATAX0);
xl = spi_read();
spi_write(ADXL345_ADDR_WRITE);
spi_write(ADXL345_DATAX1);
xh = spi_read();
output_high(ACC1_SELECT);
xo = xl|(xh << 8);
return xo;
} |
and always get the value of "0".
And i used a scope, i can see pic pulses on sdo of pic but i don't see any clock activity of pic?
Something wrong with this clock setting?
thanks everyone |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Sep 20, 2012 4:57 am |
|
|
SCL should be working. Did you make your own hardware board or buy a ready made one?
Check physical connections, solder problems, etc.
PIC pin 14 (RC3 / SCL) is connected to what parts / pin numbers on your board?
With another program, can you make a LED blink on this port?
Code: | #define ADXL345_ADDR_WRITE 0xA6
#define ADXL345_ADDR_READ 0xA7 | This seems suspicious to me. Where do these values come from? |
|
![](templates/subSilver/images/spacer.gif) |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
|
Posted: Thu Sep 20, 2012 5:53 am |
|
|
Hello ckielstra
i purchased one made on sparkfun:
Triple Axis Accelerometer Breakout - ADXL345
https://www.sparkfun.com/products/9836
It is connected to pin 14 of adxl345.
The problem is that i don't see activity on pin pic, so i will follow your idea and make a led blink, to check if pin isn't damaged.
theses values are wrong :( it was to i2c program sorry :(
so...i have a question when i look to example "9356SPI.C" of ccs compiler:
Code: |
spi_write(0xa);
spi_write(address);
spi_write(data); |
looking to adxl345, i need to write tree times? or two? |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Thu Sep 20, 2012 6:20 am |
|
|
hmm....back to basics..
You ARE running this PIC and module on 3 volts right ?????? |
|
![](templates/subSilver/images/spacer.gif) |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
|
Posted: Thu Sep 20, 2012 6:33 am |
|
|
Right not exactly 3v, but 3.3V |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Sep 20, 2012 12:49 pm |
|
|
3.3V is fine for both the ADXL345 and PIC16F886.
I suggest you have a look at the Sparkfun site again as several code examples are posted there. For example this Arduino code example is easy to convert to CCS and should get you going.
Try this code and when not working come back here and post your complete program, that is, not using any special header files that are not included with the compiler ("../h/ring.h" ) |
|
![](templates/subSilver/images/spacer.gif) |
lloureiro
Joined: 19 Sep 2012 Posts: 9
|
3 wire vs 4 wire |
Posted: Fri Sep 21, 2012 2:38 am |
|
|
Good morning.
I'm a little confused now. I saw that pic16f886 supports 3 wire spi.
http://ww1.microchip.com/downloads/en/DeviceDoc/41291D.pdf
But when I look at mssp diagram, it have SDO and SDI pins, not the same.
I only connect sda adxl345 to sda RC4/SDI/SDA of pic?
and the clock and chip select?
And I need change trisc direction, right?
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Fri Sep 21, 2012 2:24 pm |
|
|
You also have SCL. Pin C3.
You can use _any_ pin to control CS. It is not a hardware 'feature' of the SPI interface. The interface has:
CLOCK - SCL
DATA OUT - SDO - must connect to the input of the other device
DATA IN - SDI - must connect to the output of the other device
Then CS on the slave device _you_ enable, by connecting to a pin on the PIC, and driving this LOW when you want to talk to the device, and raising it when the transaction has finished.
The fact that this is any pin, is why you can control more than one device, just using different pins to drive their CS lines.
No, you do not have to control TRIS. The compiler does this for you.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Fri Sep 21, 2012 10:16 pm |
|
|
There's another convention for naming SPI lines for the data pins that give more "instant understanding" when looking at schematics and such..
MOSI - Master Out Slave In
MISO - Master In Slave Out
Just thought I'd mention...
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Sep 22, 2012 2:58 am |
|
|
At first sight, there are two problems in your code:
- you are setting port C to all output after setup_spi(). This will reprogram SDI (=MISO) to an output and block SPI data read.
- you are not activating PIN_C0 during initialization
As mentioned by Ttelmah, setup_spi() takes care of the TRIS settings of SPI related pins, but it won't correct later changes to TRIS_C.
The SCK output is however supposed to work. If you don't see it, this point should be clarified first. Maybe it's shorted in hardware. |
|
![](templates/subSilver/images/spacer.gif) |
|