Getting USART, USART2 and ISRRX to work together

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Mawbert
Posts: 13
Joined: Tue Dec 27, 2011 4:14 pm
Location: USA

Getting USART, USART2 and ISRRX to work together

Post by Mawbert » Thu Jan 05, 2012 5:23 pm

Just a short note to let folk know who also have trouble getting USART.bas, USART2.bas and ISRRX.bas to work together. I noticed that both USART (used by ISSRX) and USART2 had the same public variables names causing variables to be overwritten in an unintended fashion. I renamed the USART2 public variables and that enabled me to use ISRRX and USART2 together.

Code: Select all

{

Edited FOR PIC18F46K22 and with public variables renamed

*****************************************************************************
*  Name    : USART2 Library                                                 *
*  Author  : David John Barker                                              *
*  Notice  : Copyright (c) 2003 Mecanique                                   *
*          : All Rights Reserved                                            *
*  Date    : 21/01/06                                                       *
*  Version : 1.5 Reworked for silicon error                                 *
*  Version : 1.4 Removed USART2_TXIF from WriteItem(Str) and replaced with PIR1, 4 *
*  Version : 1.3 Added support for USART_BRG16                              *
*  Notes   : 1.2 Supports second USART module                               *
*****************************************************************************
}
Module USART2

Include "system.bas"

// calculate clock...
Const FOSC = _clock * 1000000

// low speed or high speed (USART2_BRGH)...
#if IsOption(USART2_BRGH) And Not (USART2_BRGH in (true, false))
   #error USART2_BRGH, "Invalid option. USART2_BRGH must be TRUE or FALSE."
#endif
#option USART2_BRGH = true   
#if USART2_BRGH = true   
Const USART2_MODE = $24
#else
Const USART2_MODE = $20
#endif   

// 8 bit or 16 bit (USART2_BRG16 only supported with EUSART modules)...
#if IsOption(USART2_BRG16) And Not (USART2_BRG16 in (true, false))
   #error USART2_BRG16, "Invalid option. USART2_BRG16 must be TRUE or FALSE."
#endif
#option USART2_BRG16 = false
#if Not USART2_BRG16 And Not USART2_BRGH     // USART2_BRG16 = 0, USART2_BRGH = 0
Const USART2_FMULT = 64
#elseif USART2_BRG16 And Not USART2_BRGH     // USART2_BRG16 = 1, USART2_BRGH = 0
Const USART2_FMULT = 16
#elseif Not USART2_BRG16 And USART2_BRGH     // USART2_BRG16 = 0, USART2_BRGH = 1
Const USART2_FMULT = 16
#else                                      // USART2_BRG16 = 1, USART2_BRGH = 1
Const USART2_FMULT = 4
#endif  

// map registers to USART(x)
#if _usart < 2
   #error _device + " does not support second USART"
  
// MCU has more than one USART...
#else 
Public Dim                         // -> USART2
   #if USART2_BRG16
   USART2_SPBRGRegister As SPBRG2.AsWord,
   USART2_BRG16 As BAUDCON2.3,
   #else
   USART2_SPBRGRegister As SPBRG2,
   #endif
   USART2_RCRegister As RCREG2,           //    as RCREG2
   USART2_TXRegister As TXREG2,           //    as TXREG2
   USART2_RCStatus As RCSTA2,             //    as TXSTA2
   USART2_TXStatus As TXSTA2,             //    as TXSTA2
   USART2_RCInput As TRISD.Booleans(7),   //    changed from TRISG (2)to TRISD (7)
   USART2_TXInput As TRISD.Booleans(6)    //    changed from TRISG (1) to TRISD (6)
#endif
 
Dim                                // -> USART2
   USART2_PIR As PIR3,                    //    as PIR3 
   USART2_PIE As PIE3,                    //    as PIE3
   USART2_IPR As IPR3                     //    as IPR3

