|
|
View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
RS232 problem |
Posted: Mon May 07, 2012 6:14 am |
|
|
I want to connect pic18F4431 and 18F2550 through UART. So I connect the pins and here are my programs:
Master:
Code: |
#include <18F2550.h>
#FUSES HS,NOWDT
#use delay(clock=25M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8,parity=N,stop=1,stream = UART1)
void TransmitSymbol(char p)
{
char s;
fputc(p,UART1);
while(!kbhit());
s=fgetc(UART1);
if(s==p)
{
output_high(PIN_C1);
}
}
|
Slave:
Code: |
#include <18F4431.h>
#FUSES HS,NOWDT
#use delay(clock=25M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,bits=8,parity=N,stop=1,stream = UART1)
void Transmit()
{
char p;
int i;
if(kbhit())
{
p=fgetc(UART1);
i=(int)p;
writeint(i,0x80);
fputc(p,UART1);
}
}
|
writeint is a function which writes an integer to my LCD.
And here is the problem the numbers don`t match. When I transmit "5", I have 194(or 195) on the creen. And the number sent back to the master doesn`t macht(the LED doesn`t turn on).
What am I doing wrong???
Thanks![/code] |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Mon May 07, 2012 6:34 am |
|
|
I wouldn't use UART1 as a stream name. That's a reserved name in CCS
now as a UART configuration parameter. If you are using just one RS232
channel I would get rid of the stream completely.
Are you really using a 25MHZ oscillator? (unusual value)
Are you sure TransmitSymbol is actually getting the right value (p)?
Until you get it working I would hardcode P to a value inside TransmitSymbol to
make sure a known char is transmitted.
_________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Mon May 07, 2012 7:54 am |
|
|
Always use 'hard' values while testing ! Too hard to troubleshoot WHERE the problem is otherwise.
Also always add 'errors' to the USES rs232(.....) line,it'll keep the UART from 'hanging'. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19485
|
|
Posted: Mon May 07, 2012 8:00 am |
|
|
Agree about 25MHz. This is such a rare crystal value it is very unlikely. It is also the absolute 'max' allowed by the oscillator in the PIC, so much more prone to problems.
Some other comments:
i=(int)p;
This does absolutely nothing. A character is already an int. Char, int, int8, and byte are all just synonyms for one another in CCS.
Add 'ERRORS' to your RS232 declarations. Repeat 50*, this _must_ be present when using the hardware UART, unless you handle the error conditions yourself. Won't cause a problem here but _essential_ long term.
Without seeing the rest of the code, it is impossible to tell what will happen if multiple characters are seen to arrive at one end, but it is 'suspicious', that the sent character, is (in binary):
10101011001....
While what is displayed is:
10011000110......
Looking rather as if the single bits are being seen as double or even possibly triple bits at one end.
Two zeros, where one should be seen, two ones where one should be seen etc. etc.. I'd say one of the clocks is actually running much slower than the other.
Best Wishes |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Mon May 07, 2012 9:37 am |
|
|
I think the problem is in the oscilator. I`ve just noticed I`m using different quarts on the both chips(20Mhz and 25MHz). I`ll change them and try again.
I want to ask another thing. Is there a way the slave controller to adjust its bit rate to the master???
I`ve seen in books this to be done for motorolla 68XX.
Thanks! |
|
|
|
|
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
|