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

cc2500 spi interfacing with pic

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



Joined: 28 Aug 2014
Posts: 6

View user's profile Send private message

cc2500 spi interfacing with pic
PostPosted: Thu Aug 28, 2014 5:12 am     Reply with quote

I need some help from you that am on interfacing the cc2500 with PIC micro controller.
I
have found that, when i write an even no data(eg: 0x02) to a reg,i got output always 0x0F when i read same reg. And when i write an odd no data (eg 0x05 (00000101) ) to a reg, i got output one bit rotated 0x82 (10000010).

Hoping for a reply.

Thanks in Advance.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 6:57 am     Reply with quote

Hi,

Are you using the CCS 'C' Compiler?

Post the code you've got that shows the problem.

John
namiz



Joined: 28 Aug 2014
Posts: 6

View user's profile Send private message

PostPosted: Thu Aug 28, 2014 10:45 pm     Reply with quote

Thank you John for reply.
my code is here. Yes, am using CCS C Compiler.

Code:

#include <18F25K20.h>

#fuses   BROWNOUT
#fuses   NOPUT
#fuses   NOPROTECT
#fuses   NOWDT
#fuses   INTRC
#byte SSPSTAT = 0XFC7
#byte SSPBUF = 0XFC9
#byte SSPCON1 = 0XFC6
#byte TRISA = 0XF92
#byte TRISC = 0XF94
#byte ADCON1 = 0XFC1
#bit BF = SSPSTAT.0



#define LED   PIN_B4
#define SS   PIN_C7
#define SDO   PIN_C5
#define SDI   PIN_C4
#use delay(clock=64000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7,ERRORS,STREAM=Debug,DISABLE_INTS)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS,STREAM=Debug,DISABLE_INTS)
unsigned char val[3]={};
void SPIinit();
void spiwrite(unsigned char data);
unsigned char spiread();
unsigned char spitrfx(unsigned char data);
void WriteReg(unsigned char addr, unsigned char value);
unsigned char ReadReg(unsigned char addr);

main()
{
int i;
setup_oscillator(OSC_64MHZ);

SPIinit();
SET_TRIS_B( 0x00 );

while(1)
{
   fprintf(Debug,"first running****\r\n");
   
   writereg(0x1B,0x05);
   delay_ms(10);
   val[0]=ReadReg(0x1B);

   delay_ms(2000);
   
   for(i=0;i<1;i++)
   {
      //val[i] = spiread();
      fprintf(Debug,"val[%d]:%x\r\n",i,val[i]);
   }
   output_bit( SS, 1);


//while(1);

}

}

void SPIinit()
{
TRISC = 0X10;
SSPCON1 = 0X21;
SSPSTAT = 0X00;

}

void spiwrite(unsigned char data)
{
SSPBUF = data;
while(BF!=1);
data = SSPBUF;

}

unsigned char spitrfx(unsigned char data)
{
SSPBUF = data;
while(BF!=1);
return(SSPBUF);
}

unsigned char spiread()
{
return(SSPBUF);

}

void WriteReg(unsigned char addr, unsigned char value)
{
unsigned char a,b;
  output_bit(SS,0);
 
  while (input(SDI));
 
   
  a=spitrfx(addr);
  delay_ms(10);
  b=spitrfx(value);
  delay_ms(10);   
  output_bit(SS,1);
fprintf(Debug,"a=%d, b= %d\r\n",a,b);
}