// public baudrate constants...
#if USART2_BRG16
Public Const
   USART2_br300 As Word   = FOSC / (USART2_FMULT * (300 + 1)) - 1 + 0.5,
   USART2_br600 As Word   = FOSC / (USART2_FMULT * (600 + 1)) - 1 + 0.5,
   USART2_br1200 As Word  = FOSC / (USART2_FMULT * (1200 + 1)) - 1 + 0.5,
   USART2_br2400 As Word  = FOSC / (USART2_FMULT * (2400 + 1)) - 1 + 0.5,
   USART2_br4800 As Word  = FOSC / (USART2_FMULT * (4800 + 1)) - 1 + 0.5,
   USART2_br9600 As Word  = FOSC / (USART2_FMULT * (9600 + 1)) - 1 + 0.5,
   USART2_br19200 As Word = FOSC / (USART2_FMULT * (19200 + 1)) - 1 + 0.5,
   USART2_br38400 As Word = FOSC / (USART2_FMULT * (38400 + 1)) - 1 + 0.5,
   USART2_br57600 As Word = FOSC / (USART2_FMULT * (57600 + 1)) - 1 + 0.5,
   USART2_br115200 As Word = FOSC / (USART2_FMULT * (115200 + 1)) - 1 + 0.5
#else
Public Const
   USART2_br300 As Byte   = FOSC / (USART2_FMULT * (300 + 1)) - 1 + 0.5,
   USART2_br600 As Byte   = FOSC / (USART2_FMULT * (600 + 1)) - 1 + 0.5,
   USART2_br1200 As Byte  = FOSC / (USART2_FMULT * (1200 + 1)) - 1 + 0.5,
   USART2_br2400 As Byte  = FOSC / (USART2_FMULT * (2400 + 1)) - 1 + 0.5,
   USART2_br4800 As Byte  = FOSC / (USART2_FMULT * (4800 + 1)) - 1 + 0.5,
   USART2_br9600 As Byte  = FOSC / (USART2_FMULT * (9600 + 1)) - 1 + 0.5,
   USART2_br19200 As Byte = FOSC / (USART2_FMULT * (19200 + 1)) - 1 + 0.5,
   USART2_br38400 As Byte = FOSC / (USART2_FMULT * (38400 + 1)) - 1 + 0.5,
   USART2_br57600 As Byte = FOSC / (USART2_FMULT * (57600 + 1)) - 1 + 0.5,
   USART2_br115200 As Byte = FOSC / (USART2_FMULT * (115200 + 1)) - 1 + 0.5
#endif
  
// alias public bitnames to TXSTA(x)...
Public Dim
   USART2_CSRC As USART2_TXStatus.7,
   USART2_TX9 As USART2_TXStatus.6,
   USART2_TXEN As USART2_TXStatus.5,
   USART2_SYNC As USART2_TXStatus.4,
   USART2_BRGH As USART2_TXStatus.2,
   USART2_TRMT As USART2_TXStatus.1,
   USART2_TX9D As USART2_TXStatus.0

// alias public bitnames to RCSTA(x)...
Public Dim
   USART2_SPEN As USART2_RCStatus.7,
   USART2_RX9 As USART2_RCStatus.6,
   USART2_SREN As USART2_RCStatus.5,
   USART2_CREN As USART2_RCStatus.4,
   USART2_ADDEN As USART2_RCStatus.3,
   USART2_FERR As USART2_RCStatus.2,
   USART2_OERR As USART2_RCStatus.1,
   USART2_RX9D As USART2_RCStatus.0
   
// alias public interrupt flags...
Public Dim
   USART2_RCIF As USART2_PIR.5,  // receive buffer full 
   USART2_TXIF As USART2_PIR.4,  // transmit buffer empty 
   USART2_RCIE As USART2_PIE.5,  // receive interrupt enable
   USART2_TXIE As USART2_PIE.4,  // transmit interrupt enable
   USART2_RCIP As USART2_IPR.5,  // receive interrupt priority
   USART2_TXIP As USART2_IPR.4   // transmit interrupt priority
   
// public boolean flags...
Public Dim  
   USART2_DataAvailable As USART2_PIR.Booleans(5),         // USART2_RCIF
   USART2_ReadyToSend As USART2_PIR.Booleans(4),           // USART2_TXIF
   USART2_ContinousReceive As USART2_RCStatus.Booleans(4), // USART2_CREN
   USART2_Overrun As USART2_RCStatus.Booleans(1),          // USART2_OERR
   USART2_FrameError As USART2_RCStatus.Booleans(2),       // USART2_FERR
   USART2_RCIEnable As USART2_PIE.Booleans(5),             // USART2_RCIE
   USART2_TXIEnable As USART2_PIE.Booleans(4),             // USART2_TXIE
   RCIPHigh As USART2_IPR.Booleans(5),              // USART2_RCIP
   USART2_TXIPHigh As USART2_IPR.Booleans(4)               // USART2_TXIP

