USART and SUART in same program?

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

USART and SUART in same program?

Post by garryp4 » Sat Jun 02, 2007 7:38 pm

I need to use both the hardware UART and a software UART in the same program but can not get them both to work. If I comment out the software UART include and associated code,the begining serial out works. But just uncommenting the 'include "suart.bas"' even with out using it prevents the message from the hardware UART. I know the program is running as the LED's flash. Any help?


Device = 18F4620
Clock = 20

Include "suart.bas"
Include "usart.bas"
Include "convert.bas"
Include "string.bas"


' Declare Variables
Dim red As PORTE.2 ' Red LED
Dim green As PORTE.1 ' Green lED
Dim depth As Word


' Establish Subroutines
Sub RED_LED(delay As Word)
High (red)
DelayMS (delay)
Low (red)
End Sub

Sub GREEN_LED(delay As Word)
High (green)
DelayMS (delay)
Low (green)
End Sub

Function sonar_depth(pdepth As Byte) As Word
Dim snr_str As String
Dim cmpr_str As String
Dim Value As Char
Dim h_byte As Byte
Dim l_byte As Byte
cmpr_str = "BBT,"
snr_str = "0000"

While cmpr_str <> snr_str
Right(snr_str, 3)
UART.Read(Value)
snr_str = snr_str + Value
If snr_str = cmpr_str Then
UART.Read(h_byte)
UART.Read(l_byte)
pdepth = (h_byte * 10) + l_byte
Break
EndIf
Wend
End Function

' Set Serial Port Spec's
SetTX(PORTD.5)
SetRX(PORTD.6)
SetBaudrate(sbr4800)
SetMode(umInverted)

SetBaudrate(br9600)

' Populate Array's and variables

depth = 0

' Do Stuff
RED_LED(400)
GREEN_LED(400)

USART.Write("I'M ALIVE ",13,10)

'---------------------------------------------------------------------------
MAIN:

sonar_depth(0)
USART.Write("Value : ", DecToStr(depth), 13, 10)
GREEN_LED(200)

GoTo MAIN

'---------------------------------------------------------------------------

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sat Jun 02, 2007 10:20 pm

Give preference to USART for RX. USART has a 2-byte Receive Buffer and can work in the background. Try to relegate UART for TX only.

garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Sun Jun 03, 2007 2:40 am

I do not understand what you mean. The hardware has the Pic USART wired to a DB9 to talk to the outside world. The software UART is collecting serial data from a NEMA sonar. I only have 1 hardware USART available. Pretty much I'm trying to copy what I've been doing in PBP.

I have followed a previous string about timeouts for the software USART but did not find any help (a lot more questions). I am not too concerned about a timeout for this application yet as the string is pretty much guarnteed to come through every second. The question was about using the hardware and software UARTS in the same program. So far I have been able to use either, but not both. Any help?

Thanks for your time and the extra PIC with the order.

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Jun 03, 2007 4:06 am

I took this up in another thread about SUART RX which might help explain it best:

http://www.sfcompiler.co.uk/forum/viewt ... =1203#1203

garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Sun Jun 03, 2007 4:47 am

Thanks, but that is part of the string I have already read through. I fully understand that a software UART is just that, software. It's all about timing and a pin state.

So, back to the question. Why does the hardware USART not work when I Include "SUART.bas"? I can comment out the SUART lines and the hardware USART works fine. If I just Include "SUART.bas" with out any other software USART code, the hardware USARTwill no longer send to hyperterm as it had been. What part of the picture am I missing? Is there an example out there that uses both? I've only had this for a few days and am totaly stuck.

Thanks.

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Jun 03, 2007 7:51 am

It may not be the full solution, but you have used SetBaudrate twice, once for each module. Since this sub is valid for both modules, you must precede it with the module name, e.g. UART.SetBaudrate or USART.SetBaudrate. As soon as you inlcude the "SUART.bas" module, it picks up the SetBaudrate for the USART, I think because the include for the "SUART.bas" appears before that for "USART.bas" in your code. You are simply not setting your baudrate for the USART module as a result.

Hope this helps.

Steve

P.S. To be on the safe side, I always include the module name before every sub or function call as a matter of habit.

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Sun Jun 03, 2007 8:09 am

The reason for your problem is that your using a generic command
SetBaudrate(br9600)

And as "suart.bas" is the first module in the list your doing the serial usart setting not the hardware.

Just swapping the order of the includes will ensure the right SetBaudrate routine is called

edit
Opps to late

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Jun 03, 2007 8:47 am

Swapping the order of the includes would surely mean that the other one would no longer work?

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Sun Jun 03, 2007 8:56 am

His problem was that he found with out using any soft usart commands the hardware failed to work. I was just explaining why.

The real answer is to address the SetBaudrate(br9600) to the correct module

USART.SetBaudrate(br9600)
UART.SetBaudrate(br9600)

garryp4
Posts: 126
Joined: Mon May 21, 2007 7:18 am
Location: Loveland, CO USA

Post by garryp4 » Mon Jun 04, 2007 8:01 pm

Yeah, that took care of it. I was assuming (I know, you and me!) that the USART was using br9600 and the SUART was using sbr9600, two different instructions. I will take your advice and include the module name in the instruction. Thanks.

User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Post by octal » Tue Jun 05, 2007 7:52 am

Hi,
As a rule I never assume anything. Swordfish Basic is modular and most of what we can develop today can become someday a module. So as a rule I personnaly write all modules functions using the module name before the function. This also (with private/public modifiers) gives a somewhat Object Oriented aspect to swordfish programming.

Regards
Octal

Post Reply