|
|
View previous topic :: View next topic |
Author |
Message |
namiz
Joined: 28 Aug 2014 Posts: 6
|
cc2500 spi interfacing with pic |
Posted: Thu Aug 28, 2014 5:12 am |
|
|
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
|
|
Posted: Thu Aug 28, 2014 6:57 am |
|
|
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
|
|
Posted: Thu Aug 28, 2014 10:45 pm |
|
|
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
|
|
Posted: Fri Aug 29, 2014 1:43 am |
|
|
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
|
|
Posted: Thu Sep 04, 2014 5:42 am |
|
|
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
|
|
|
namiz
Joined: 28 Aug 2014 Posts: 6
|
|
Posted: Wed Sep 17, 2014 4:45 am |
|
|
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
|
|
Posted: Wed Sep 17, 2014 4:45 am |
|
|
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
|
|
Posted: Wed Sep 17, 2014 2:20 pm |
|
|
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
|
CC2500 program with pic |
Posted: Fri Mar 25, 2016 3:52 am |
|
|
Hi Namiz,
Please could you share the working program for CC2500 with PIC. _________________ Suresh |
|
|
|
|
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
|