Public Dim
   USART2_ReadTerminator As Char                    // read string terminator
{
****************************************************************************
* Name    : SetBaudrate                                                    *
* Purpose : Sets the hardware USART baudrate                               *
*         : Pass SPBRG constant, as defined above. For example, USART2_br115200   *
****************************************************************************
}
Public Sub SetBaudrate(pSPBRG As USART2_SPBRGRegister = USART2_br19200)
   USART2_RCStatus = $90         // serial port enable, continuous receive
   USART2_RCInput = true         // receive pin is input
   USART2_TXInput = false        // transmit pin is output
   USART2_TXStatus = USART2_MODE  // high or low speed
   #if USART2_BRG16
   USART2_BRG16 = 1
   #endif
End Sub
{
****************************************************************************
* Name    : ClearOverrun                                                   *
* Purpose : Clear USART USART2_Overrun error by resetting receive flag            *
****************************************************************************
}
Public Sub ClearOverrun()
   If USART2_Overrun Then
      USART2_CREN = 0  // disable continuous receive
      USART2_CREN = 1  // enable continous receive
   EndIf   
End Sub
{
****************************************************************************
* Name    : ReadByte                                                       *
* Purpose : Read a byte from the hardware USART                            *
*         : Waits for USART data, return byte in RCREG(x)                  *
****************************************************************************
}
Public Function ReadByte() As USART2_RCRegister
   Repeat
      ClrWDT
   Until USART2_DataAvailable
End Function
{
****************************************************************************
* Name    : ReadBoolean                                                    *
* Purpose : Read a boolean from the hardware USART                         *
****************************************************************************
}
Public Function ReadBoolean() As Boolean
   Result = Boolean(ReadByte)
End Function 
{
****************************************************************************
* Name    : ReadWord                                                       *
* Purpose : Read a word from the hardware USART                            *
****************************************************************************
}
Public Function ReadWord() As Word
   Result.Bytes(0) = ReadByte
   Result.Bytes(1) = ReadByte
End Function  
{
****************************************************************************
* Name    : ReadLongWord                                                   *
* Purpose : Read a long word from the hardware USART                       *
****************************************************************************
}
Public Function ReadLongWord() As LongWord
   Result.Bytes(0) = ReadByte
   Result.Bytes(1) = ReadByte
   Result.Bytes(2) = ReadByte
   Result.Bytes(3) = ReadByte
End Function  
{
****************************************************************************
* Name    : ReadFloat                                                      *
* Purpose : Read a float from the hardware USART                           *
****************************************************************************
}
Public Function ReadFloat() As Float
   Result.Bytes(0) = ReadByte
   Result.Bytes(1) = ReadByte
   Result.Bytes(2) = ReadByte
   Result.Bytes(3) = ReadByte
End Function 
{
****************************************************************************
* Name    : WriteByte                                                      *
* Purpose : Write a byte value to the hardware USART                       *
*         : Wait until ready to send is enabled, then send WREG byte       *
****************************************************************************
}
Public Sub WriteByte(pValue As WREG)
   Repeat
      ClrWDT
   Until USART2_ReadyToSend
   USART2_TXRegister = WREG
End Sub
{
****************************************************************************
* Name    : WriteBoolean                                                   *
* Purpose : Write a boolean value to the hardware USART                    *
****************************************************************************
}
Public Sub WriteBoolean(pValue As Boolean)
   WriteByte(Byte(pValue))
End Sub
{
****************************************************************************
* Name    : WriteWord                                                      *
* Purpose : Write a word value to the hardware USART                       *
****************************************************************************
}
Public Sub WriteWord(pValue As Word)
   WriteByte(pValue.Bytes(0))
   WriteByte(pValue.Bytes(1))
End Sub
{
****************************************************************************
* Name    : WriteLongWord                                                  *
* Purpose : Write a word value to the hardware USART                       *
****************************************************************************
}
Public Sub WriteLongWord(pValue As LongWord) 
   WriteByte(pValue.Bytes(0))
   WriteByte(pValue.Bytes(1))
   WriteByte(pValue.Bytes(2))
   WriteByte(pValue.Bytes(3))
