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

PIC to PIC communication problem

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



Joined: 14 May 2013
Posts: 8

View user's profile Send private message

PIC to PIC communication problem
PostPosted: Mon May 20, 2013 9:25 pm     Reply with quote

Hi all,

I want to setup a RF communication between my two PIC18F4550.
I followed the recommendation from this forum,setup a wire communication between 2 PICs first.
TX will send "1" to RX and RX will display the result in UART TOOL.
The TX side PIN_C6 is connected to RX side PIN_C7.
Here is the simple code for testing which is not working.
I'm using the demo ccs compiler version.

TX
Code:

#include <18F4550.h>
#fuses hs,NOWDT,put,brownout,nolvp,CPUDIV1 //add in CPUDIV1 output freq = 20MHz
#use delay(clock=20000000)
#use rs232(stream=transmitter,baud=9600,xmit=PIN_C6,ERRORS,UART1)

void main()
{
   while(1)
   {
      fprintf(transmitter,"1");
      delay_ms(50);
   }
}


RX
Code:

#include <18F4550.h>
#fuses hs,NOWDT,put,nobrownout,nolvp,CPUDIV1 //add in CPUDIV1 output freq = 20MHz
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B6)
#use rs232(stream=receiver,baud=9600,rcv=PIN_C7,ERRORS,UART1)

void main()
{
      int value;
      delay_ms(1000); //add in some delay in order to communicate with pickit2
      value = fgetc(receiver);
      printf("%d", value);
      printf("\n");
while(1);// use while loop
}


Can anyone give me some clue? thank you so much! Very Happy
ckielstra



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

View user's profile Send private message

PostPosted: Tue May 21, 2013 2:03 am     Reply with quote

Code:
#use rs232(stream=transmitter,baud=9600,xmit=PIN_C6,ERRORS,UART1)

When you specify 'UART1' you are not supposed to also specify the pin numbers. Combining both methods for the pin numbers might confuse the compiler. Up to your personal preference, change to:
Code:
#use rs232(stream=transmitter,baud=9600,ERRORS,UART1)
or
Code:
#use rs232(stream=transmitter,baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)


Then, there are two problems in your receiver:
1) The code is only executed one time as the 'while' command is used wrong.
2) You are transmitting a character every 500ms but in the receiver you have a delay of 1000ms. This means you will loose 1 out of 2 characters. I don't think you need a delay in transmitting data to the PicKit so easiest is to delete that line.

Change the receiver to:
Code:
void main()
{
   int value;
   while(1)
   {
      value = fgetc(receiver);
      printf("%d", value);
      printf("\n");
   }
}
ezflyr



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

View user's profile Send private message

PostPosted: Tue May 21, 2013 6:17 am     Reply with quote

Hi,

One additional note about your #use rs232 defines. If you decide to specify pin numbers, be sure to specify both a receive pin and a transmit pin regardless of whether you intend to use both. If you don't do this, the compiler will generate code for a software UART even when you use hardware UART pins.

Another potential problem is where your output is actually going in your receive program? You define a single 'stream' in your receive code for the incoming data, and then use 'printf' to show the result. Where is this data going? Perhaps you've got some terminal window setup, and the data is being redirected or something (I don't use these methods, so I'm speculating), just be sure you know where the data is going, and you test whatever method you've got!

John
redlion



Joined: 14 May 2013
Posts: 8

View user's profile Send private message

PostPosted: Tue May 21, 2013 8:26 pm     Reply with quote

thanks ckielstra & ezflyr for your replies Very Happy Very Happy
I have modified my coding base on your suggestion but still not working
I need to add some delay in my receiver side in order to display the proper result from pickit2 UART TOOL (Im using the UART TOOL to display my result)

here is my updated code
RX
Code:

#include <18F4550.h>
#fuses hs,NOWDT,put,nobrownout,nolvp,CPUDIV1 //add in CPUDIV1 output freq = 20MHz
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B6)
#use rs232(stream=receiver,baud=9600,rcv=PIN_C7,xmit=PIN_C6,ERRORS)

void main()
{
      int value;     
      delay_ms(1000); //add in some delay in order to communicate with pickit2
      while(1)
      {
      value = fgetc(receiver);
      printf("%d", value);
      printf("\n");
      }
}


TX
Code:

#include <18F4550.h>
#fuses hs,NOWDT,put,brownout,nolvp,CPUDIV1 //add in CPUDIV1 output freq = 20MHz
#use delay(clock=20000000)
#use rs232(stream=transmitter,baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)

void main()
{
   delay_ms(1000);
   while(1)
   {
      fprintf(transmitter,"1");
      delay_ms(50);
   }
}


anything wrong?

best regards,
redlion
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue May 21, 2013 10:48 pm     Reply with quote

One thing I usually do on any circuit (your transmitter in this case) is add a routine to print the firmware version or some other welcome message before going into the main transmit loop for example. That way, I have a clue that I have at least made it that far. You don't indicate what tools you have available, but a scope is nice and would allow you to look at the transmit and receive lines for signs of activity (or even a led with a limiting resistor on the transmit lines so you can see when things get sent although at 9600, it is only a brief flicker). The more "test points" you can put in your hardware and software, the more likely you are to find where the problem is.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
ckielstra



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

View user's profile Send private message

PostPosted: Wed May 22, 2013 1:12 am     Reply with quote

You say you are using the Demo version of the compiler but can you also post the version number? It looks like 4.xxx and can be found at the top of the generated list file (*.lst) or through the Windows Start/PICC menu item.

Do a simple blinking LED test on both boards. Have the LED blink for 1 second on/off in a loop, this proves the board is running and at the expected frequency (it is easy to be running a factor 2 off).

The boards are connected through a wire. Silly question, but have you double checked this is connected right, i.e. C6 of the transmitter to C7 of the receiver (a cross connection, not straight)?

How long is the wire between the boards? As you are running TTL voltage levels this should be kept short, maximum about 1 meter.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed May 22, 2013 3:27 am     Reply with quote

and (of course), remember you need the ground connection between the boards as well.

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9240
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed May 22, 2013 4:56 am     Reply with quote

Your TX PIC code has a 50 millisecond delay between sends..it would be better to delay by 500ms or 1000ms to help debug.
Also add a simple LED /resistor to another I/O Pin and 'toggle' it in the loop. That way you have a visual indication your TX program is running.

hth
jay
redlion



Joined: 14 May 2013
Posts: 8

View user's profile Send private message

PostPosted: Wed May 22, 2013 9:30 pm     Reply with quote

Thank you all! gpsmikey,ckielstra,Ttelmah,temtronic for your troubleshooting techiques Very Happy Very Happy

I used them to troubleshoot my code, after made some changes to my code,finally the 2 pics can communicate! Very Happy

but there's only one problem.
In the transmitter im sending character "1" or "A"
In the receiver Im receiving "49" or "65" which is in decimal not character format. erm...do I need any converter to convert it? Im abit lost Rolling Eyes

Thank you so much!!

best regards,
redlion
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 22, 2013 9:37 pm     Reply with quote

Look at the section on printf in the CCS manual. It lists format specifiers
to display the data in several formats.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
redlion



Joined: 14 May 2013
Posts: 8

View user's profile Send private message

PostPosted: Wed May 22, 2013 10:05 pm     Reply with quote

Thank you PCM programmer! I know what my problem is now. Its working now. Thank you so much! Now I am going to build my RF communication Very Happy
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