USART and SUART in same program?
Moderators: David Barker, Jerry Messina
USART and SUART in same program?
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
'---------------------------------------------------------------------------
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
'---------------------------------------------------------------------------
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.
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.
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
http://www.sfcompiler.co.uk/forum/viewt ... =1203#1203
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.
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.
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.
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.
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
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