End Sub
{
****************************************************************************
* Name    : WriteFloat                                                     *
* Purpose : Write a floating point number to the hardware USART            *
****************************************************************************
}
Public Sub WriteFloat(pValue As Float) 
   WriteByte(pValue.Bytes(0))
   WriteByte(pValue.Bytes(1))
   WriteByte(pValue.Bytes(2))
   WriteByte(pValue.Bytes(3))
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a boolean from the hardware USART                         *
****************************************************************************
}
Sub ReadItem(ByRef pValue As Boolean)
   pValue = Boolean(ReadByte)
End Sub 
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a byte from the hardware USART                            *
****************************************************************************
}
Sub ReadItem(ByRef pValue As Byte)
   pValue = ReadByte
End Sub  
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a shortint from the hardware USART                        *
****************************************************************************
}
Sub ReadItem(ByRef pValue As ShortInt)
   pValue = ReadByte
End Sub  
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a word from the hardware USART                            *
****************************************************************************
}
Sub ReadItem(ByRef pValue As Word)
   pValue = ReadWord
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read an integer from the hardware USART                        *
****************************************************************************
}
Sub ReadItem(ByRef pValue As Integer)
   pValue = ReadWord
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a long word from the hardware USART                       *
****************************************************************************
}
Sub ReadItem(ByRef pValue As LongWord)
   pValue = ReadLongWord
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a long integer from the hardware USART                    *
****************************************************************************
}
Sub ReadItem(ByRef pValue As LongInt)
   pValue = ReadLongWord
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a floating point number from the hardware USART           *
****************************************************************************
}
Sub ReadItem(ByRef pValue As Float) 
   pValue = ReadFloat
End Sub
{
****************************************************************************
* Name    : ReadItem (OVERLOAD)                                            *
* Purpose : Read a string from the hardware USART. Use USART2_ReadTerminator      *
*         : to specify the input string terminator character.              *
*         : 1.5 Reworked for silicon error                                 *
****************************************************************************
}
Sub ReadItem(ByRef pText As String)
   Dim TextPtr As POSTINC0
   Dim TextAddr As FSR0
   Dim Value As Byte
   
   TextAddr = AddressOf(pText)
   Value = ReadByte
   While Value <> Byte(USART2_ReadTerminator)
      TextPtr = Value
      Value = ReadByte
   Wend
   TextPtr = null
End Sub
{
****************************************************************************
* Name    : Read (COMPOUND)                                                *
* Purpose : Read an item from the hardware USART                           *
****************************************************************************
}
Public Compound Sub Read(ReadItem)
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a boolean value to the hardware USART                    *
****************************************************************************
}
Sub WriteItem(pValue As Boolean)
   WriteByte(Byte(pValue))
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a byte value to the hardware USART                       *
****************************************************************************
}
Sub WriteItem(pValue As WREG)
   WriteByte(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a short int value to the hardware USART                  *
****************************************************************************
}
Sub WriteItem(pValue As ShortInt)
   WriteByte(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a word value to the hardware USART                       *
****************************************************************************
}
Sub WriteItem(pValue As Word) 
   WriteWord(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write an integer value to the hardware USART                   *
****************************************************************************
}
Sub WriteItem(pValue As Integer) 
   WriteWord(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a long word to the hardware USART                        *
****************************************************************************
}
Sub WriteItem(pValue As LongWord) 
   WriteLongWord(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a long integer to the hardware USART                     *
****************************************************************************
}
Sub WriteItem(pValue As LongInt) 
   WriteLongWord(pValue)
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a floating point number to the hardware USART            *
****************************************************************************
}
Sub WriteItem(pValue As Float) 
   WriteByte(pValue.Bytes(0))
   WriteByte(pValue.Bytes(1))
   WriteByte(pValue.Bytes(2))
   WriteByte(pValue.Bytes(3))
End Sub
{
****************************************************************************
* Name    : WriteItem (OVERLOAD)                                           *
* Purpose : Write a string value to the hardware USART                     *
****************************************************************************
}
Sub WriteItem(pText As String)
   FSR0 = AddressOf(pText)
   #if WDT
   Asm
     movf  POSTINC0, W
     bz    $ + 12  
     ClrWDT
     btfss USART2_PIR, 4
     bra   $ - 4
     movwf USART2_TXRegister
     bra   $ - 12
   End Asm
   #else
   Asm
     movf  POSTINC0, W
     bz    $ + 10  
     btfss USART2_PIR, 4
     bra   $ - 2
     movwf USART2_TXRegister
     bra   $ - 10
  End Asm
  #endif
