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

xbee radio communication between PICs

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



Joined: 12 Jul 2010
Posts: 2

View user's profile Send private message

xbee radio communication between PICs
PostPosted: Mon Jul 12, 2010 4:23 am     Reply with quote

Hi,
I m trying to send the temperature from one PIC to an other and I am facing some problems.
I work in simulation mode, Proteus 7 for the schematic and CCS C for the C Code.

Here My Code,

the file UART.c
Code:

char RecUART (void);
void EmitUART(char c);
void InitUART(void);
/* Initialisation de l'UART */
void InitUART(void)
{
TXSTA = 0x20;
RCSTA = 0x90;
SPBRG = 0x06;
}
/* Emission d'un octet sur l'UART */
void EmitUART(char c)
{
TXREG = c;
}
/* Reception d'un Octet sur l'UART */
char RecUART (void)
{
return(RCREG);
}

The file xbee.c
Code:

#include "USART.c"

void InitXbee(void)
{
set_tris_d=0x18;
PORTD=0x05;
}
void SendByte(char c)
{
EmitUART(c);
}

char ReadByte(void)
{
return(RecUART());
}

void SendString(const unsigned char *s)
{
int i=0;
while(s[i]!='\0')
{
/* utilisation de lcd_putch */
SendByte(s[i]);DelayMs(100);
i++;
}

void ReadString(const unsigned char *s)
{
int i=0;
while( ReadByte()!='\0')
{
s[i]=ReadByte()
i++;
DelayMs(100);
}
}

Emitters code
Code:

#include <16F877.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "Lcd.c"
#include "USART.c"

#device adc= 10 //sur 10 bits
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)

#byte PortA = 5 // adresse du PORT A
#byte PortB = 6 // adresse du PORT B

long tension;
float val;
char ordre[32];

void main() {

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

set_tris_a(0b00001111);
setup_adc_ports(A_ANALOG);

setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);

InitUART();
InitXbee();

while(1)
{
delay_ms(100);
tension = read_adc();
temp=((float)tension/1023)*5*100;
sprintf(ordre,"%f",temp)//pour convertir de float à string
SendString(ordre);
}
}

receiver code
Code:

#include <16F877.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOPROTECT, PUT, NOLVP
#use rs232 (baud = 9600, xmit = PIN_C6, rcv = PIN_C7)
#include "Lcd.c"

#include "Lcd.c"
#include "USART.c"

void main()
{
char ordre[32];

lcd_init();

InitUART();
InitXbee();

while(1 ){
ReadString(ordre);
printf(lcd_putc,"\f%f C",ordre);
}
}
 

Thank you for your future response.
nina



Joined: 20 Apr 2007
Posts: 111

View user's profile Send private message Send e-mail

xbee
PostPosted: Mon Jul 12, 2010 9:35 am     Reply with quote

becool123

where did you get xbee.c ?

tks

nina
ckielstra



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

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 9:38 am     Reply with quote

Quote:
and I am facing some problems.
It helps if you describe what your problem is. Here we are not good in reading minds (yet).

Code:
#include "Lcd.c"

#include "Lcd.c"
1 time is enough.

Code:
void InitUART(void)
{
TXSTA = 0x20;
RCSTA = 0x90;
SPBRG = 0x06;
}
This will overwrite the settings from the '#use RS232' line. Delete these lines.

Code:
/* Emission d'un octet sur l'UART */
void EmitUART(char c)
{
TXREG = c;
}
/* Reception d'un Octet sur l'UART */
char RecUART (void)
{
return(RCREG);
}
Why not use putc() and getc()? These are more portable and CCS has included some code to handle error flags in the UART.

Code:
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
Add 'ERRORS'. This makes the CCS compiler insert code for clearing error flags in the UART.

Code:
void ReadString(const unsigned char *s)
{
int i=0;
while( ReadByte()!='\0')
{
s[i]=ReadByte()
i++;
DelayMs(100);
}
}
Why the 100ms delay? Characters will arrive at 19200bps ~ 1920 characters / second ~ 0.5ms per character.
The hardware UART has a receive buffer for 3 characters. When receiving more data the UART will stop and only start again when the error status is cleared (that's why I like the CCS functions!).
Remove this delay, and many of the other delays as well.
becool123



Joined: 12 Jul 2010
Posts: 2

View user's profile Send private message

PostPosted: Mon Jul 12, 2010 11:10 am     Reply with quote

thank you really for your response Smile
I corrected some lines in my code thanks to your comments, and you have reason about "USART.c", we can use putc(), puts(), getc() , gets() .

So the problem is in Xbee.c :
In the datasheet of xbee, they talk about AT command, RTS, CTS!!!
Should I use them or I can send I receive like I did?
Thank you
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