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 support@ccsinfo.com

SPI to get data from compass

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








SPI to get data from compass
PostPosted: Fri Aug 11, 2006 1:19 pm     Reply with quote

I can't get data from the compass:

What I got are:

X=0

Y=0

Can anybody help?
I am new to PIC

Thanks


Here is my code:
Code:
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 4000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)   

unsigned int   xdata,ydata,hdg_data;    //8-bit unsigned data type

#define _cs      PIN_B1         //Active Low
#define ser_out   PIN_C5
#define ser_in   PIN_C4
#define clk      PIN_C3

//******Initialization********
void init(void)
{
   output_high(_cs);
   delay_us(1);
   output_low(clk);
   delay_us(1);
   output_low(ser_out);
   delay_us(1);
   output_low(ser_in);
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
}


//*******Get xdata**************
void get_xdata()
{
   output_low(_cs);
   delay_us(1);
   output_high(clk);
   delay_us(1);
   spi_write(0x0B);
   delay_us(100);
   spi_write(0x0C);
   delay_ms(20);
   xdata=spi_read();
   delay_us(100);
   output_low(clk);
   delay_us(1);
   output_high(_cs);
   delay_ms(10);
   
   printf("\n\rX: %u\n\r",xdata);
   
   
}


//*******Get ydata**************
void get_ydata()
{
   output_low(_cs);
   delay_us(1);
   output_high(clk);
   delay_us(1);
   spi_write(0x0D);
   delay_us(100);
   spi_write(0x0E);
   delay_ms(20);
   ydata=spi_read();
   delay_us(100);
   output_low(clk);
   delay_us(1);
   output_high(_cs);
   delay_ms(10);
   
   printf("\n\rY: %u\n\r",ydata);
   
   
}
//*********main***********
main()
{
   delay_ms(20);
   init();
   

   
while(1)
  {
     
   
   get_xdata();
   delay_ms(20);
   get_ydata();   
   
   

   
   
  }

}



My compass is PNI-V2Xe from PNI corporation.

Connected to PIC as follow:

PNI_SCLK-----------PIC_RC3
PNI_MISO-----------PIC_RC4
PNI_MOSI-----------PIC_RC5
PNI_SSNOT---------PIC_RB1
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 11, 2006 1:48 pm     Reply with quote

I didn't look at your code in detail, but your SPI mode is incorrect. The
PNI-V2Xe data sheet shows that the chip operates in Mode 0 (page 18):
https://www.pnicorp.com/downloadResource/c4b6/datasheets/73/v2xebook.pdf
You have it configured for Mode 2. I suggest that you configure the
SPI with the #define statements shown below. Then it's very clear
as to what mode is being used:

Code:

#define SPI_MODE_0_0 0x4000
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0010
#define SPI_MODE_1_1 0x4010

//======================================
void main()
{
setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_16);

while(1);
}


There might be other problems in your code. I didn't look for more.
Guest








PostPosted: Fri Aug 11, 2006 2:14 pm     Reply with quote

I didn't look too much either but if you are using hardware SPI you should not ever have to output_ anything on the CLK line. Its handled by hardware.

I've never put 1us delays in between my CS and data before. Every SPI device I have ever used has had >100nS propogation between CS set asserted and CS hold.

If you were at 80Mhz you might actually need to delay a few cycles.

If you have the time it doesn't hurt to leave it there I guess, but you are only 4Mhz anyway so why bother?

My point of view is from working on mostly time sensitive projects. I usually can not stop what I'm going even for 1uS.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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