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

RS232 problem

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



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

RS232 problem
PostPosted: Mon May 07, 2012 6:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 07, 2012 6:34 am     Reply with quote

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.

Code:
  p='5';
  fputc(p);

_________________
Google and Forum Search are some of your best tools!!!!
temtronic



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

View user's profile Send private message

PostPosted: Mon May 07, 2012 7:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 07, 2012 8:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 07, 2012 9:37 am     Reply with quote

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!
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