View previous topic :: View next topic |
Author |
Message |
abhishek@karmayog
Joined: 01 Dec 2011 Posts: 5
|
cc2500 with pic |
Posted: Thu Dec 01, 2011 6:53 am |
|
|
this is the code i am trying to use to read pktlen register from cc2500.
the expected reset value is ffh
I get 00h.
I want to interface pic with cc2500.
can anyone help?
Code: |
#include<16F877A.h>
#fuses HS,NOPROTECT,NOBROWNOUT,NOWDT,NOPUT,NOWRT,NODEBUG,LVP,NOCPD
#use delay(clock=12000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void lcd_reset()
{
output_d(0xFF);
delay_ms(20);
output_d(0x30 + 0x08);
output_d(0x30);
delay_ms(20);
output_d(0x30 + 0x08);
output_d(0x30);
delay_ms(20);
output_d(0x30 + 0x08);
output_d(0x30);
delay_ms(20);
output_d(0x20 + 0x08);
output_d(0x20);
delay_ms(20);
}
void lcdcmd(unsigned char cmd)
{
output_d((cmd & 0xF0) + 0x08);
delay_ms(20);
output_d(cmd & 0xF0);
output_d(((cmd<<4) & 0xF0) + 0x08);
delay_ms(20);
output_d((cmd<<4) & 0xF0);
//return;
}
void lcddat(unsigned char dat)
{
output_d((dat & 0xF0) + 0x0C);
delay_ms(20);
output_d((dat & 0xF0) + 0x04);
output_d(((dat<<4) & 0xF0) + 0x0C);
delay_ms(20);
output_d(((dat<<4) & 0xF0) + 0x04);
}
void lcd_init()
{
lcd_reset();
lcdcmd(0x28);
delay_ms(20);
lcdcmd(0x0E);
delay_ms(20);
lcdcmd(0x01);
delay_ms(20);
lcdcmd(0x06);
//while(1);
delay_ms(20);
lcdcmd(0x81);
//lcddat('A');
//delay_ms(20);
}
void hextoascii(unsigned char a)
{
unsigned char msb,lsb;
msb=(a & 0xf0);
lsb=(a & 0x0f);
if(msb<=0x90)
{
msb=((msb>>4)+0x30);
}
else
{
//printf("%x",msb);
msb=msb-0xa0;
//printf("%x",msb>>4);
msb=((msb>>4)+0x41);
//printf("%x",msb);
}
if(lsb<=0x09)
{
lsb=((lsb)+0x30);
}
else
{
lsb=((lsb)+0x41-0x0a);
}
lcddat(msb);
lcddat(lsb);
}
void config_yantra()
{
unsigned char reply;
unsigned int1 start;
output_low(PIN_D3);
while(input_state(PIN_C4));
//spi_write(0x86);
//output_high(PIN_D3);
//while(!spi_data_is_in());
reply=spi_read(0x86);
hextoascii(reply);
//delay_us(12);
//spi_write(0x19);
//spi_write(0x98);
reply=spi_read(0);
while(!spi_data_is_in());
hextoascii(reply);
output_high(PIN_D3);
//lcddat("H");
while(1);
//SPI_WRITE ();
//while(!spi_data_is_in());
//reply=spi_read();
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(spi_master |spi_h_to_l | spi_clk_div_4);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
SET_TRIS_D(0x00);
//output_d(0xdb);
set_tris_b(0x00);
//output_b(0x06);
//delay_ms(1000);
//delay_ms(1000);
lcd_init();
lcddat('P');
delay_ms(20);
lcddat('I');
delay_ms(20);
lcddat('C');
delay_ms(20);
//hextoascii(0xff);
config_yantra();
} |
_________________ Innovator |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9216 Location: Greensville,Ontario
|
|
Posted: Thu Dec 01, 2011 7:23 am |
|
|
first..please use the 'code' option for posting your code here.It'll really help us ry to firgure what's going on...
2nd. always add 'errors' to the use RS232(....) options..
3rd. after you call 'config_yantra) you don't do anything with the 'reply'(data coming back
4th. 'main' only executes once as there is no while(1) {....} around the code
5th. get rid of the tris() commands..unless you're absolutely sure you've set it up right !
6th. if the LCD isn't running correctly, use the CCS supplied driver.
well that should help you along..... |
|
|
abhishek@karmayog
Joined: 01 Dec 2011 Posts: 5
|
|
Posted: Thu Dec 01, 2011 8:12 am |
|
|
The LCD is working well.
3rd. after you call 'config_yantra) you don't do anything with the 'reply'(data coming back
I am displaying the data coming back on lcd using hextoascii routine
Code: |
void hextoascii(unsigned char a)
{
unsigned char msb,lsb;
msb=(a & 0xf0);
lsb=(a & 0x0f);
if(msb<=0x90)
{
msb=((msb>>4)+0x30);
}
else
{
//printf("%x",msb);
msb=msb-0xa0;
//printf("%x",msb>>4);
msb=((msb>>4)+0x41);
//printf("%x",msb);
}
if(lsb<=0x09)
{
lsb=((lsb)+0x30);
}
else
{
lsb=((lsb)+0x41-0x0a);
}
lcddat(msb);
lcddat(lsb);
} |
This code i have made is just beginning to configure cc2500
I am just trying to to read values of registers from cc2500.
Can anybody help me with spi communication for cc2500 _________________ Innovator |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Dec 01, 2011 8:40 am |
|
|
Just a few more tips:
Are you sure you want to use the LVP fuse? In the last 5 years I have seen no-one on this forum using a Low Voltage Programmer. Setting the LVP fuse has the disadvantage you can not use the I/O pin for other purposes and a small induced voltage spike can halt your processor if you have no pull-up resistor (or pull-down, I always forget which one because I never use this feature).
Give functions a name that represents what the function is actually doing. Your function hextoascii is actually displaying a hex value on the LCD, so for example display_hex would have been a better name.
Second, the whole function hextoascii can be replaced by a call to printf. In CCS the printf function was extended with a clever feature to redirect the output to a seperate displaying function, i.e. your lcddat function.
This way, you can replace the call:by: Code: | printf(lcddat, "%02X", reply); |
Your code will be easier to read if you use define meaningful names for the I/O pins. For example you could replace: Code: | output_low(PIN_D3); | by: Code: | #define CS PIN_D3
output_low(CS); |
When configuring the SPI settings I like to use the following defines: Code: | // SPI mode definitions.
#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) | You are setting up mode2. When I look at the timing diagram, Figure 7, of the CC2500 datasheet it looks to me like mode0 should be used instead: Code: | setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4 ); | Read more about SPI modes on Wikipedia
Finally a link to another forum where they discuss the CC2500 being very difficult to setup and a few tips to get you started: http://www.eevblog.com/forum/index.php?topic=4280.0 |
|
|
abhishek@karmayog
Joined: 01 Dec 2011 Posts: 5
|
|
Posted: Sun Dec 11, 2011 12:58 pm |
|
|
I am really thankful for your valuable suggestions.
Sorry to reply late.
I am really not good at making my codes self explanatory.
I'll try my best to improve my coding style. _________________ Innovator |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Sun Dec 11, 2011 3:22 pm |
|
|
The CC2500 is a 3.3V device and the PIC is 5V. What kind of interface are
you using between them for the SPI? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
abhishek@karmayog
Joined: 01 Dec 2011 Posts: 5
|
|
Posted: Mon Dec 12, 2011 2:21 am |
|
|
Well I am using lm1117 to power up cc2500 and output voltage of 7805 to power up pic
Is it ok or there can be problems with spi? _________________ Innovator |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9216 Location: Greensville,Ontario
|
|
Posted: Mon Dec 12, 2011 8:09 am |
|
|
well IF you're lucky you have NOT destroyed the cc2500 !
You MUST have a 'level translator' IC between the PIC and the CC2500 !
it is very,very bad to try to run 3V only devices on 5V power supply!!!
You have 2 options.
1) add the level translator IC
or
2) use an 'L' series PIC. L meaning low voltage( 3.3Volts),which is compatible with the CC2500. |
|
|
abhishek@karmayog
Joined: 01 Dec 2011 Posts: 5
|
|
Posted: Mon Dec 12, 2011 1:59 pm |
|
|
Quote: | You MUST have a 'level translator' IC between the PIC and the CC2500 !
it is very,very bad to try to run 3V only devices on 5V power supply!!! |
The lm1117 which i am using to power up cc2500 is 3.3V dc regulator.
What i was asking is that the spi pins connected between cc2500 and pic, should they also be reduced to 3.3V while communicating with cc2500? _________________ Innovator |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Mon Dec 12, 2011 4:39 pm |
|
|
Look at page 6 of the datasheet.
It says Absolute MAX 3.6V on ANY digital pin!
As Temtronic said you may have blown the device... _________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9216 Location: Greensville,Ontario
|
|
Posted: Mon Dec 12, 2011 5:39 pm |
|
|
You must learn to read the datasheets and understand the specifications or you will really never get far.
A PIC running with a 5 volt supply typically needs a '1' to be 80% of 5 volts. Quick math says that's 4 volts.
Your CC2500 has a Vcc of 3.6 volts,which means that a '1' from it will look like a '0' to the PIC,sinc it is less than the minimum 4 volts the PIC needs.
This is just one simple example,the logic level numbers change a bit depending on the type of pin but should show the importance that you must read the datasheet.
It has always been best(easier) to use PICs and peripherals that run at the same voltage( All 5 volt devices or all 3.6 volt devices).Yes, you can use 'level translator' interface chips between them, but that adds to the overall cost, wiring issues, and perhaps 'minor' timing problems that will pull your hair out ! |
|
|
|