Menu

Alternate Software RS232 Header File SoftSerial.h

kent_twt4
2015-03-14
2015-03-14
  • kent_twt4

    kent_twt4 - 2015-03-14

    This is an update my previous software serial implementation. It has been updated to use commands similar to the internal rs232.h by adding an "X" (e.g. SerPrintX, and so forth). The SoftSerial.h. file should be placed in the GreatCowBasic/include folder (not lowlevel folder!!!) and be included like so in the program code.

    #chip 12f1822, 4
    #include <SoftSerialX.h>
    

    The table at the top of the header file explains the commands, syntax, and the constants required. The baudrates were established in a trial and error manner, and slight adjustments (up/down) to the BaudDelay's may be required in certain conditions.

    See next post for the file, make sure the text file is saved with .h suffix.

    Edit: included Hugh in credits of SoftSerialX.h as many routines derived (stolen) from GCB, thanks

     

    Last edit: kent_twt4 2015-03-14
  • kent_twt4

    kent_twt4 - 2015-03-14
    '    Alternate software serial routines for the Great Cow BASIC compiler
    '    Copyright (C) 2015 Kent Schafer, Hugh Considine
    
    '    This library is free software; you can redistribute it and/or
    '    modify it under the terms of the GNU Lesser General Public
    '    License as published by the Free Software Foundation; either
    '    version 2.1 of the License, or (at your option) any later version.
    
    '    This library is distributed in the hope that it will be useful,
    '    but WITHOUT ANY WARRANTY; without even the implied warranty of
    '    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    '    Lesser General Public License for more details.
    
    '    You should have received a copy of the GNU Lesser General Public
    '    License along with this library; if not, write to the Free Software
    '    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
    
    'Notes:
    ' This is a bare bones code for software rs232 comms
    ' terminal setup - 8 (data bits), none(parity), 1 (stop bit)
    ' Kent Schafer Mar 13, 2015
    
    '************************************************************
    'SoftSerialX.h commands                                     *
    'InitSerX                                                   *
    'SerPrintX byte, word, string                               *
    'SerSendX byte  'for non ascii characters                   *
    'SerReceiveX byte                                           *
    '************************************************************
    'baudrate table (tested for PICs may need adj. for AVRs     *
    '************************************************************
    'baudrate of 9600 & 19200 needs 8 Mhz clock minimum         *
    '************************************************************
    'BaudDelay for 19200 baud rate is 49 and HalfBaud is 25     *
    'BaudDelay for 9600 baud rate is 103 and HalfBaud is 52     *
    '************************************************************
    'BaudDelay for 9600 at 4 Mhz clock is 98 and HalfBaud is 50 *
    '************************************************************
    'baudrate 2400 & 4800 work with 4 MhZ and up clock          *
    'BaudDelay for 4800 baud rate is 206 and HalfBaud is 103    *
    'BaudDelay for 2400 baud rate is 408 and HalfBaud is 204    *
    '************************************************************
    'Mandatory syntax and constants for rs232X                  *
    '************************************************************
    'select baud and half baud delays from table values         *
    '#define BaudDelay wait XX us                               *
    '#define HalfBaudDelay wait XX us                           *
    'substitute GPIO.Pin for PortX.Pin when appropriate         *
    '#define SerTxHigh Set PortX.Pin On                         *
    '#define SerTxLow Set PortX.Pin Off                         *
    '#define SerRx PortX.Pin                                    *
    'dir PortX.Pin Out ;Tx                                      *
    'dir PortX.Pin in ;Rx                                       *
    'Call InitSerX                                              *
    '************************************************************
    Sub InitSERX
    SerTxHigh ;Initial RS232 idle state
    end sub
    
    Sub SerReceiveX (RxByte)
    RxWait:
    'wait for start bit
    IF SerRx On Then goto SerReceiveX
    'do half bit time delay
    HalfBaudDelay
    If SerRx On Then goto RxWait
    RxByte = 0
    For RxBit = 1 to 8
      BaudDelay
      Rotate RxByte Right
      If SerRx On then Set RxByte.7 1
      If SerRx Off Then Set RxByte.7 0
    Next
    BaudDelay
    End sub
    
    sub SerPrintX (PrintData$ as string)
    PrintLen = PrintData(0)
    if PrintLen = 0 then exit sub
    for SysPrintTemp = 1 to PrintLen
    SerSendX(PrintData(SysPrintTemp))
    next
    end sub
    
    Sub SerSendX(Xmit_Byte)#NR
    SerTxLow
    'wait BaudDelay us
    BaudDelay
    For cntr = 1 to 8
      Rotate Xmit_Byte Right
      #IfDEF PIC
      If Status.C ON Then
      #endif
      #ifdef AVR
      If SREG.0 On Then
      #endif
          SerTxHigh
      Else
          SerTxLow
      End if
      BaudDelay
    Next
    SerTxHigh
    BaudDelay
    end sub
    
    sub SerPrintX (LCDValue) #NR
    LCDValueTemp = 0
    LCDShowChar = 0
    SERCEN = 0
    SERDEC = 0
    SERUN = 0
    
    If LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    SerSendX (SERCEN)
    LCDShowChar = TRUE
    End If
    
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    SerSendX (SERDEC)
    End If
    
    SERUN = LCDValue + 48
    SerSendX (SERUN)
    End Sub
    
    sub SerPrintX (LCDValue as word ) #NR
    Dim SysCalcTempX As Word
    LCDValueTemp = 0
    LCDShowChar = 0
    SERDECMIL = 0
    SERMIL = 0
    SERCEN = 0
    SERDEC = 0
    SERUN = 0
    
    If LCDValue >= 10000 then
    LCDValueTemp = LCDValue / 10000 [word]
    LCDValue = SysCalcTempX
    SERDECMIL = LCDValueTemp + 48
    SerSendX (SERDECMIL)
    LCDShowChar = TRUE
    End If
    
    If LCDShowChar > 0 or LCDValue >= 1000 then
    LCDValueTemp = LCDValue / 1000 [word]
    LCDValue = SysCalcTempX
    SERMIL = LCDValueTemp + 48
    SerSendX (SERMIL)
    LCDShowChar = TRUE
    End If
    
    If LCDShowChar > 0 or LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100 [word]
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    SerSendX (SERCEN)
    LCDShowChar = TRUE
    End If
    
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10 [word]
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    SerSendX (SERDEC)
    End If
    
    SERUN = LCDValue + 48
    SerSendX (SERUN)
    End Sub
    
     

    Last edit: kent_twt4 2015-03-15

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.