End Sub
{
****************************************************************************
* Name    : Write (COMPOUND)                                               *
* Purpose : Write an item to the hardware USART                            *
****************************************************************************
}
Public Compound Sub Write(WriteItem)
{
****************************************************************************
* Name    : WaitFor                                                        *
* Purpose : Wait for a byte value to be received                           *
****************************************************************************
}
Public Function WaitFor(pValue As Byte) As Boolean
   result = ReadByte = pValue
End Function
{
****************************************************************************
* Name    : WaitForTimeout                                                 *
* Purpose : Wait for a byte value to be received, with timeout value in    *
*         : milliseconds                                                   *
****************************************************************************
}
Public Function WaitForTimeout(pValue As Byte, pTimeout As Word) As Boolean
   Dim Counter As Byte
   Dim Timeout As Word

   ClearOverrun
   Result = false
   Counter = 10
   Repeat
      Timeout = pTimeout
      While Timeout > 0
         If USART2_DataAvailable And (USART2_RCRegister = pValue) Then
            Result = true
            Exit
         EndIf   
         DelayUS(100)
         Dec(Timeout)
      Wend
      Dec(Counter)
   Until Counter = 0
End Function
{
****************************************************************************
* Name    : WaitForCount                                                   *
* Purpose : Wait for pAmount bytes to be received. The incoming data is    *
*         : stored in pArray                                               *
****************************************************************************
}
Public Sub WaitForCount(ByRef pArray() As Byte, pCount As Word)
   Dim Index As Word
   ClearOverrun
   Index = 0
   While pCount > 0
      pArray(Index) = ReadByte
      Dec(pCount)
      Inc(Index)
   Wend
End Sub
{
****************************************************************************
* Name    : WaitForStr                                                     *
* Purpose : Wait for a string to be received                               *
****************************************************************************
}
Public Sub WaitForStr(pStr As String)
   Dim Index As Byte

   ClearOverrun
   Index = 0
   While pStr(Index) <> null
      If WaitFor(pStr(Index)) Then
         Inc(Index)
      Else
         Index = 0
      EndIf
   Wend
End Sub
{
****************************************************************************
* Name    : WaitForStrCount                                                *
* Purpose : Wait for pAmount characters to be received. The incoming data  *
*         : is stored in pStr                                              *
****************************************************************************
}
Public Sub WaitForStrCount(ByRef pStr As String, pCount As Word)
   Dim Index As Byte
   ClearOverrun
   Index = 0
   While pCount > 0
      pStr(Index) = ReadByte
      Dec(pCount)
      Inc(Index)
   Wend
   pStr(Index) = null
End Sub
{
****************************************************************************
* Name    : WaitForStrTimeout                                              *
* Purpose : Wait for a string to be received, with timeout value in        *
*         : milliseconds                                                   *
****************************************************************************
}
Public Function WaitForStrTimeout(pStr As String, pTimeout As Word) As Boolean
   Dim Counter, StrIndex As Byte
   Dim Timeout As Word
   Dim Ch As Byte

   ClearOverrun
   Result = false
   StrIndex = 0
   Counter = 10
   Repeat
      Timeout = pTimeout
      While Timeout > 0
         Ch = pStr(StrIndex)
         If Ch = 0 Then
            result = true
            Exit
         ElseIf USART2_DataAvailable Then
            If Ch = USART2_RCRegister Then
               Inc(StrIndex)
            Else
               StrIndex = 0
            EndIf   
         EndIf
         DelayUS(100)
         Dec(Timeout)
      Wend
      Dec(Counter)
   Until Counter = 0
End Function
{
****************************************************************************
* Name    : DataAvailableTimeout                                           *
* Purpose : Checks to see if a byte value has been received, with          *
*         : timeout in milliseconds                                        *
****************************************************************************
}
Public Function DataAvailableTimeout(pTimeout As Word) As Boolean
   Dim Counter As Byte
   Dim Timeout As Word

   ClearOverrun
   Counter = 10
   Repeat
      Timeout = pTimeout
      While (Timeout > 0) And Not USART2_DataAvailable
         DelayUS(100)
         Dec(Timeout)
      Wend
      Dec(Counter)
   Until (Counter = 0) Or USART2_DataAvailable
   result = USART2_DataAvailable   
