Menu

Emulate HSerReceive with SoftSerial

2018-07-27
2018-07-27
  • Clint Koehn

    Clint Koehn - 2018-07-27

    You can use a hardware interrupt to act as a hardware serial receive. Hook your TX from your device to a hardware interrupt. Set the edge detect to falling. Set that port/pin to SoftSerial RX. Set any other pin to SoftSerial TX.

    This is with my new micro controller so it will probably need modying for your device, but I have used it on an older model that had hardware interrupts and it worked.

    #chip 16f19176, 32
    
    #include <SoftSerial.h>
    
    '======= Simulate hardware rx ==============================
    #define SER1_TXPORT PORTB
    #define SER1_TXPIN 1
    #define SER1_RXPORT PORTB 'needs to be a port and pin
    #define SER1_RXPIN 0      'that is an interrupt
    #define SER1_BAUD 9600
    #define SerInPort1 PORTB.0
    #define SerOutPort1 PORTB.1
    
    'Set pin directions
    dir SerOutPort1 Out
    Dir SerInPort1 In
    
    set SerOutPort1 on
    
    #define SOFTWARE_BUFFER1 60
    
    dim rxbuf1 as string * SOFTWARE_BUFFER1 'uses a string for a buffer
    dim rxflag1 as Integer                  'flag to tell when new char
    
    rxflag1 = 0
    rxbuf1(0) = 0
    
    INTEDG = 0 'Interrupt on falling edge
    
    On Interrupt ExtInt0 Call Rx1Int
    '===================================
    
    do
        if rxflag1 = 1 Then  ' new character in buffer
            rxflag1 = 0         ' reset buffer flag
            '----- start char is ? and end char is CR -----------
            if (rxbuf1(1) = "?") and (rxbuf1(rxbuf1(0)) = 13) Then
                  'complete message
                   rxbuf1(1) = "!"   'change start char
                   wait 500 us       'need brief delay before send 
                   Ser1Print rxbuf1
                    rxbuf1(0) = 0     'clear buffer
            end if
      end if    
    loop
    
    sub Rx1Int
        rxbuf1(0)++                            'inc char location
        rxbuf1(rxbuf1(0)) = Ser1Receive        'store char in string variable
        if rxbuf1(0) = SOFTWARE_BUFFER1 then   'if end of buffer
           rxbuf1(0) = 0                         '    start over
        end if
         rxflag1 = 1                            'set flag for new character
    end sub
    

    Later,
    Clint

     

    Last edit: Clint Koehn 2018-07-27
  • Chris Roper

    Chris Roper - 2018-07-27

    Simple, Elegant and addresses the main downfall of SoftSerial.
    I, and I am sure many others, will find this both useful and inspirational.
    Thanks for sharing.

    Cheers
    Chris

     
  • Clint Koehn

    Clint Koehn - 2018-07-27

    Thanks for the kind words Chris. I have a chip that has 2 hardware serial ports but I needed a third one. One radio went to the computer, one was for a sensor board I made and I needed a third one for a 4D display.

    Later,
    Clint

     

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.