unsigned char ReadReg(unsigned char addr)
{
unsigned char x,y;
  addr = addr + 0x80;
  output_bit(SS,0);
  while (input(SDI));
  x = spitrfx(addr);
  delay_ms(10);
  y = spitrfx(0);
  delay_ms(10); 
  output_bit(SS,1);
fprintf(Debug,"x=%d, y= %d\r\n",x,y);
  return y; 
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Fri Aug 29, 2014 1:43 am     Reply with quote

If using CCS, use CCS!......

Use the CCS functions for SPI.
Get rid of all your register accesses. They are not needed.

Now the common reason for reading the value 'rotated', is that you are clocking on the wrong edge in the SPI settings.
The cc2500, expects the clock to start low, and for you to read/write on the rising edges of the clock. This is SPI mode 1.
So select with:

#use SPI(MODE=1, SPI1, STREAM=CC2500, baud=8MHz)

And then to send a byte, use:

spi_xfer(CC2500,val_to_send,8);

and to read a byte at the same time, use:

val_returned=spi_xfer(CC2500,val_to_send,8);

Let the compiler do it's job.
namiz



Joined: 28 Aug 2014
Posts: 6

View user's profile Send private message

PostPosted: Thu Sep 04, 2014 5:42 am     Reply with quote

I have used ccs functions but still theres same problem.
Now when i read same reg, it shows different values.

I changed the code like this.
Code:

#use delay(clock=64000000)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7,ERRORS,STREAM=Debug,DISABLE_INTS)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS,STREAM=Debug,DISABLE_INTS)
#use SPI(MODE=1, SPI1, STREAM=CC2500, baud=8000000)
unsigned char val[3]={};
void WriteRegCCS(unsigned char addr, unsigned char value);
unsigned char ReadRegCCS(unsigned char addr);
main()
{

int i;
setup_oscillator(OSC_64MHZ);

//SPIinit();
TRISC = 0X10;
SET_TRIS_B( 0x00 );

while(1)
{


   fprintf(Debug,"first running****\r\n");
   writeregCCS(0x09,0x04);
   delay_ms(10);
   val[0]=ReadRegCCS(0x09);

delay_ms(2000);
   
   for(i=0;i<1;i++)
   {
      //val[i] = spiread();
      fprintf(Debug,"val[%d]:%x\r\n",i,val[i]);
   }
   output_bit( SS, 1);

}

}


void WriteRegCCS(unsigned char addr, unsigned char value)
{
unsigned char a,b;
  output_bit(SS,0);
 
  while (input(SDI));
 
  a=spi_xfer(CC2500,addr,8); 
 
  delay_ms(10);
  b=spi_xfer(CC2500,value,8);
  delay_ms(10);   
  output_bit(SS,1);
fprintf(Debug,"a=%d, b= %d\r\n",a,b);
}

unsigned char ReadRegCCS(unsigned char addr)
{
unsigned char x,y;
  addr = addr + 0x80;
  output_bit(SS,0);
  while (input(SDI));
  x = spi_xfer(CC2500,addr,8);
  delay_ms(10);
  y = spi_xfer(CC2500,0,8);
  delay_ms(10); 
  output_bit(SS,1);
fprintf(Debug,"x=%d, y= %d\r\n",x,y);
  return y; 
}

I got output as
/////// HYPERTERMINAL OUTPUT///////////////////

first running****
a=15, b= 15
x=15, y= 15
val[0]:c6
first running****
a=15, b= 15
x=15, y= -51
val[0]:cd
first running****
a=15, b= 32
x=15, y= -12
val[0]:f4
first running****
a=15, b= 4
x=15, y= -118
val[0]:8a


Thanks in advance..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 05, 2014 2:31 pm     Reply with quote

If you get flaky data, you should suspect if you have the wrong SPI mode.

Download the CC2500 data sheet:
http://www.ti.com/lit/ds/symlink/cc2500.pdf
Look at the SPI timing diagram on page 22. Which edge of SCLK do they
show samples the data, right in the middle of the byte ?

This website shows the clock idle state and the sampling edge for
each mode.
http://www.totalphase.com/support/articles/200349236-SPI-Background#modes

You need to edit this statement and fix the mode:
Code:
#use SPI(MODE=1, SPI1, STREAM=CC2500, baud=8000000)
namiz



Joined: 28 Aug 2014
Posts: 6

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 4:45 am     Reply with quote

I have gone through datasheet and
it is understood that.
idle state of sck (ckp) = 0
cke = 1
and sample @ middle of data.

i have changed mode now i got only
0x0F as i read any reg aftr write operation.

Thanks in advance.
namiz



Joined: 28 Aug 2014
Posts: 6

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 4:45 am     Reply with quote

I have gone through datasheet and
it is understood that.
idle state of sck (ckp) = 0
cke = 1
and sample @ middle of data.

i have changed mode now i got only
0x0F as i read any reg aftr write operation.

Thanks in advance.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 17, 2014 2:20 pm     Reply with quote

I always forget how to figure out the correct SPI mode and that's why I made a short lookup table:
Code:
// SPI modes overview.
// SPI Mode | MOTOROLA | MICROCHIP | CCS                          | Clock line idle | Data clocked in at
//----------------------------------------------------------------|-----------------|-------------------
//          | CPOL CPHA|  CKP CKE  |                              |                 |
//    0     |  0    0  |   0   1   | SPI_L_TO_H | SPI_XMIT_L_TO_H | low             | low to high edge
//    1     |  0    1  |   0   0   | SPI_L_TO_H                   | low             | high to low edge
//    2     |  1    0  |   1   1   | SPI_H_TO_L                   | high            | high to low edge
//    3     |  1    1  |   1   0   | SPI_H_TO_L | SPI_XMIT_L_TO_H | high            | low to high edge

Howto:
1) Look up the clock level in idle state.
2) Look up if the data is clocked in at the up-going or down-going edge.
3) Find SPI mode in the table.

When I look at page 22 of the referenced datasheet I see:
1) Clock in idle = low
2) Data is clocked in at up-going edge.
==> SPI mode = mode 0
That's different from PCM's example but the same as what you've figured out (CPOL=0, CKE=1), we just don't know if it's the same as what you put down in your program.
jsureshp



Joined: 22 Jul 2010
Posts: 1
Location: chennai

View user's profile Send private message

CC2500 program with pic
PostPosted: Fri Mar 25, 2016 3:52 am     Reply with quote

Hi Namiz,

Please could you share the working program for CC2500 with PIC.
_________________
Suresh
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