End Function
{
****************************************************************************
* Name    : Rep                                                            *
* Purpose : Write a byte value, pAmount times                              *
****************************************************************************
}
Public Sub Rep(pValue, pAmount As Byte)
   While pAmount > 0
      WriteByte(pValue)
      Dec(pAmount)
   Wend   
End Sub
{
****************************************************************************
* Name    : Skip                                                           *
* Purpose : Read pAmount bytes                                             *
****************************************************************************
}
Public Sub Skip(pAmount As Byte)
   Dim Value As Byte
   While pAmount > 0
      Value = ReadByte
      Dec(pAmount)
   Wend   
End Sub

// module initialisation...
USART2_ReadTerminator = null 
Beginner

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Thu Jan 05, 2012 5:48 pm

Normally in situations like this you would just use the module name to qualify which public variable you want to access, like USART.DataAvailable or USART2.DataAvailable.

I've never used all three modules together. Is there something in ISRRX that would keep this from working? It only includes USART.bas, so it shouldn't see any of the USART2.bas module definitions.

Mawbert
Posts: 13
Joined: Tue Dec 27, 2011 4:14 pm
Location: USA

Post by Mawbert » Thu Jan 05, 2012 6:33 pm

My apologies. I seem to be trying to confuse others as well as myself. I seemed to have changed something else along the way. You do not have to change USART2 it seems even though it seems to overwrite USART's variables.

i.e. with the standard USART2

Code: Select all

BaudSetting.byte0 = SPBRG1
BaudSetting.byte1 = SPBRGH1

USART2.Write(13,13)
USART2.Write("TXSTA1   = ",BinToStr(TXSTA1),13)
USART2.Write("RCSTA1   = ",BinToStr(RCSTA1),13)
USART2.Write("BAUDCON1 = ",BinToStr(BAUDCON1),13)
USART2.Write("SPBRGH1 = ",BinToStr(SPBRGH1),13)
USART2.Write("SPBRG1 = ",BinToStr(SPBRG1),13)
USART2.Write("SPBRGH1:SPBRG1 = ",DecToStr(BaudSetting),13)
USART2.Write("SPBRGRegister = ",DecToStr(SPBRGRegister),13)
USART2.Write("br9600 ",DecToStr(br9600),13)
give:

TXSTA1 = 100010
RCSTA1 = 10010000
BAUDCON1 = 1101000
SPBRGH1 = 0
SPBRG1 = 1100111
SPBRGH1:SPBRG1 = 103
SPBRGRegister = 8 (Not = to 103 as expected)
br9600 103

Without my changes SPBRGRegister was not equal to SPBRGH1:SPBRG1 using the unmodified USART2 because USART2 was overwriting it.

With my modified USART2:

TXSTA1 = 100010
RCSTA1 = 10010000
BAUDCON1 = 1101000
SPBRGH1 = 0
SPBRG1 = 1100111
SPBRGH1:SPBRG1 = 103
SPBRGRegister = 103
br9600 103

I assumed that SPBRGRegister should be equal to SPBRGH1:SPBRG1 but being a novice I do not understand why this does not matter.

Sorry if I confused others too.
Beginner

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Thu Jan 05, 2012 6:54 pm

That's because when you say

Code: Select all

USART2.Write("SPBRGRegister = ",DecToStr(SPBRGRegister),13) 
you're not specifying WHICH 'SPBRGRegister' declaration to write, so it picks the first one INCLUDEd, which in this case I'm betting is USART.bas. You should qualify the variable accesses with the module name so that the compiler knows which one you mean.

Code: Select all

USART2.Write("SPBRGRegister = ",DecToStr(USART2.SPBRGRegister),13) 
will do what you want.

Even though the modules use the same variable names, they are NOT the same, so they aren't getting overwritten. Read up on public, private, and scope in the manual.

Mawbert
Posts: 13
Joined: Tue Dec 27, 2011 4:14 pm
Location: USA

Post by Mawbert » Thu Jan 05, 2012 7:49 pm

Thanks. That helps. I will be a bit more careful before I offer uneducated input in the future.
Beginner

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Thu Jan 05, 2012 8:23 pm

Don't worry about it. You're not the first.

With it's structured nature, Swordfish is a bit different than most flavors of basic you're probably familiar with, so it can take a little getting use to.

Post Reply