CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

spi_read help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

spi_read help
PostPosted: Sat Apr 04, 2009 6:19 am     Reply with quote

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








PostPosted: Sat Apr 04, 2009 11:46 am     Reply with quote

Search the forums. There is plenty of example code out there. Also I noticed you are not setting up your control registers. Good Luck Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 12:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 1:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 2:21 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 2:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 4:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 5:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 6:07 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 7:12 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 04, 2009 7:54 pm     Reply with quote

Thank you very much for trying to help me anyway. I really appreciate it. I am really stuck here so all thoughts are welcome Smile

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








PostPosted: Sat Apr 04, 2009 8:16 pm     Reply with quote

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








PostPosted: Sat Apr 04, 2009 10:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 06, 2009 9:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 06, 2009 10:13 am     Reply with quote

You never posted your PIC. You could go to the Microchip Data Sheet
finder page and look at the errata documents for your PIC.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2046
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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