|
|
View previous topic :: View next topic |
Author |
Message |
redlion
Joined: 14 May 2013 Posts: 8
|
PIC to PIC communication problem |
Posted: Mon May 20, 2013 9:25 pm |
|
|
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! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 21, 2013 2:03 am |
|
|
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
|
|
Posted: Tue May 21, 2013 6:17 am |
|
|
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
|
|
Posted: Tue May 21, 2013 8:26 pm |
|
|
thanks ckielstra & ezflyr for your replies
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
|
|
Posted: Tue May 21, 2013 10:48 pm |
|
|
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
|
|
Posted: Wed May 22, 2013 1:12 am |
|
|
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: 19545
|
|
Posted: Wed May 22, 2013 3:27 am |
|
|
and (of course), remember you need the ground connection between the boards as well.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed May 22, 2013 4:56 am |
|
|
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
|
|
Posted: Wed May 22, 2013 9:30 pm |
|
|
Thank you all! gpsmikey,ckielstra,Ttelmah,temtronic for your troubleshooting techiques
I used them to troubleshoot my code, after made some changes to my code,finally the 2 pics can communicate!
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
Thank you so much!!
best regards,
redlion |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
redlion
Joined: 14 May 2013 Posts: 8
|
|
Posted: Wed May 22, 2013 10:05 pm |
|
|
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 |
|
|
|
|
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
|