View previous topic :: View next topic |
Author |
Message |
geokonst
Joined: 04 Apr 2009 Posts: 27
|
spi_read help |
Posted: Sat Apr 04, 2009 6:19 am |
|
|
Dear all,
I am trying to interface a pic24 with a LIS302DL accelerometer via spi
The code I am using is
Code: |
#include "C:\...Spi.h"
#PIN_SELECT SDI1=PIN_G8
#PIN_SELECT SDO1=PIN_G7
#PIN_SELECT SCK1OUT=PIN_G6
#PIN_SELECT SS1OUT=PIN_B1
#PIN_SELECT U2TX=PIN_F5
#PIN_SELECT U2RTS=PIN_F4
#use rs232(UART1,baud=19200,parity=N,bits=8)
#use spi(MODE=2,SAMPLE_FALL, BITS=8)
byte dataout;
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
while (1){
output_bit (PIN_B2,0);
spi_read(0x8f);
dataout = spi_read(0xFF);
output_a (dataout);
delay_us(10);
output_bit (PIN_B2,1);
delay_ms(10);
}
} |
I didn't use spi_write because that wouldn't generate a clock and I used two reads so I could get 16 clk cycles. Also I had to manually lower CS as it spi_read wouldnt do it.
Anyway, the above code works and I can see on the scope that the accelerometer responds fine to the whoamI register 8f.
The problem is that dataout is always 0. I have no idea why this is happening so any help is very welcome.
Best regards |
|
|
Guest
|
|
Posted: Sat Apr 04, 2009 11:46 am |
|
|
Search the forums. There is plenty of example code out there. Also I noticed you are not setting up your control registers. Good Luck |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 04, 2009 12:13 pm |
|
|
Quote: | #use spi(MODE=2,SAMPLE_FALL, BITS=8)
byte dataout;
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16); setup_spi2(SPI_SS_DISABLED); |
You are using the #use spi() library code and the setup_spi() statement.
You should only use one of these methods. Don't use both. I suggest
that the setup_spi() method is more likely to work.
Quote: | setup_wdt(WDT_ON); |
Don't enable the Watchdog timer in a test program, where your main
goal is to test communication with a device. Delete that line.
Quote: | while (1){
output_bit (PIN_B2,0); |
Is this the Slave Select line ? If so, use a #define statement above
main() to define it. Example:
Code: | #define LIS302DL_CS_PIN PIN_B2 |
Then do it like this in your code:
Code: | output_low(LIS302DL_CS_PIN); |
Quote: | I didn't use spi_write because that wouldn't generate a clock |
Don't you think that's a major problem ? It should be investigated
and fixed, if possible.
Here is a thread on the LIS302DL:
http://www.ccsinfo.com/forum/viewtopic.php?t=37644
The person never stated if he got it working. But the thread contains
a lot of useful information. |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Sat Apr 04, 2009 1:06 pm |
|
|
Dear Gust/Programmer, thank you both for your prompt reply.
Guest:
I have searched the forum but couldn't find something relevant. I am not sure I understand what you mean control registers, could you please define? Thanks for your wish. Please wish me skills too.
PCM Programmer:
Quote: | You are using the #use spi() library code and the setup_spi() statement.
You should only use one of these methods. Don't use both. I suggest
that the setup_spi() method is more likely to work. |
You are right! thanks. I fixed that but with no results.
Quote: | Don't enable the Watchdog timer in a test program, where your main
goal is to test communication with a device. Delete that line. |
I did that too. nothing
Quote: | Is this the Slave Select line ? If so, use a #define statement above
main() to define it. | I really don't see the point for doing that, especially on a test code. Am I missing something?
Quote: | Don't you think that's a major problem ? It should be investigated
and fixed, if possible.
|
Yes I definitely think that's a major problem. But I've tried various approaches and this one works.
Thank you for the link, but I think we are missing the point here. I do get a response from the accelerometer. The pic though is not reading it.
Weird isn't it? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 04, 2009 2:21 pm |
|
|
Are you using the correct SPI mode ? In the link that I posted, I said
that the LIS302DL uses mode 3. In your original post, you are using
mode 1. |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Sat Apr 04, 2009 2:38 pm |
|
|
Sorry I was trying different things, but yes i am generally using mode 3. BTW I wrote some spi bit-banging code and works fine. Its the hardware I can't make to work.
the revised according to your suggestions code is:
Code: |
#include "C:\...\ Spi.h"
#PIN_SELECT SDI1=PIN_G8
#PIN_SELECT SDO1=PIN_G7
#PIN_SELECT SCK1OUT=PIN_G6
//#PIN_SELECT SS1OUT=PIN_B1
#PIN_SELECT U2TX=PIN_F5
#PIN_SELECT U2RTS=PIN_F4
#use rs232(UART1,baud=19200,parity=N,bits=8)
#use spi(BITS=8, MASTER, MODE=3)
int dataout;
void main(){
while (1){
output_bit (PIN_B2,0);
spi_read(0x8f);
dataout = spi_read(0xFF);
output_a (dataout);
delay_us(10);
output_bit (PIN_B2,1);
delay_ms(10);
}
} |
I chose #use because it seems to have more options, although I also tried using only spi_setup.
Thank you very much for your time and patience |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 04, 2009 4:40 pm |
|
|
But the #use spi() library is new. It has been buggy. It's safer to use
the older setup_spi() method. |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Sat Apr 04, 2009 5:11 pm |
|
|
Ok I will give it another go, although I have already. I'll keep you updated.
Thanks. If you have any other ideas please let me know. |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Sat Apr 04, 2009 6:07 pm |
|
|
Dear PCM,
is SPI_XMIT still supported in spi_setup? The compiler is giving me an " Undefined identifier ... SPI_XMIT_L_TO_H
But come to think about it if I had the mode wrong I should still get some data- wrong but non-zero. So there must be something fundamental going wrong. I have checked the connections to the uC.
Cheers! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 04, 2009 7:12 pm |
|
|
These spi mode #define statements use constants from the .H files
for 16F and 18F PICs:
Code: | #define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) |
Your 24F PIC's .h file apparently doesn't all of them.
I don't have the PCD compiler, so I can't find out what's in the .H file.
I probably shouldn't have tried to help at all, for that reason. But I
thought I could help in some generic way, or with the LIS302DL. |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Sat Apr 04, 2009 7:54 pm |
|
|
Thank you very much for trying to help me anyway. I really appreciate it. I am really stuck here so all thoughts are welcome
The only reasonable explanation I could think of is that for some reason the pin is set to output. But they are by default inputs aren't they? and I also tried changing it to input with a read input command and a tris. Nothing. |
|
|
Guest
|
|
Posted: Sat Apr 04, 2009 8:16 pm |
|
|
Page 26 of the manual "Ctrl_Reg1(20H)" the PD bit needs to be set to one or else the accelerometer goes into power down mode. I have never worked with PIC24 before. |
|
|
Guest
|
|
Posted: Sat Apr 04, 2009 10:01 pm |
|
|
Dear Guest,
As I said before the accelerometer IS responding. You are right about the register but this does not apply to whoamI.
Thanks |
|
|
geokonst
Joined: 04 Apr 2009 Posts: 27
|
|
Posted: Mon Apr 06, 2009 9:40 am |
|
|
Dear all,
Just a small update - something I found out during these endless hours of trial and error.
putting a delay of about 30 us (i guess depends on osc and spi clk divider) between two spi_writes /reads will clock properly (16 cycles), otherwise the clock cycle will overlap, for reasons unknown.
Still the same problem though |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|