Help me with RS232 comms.

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Help me with RS232 comms.

Post by Francesco.C » Sat May 02, 2009 6:28 pm

Hi guys,
I need my PC to communicate with the PIC using RS232.

I understand well the hardware set-up, but I am a little lost with regard to the software.

I had a look in the software library and did not find an example to start me going.

Can anyone show me, to start with, a simple example to send a byte to my PC. I will use 'Terminal' to receive it.

Thanks in advance.

Francesco C

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Sat May 02, 2009 8:22 pm

Hi Francesco.C,

What pic are you using and do you want to use soft usart or hardware usart?
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Sat May 02, 2009 9:39 pm

If you are using the USART module the setup is very simple.

Code: Select all

    Include "usart.bas"
    
   USART.Write("HELLO WORLD",13)

Sorry do not know how to use the software module.

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Sat May 02, 2009 9:48 pm

Thanks for the replay guys,

I am using a PIC18F2553. It has USART facility built-in.

Regards

Francesco C

gramo
Registered User
Registered User
Posts: 200
Joined: Tue Mar 20, 2007 6:55 am
Location: Australia
Contact:

Post by gramo » Sun May 03, 2009 1:13 pm

If your using a PICKit 2, then USART coms is pretty easy..


If not, then something like a MAX232 or DS275 will allow interfacing with your PC from the PIC, here's a max232 circuit;

Image

Example program;

Code: Select all

Device = 18F458
Clock = 20
 
Include "usart.bas"
Include "convert.bas"
 
Dim Variable_1 As Byte
 
 
// Start Of Program...
Variable_1 = 242
SetBaudrate(br19200)
 
USART.Write("Hello World", 13, 10)                          // string
USART.Write("Binary : ", BinToStr(Variable_1), 13, 10)      // binary number
USART.Write("Hex : ", HexToStr(Variable_1), 13, 10)         // hex number
USART.Write("Decimal : ", DecToStr(Variable_1), 13, 10)     // decimal number
Hyperterminal Output;
Image


Not sure if that's what your chasing, but more info can be found hereeither way
digital-diy.com - Hobby microcontroller projects and tutorials. Assembly, PICBasic and C examples.

Australian distributor for the Swordfish Compiler

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Sun May 03, 2009 9:00 pm

Hi,
Thank you for the help provided so far.
I was not aware of the large ammount of info available on the subject.

However I have not managed to make the comms work.

This is the very simple program I chose to make things happen.

#####################
Device = 18F2553
Clock = 20

Include "usart.bas"
SetBaudrate(br19200)

again:
USART.Write("HELLO WORLD",13)
DelayMS(200)
GoTo again
#########################

All I get is 'D2H' .......

My hardware set-up is on a PCB with a PIC18F255 connected to a MAX232
and to my computer comm port1.

I tryed to change the baud rate with no luck.

I replaced the MAX232, no luck again.

I know the PIC is working as I have been using it until I decided to start this project.

Am I missing something silly from the code?

I would appreciate some more input from you guys.

Thanks again.

Francesco C

CharlieM
Registered User
Registered User
Posts: 211
Joined: Wed Oct 10, 2007 12:05 am

Post by CharlieM » Sun May 03, 2009 9:36 pm

You may have to check the datsheet to see if there is anyting else on port C that you need to disable through the config fuses.
Running version 2.2.4.0 ICC 1.2.1.0
Regards CharlieM

Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

Post by Doj » Sun May 03, 2009 10:23 pm

What happens if you do this:-

Code: Select all

Device = 18F2553 
Clock = 20

Include "usart.bas"

while 1=1

USART.SetBaudrate(br19200)
USART.Write("HELLO WORLD",13)
DelayMS(200)

wend


Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Thu May 07, 2009 7:38 pm

Thank a lot for the hints guys, program is working now!

I have found the fault was due to a floating ground signal (bad solder). :oops:

Regards

Francesco C

Post Reply