Software USART routines (Alternate)

  • kent_twt4

    Hopefully a bug will be removed soon which keeps the the 12f675, 12f683 type devices from currently working.  Using a terminal program like Bray's Terminal, or Smiley's Developer Terminal with a split send/receive screen is helpful.  Enjoy!!!

    'This example uses alternate software UART/RS232 routines.
    'The Pic is programmed to wait for a value to be sent from a
    'terminal/computer, and will return an a2d value if the received
    'value is a match.  All constants of SerInit need to be edited to suit
    'device choice and baud rate.  At this time, the low pin count
    'devices like the 12f675, 12C5XX are not supported.
    'Kent Schafer 8/6/08
    'Chip model
    #chip 18f4620,8
    #config MCLRE=ON, OSC=INTIO67, LVP=OFF
    dim ADvalue as word
    dir PortA.0 out
    dir PortA.1 in

    Set PORTA.0 On
    wait 1 s
    Set PortA.0 off
    wait 1 s
    ;Pic waits for signal from terminal/computer
    RCV_RS232            ;Return value is RxByte
    Bin2ascii RxByte    ;echo back received string/ascii value
    XMIT_PRINT ("hello") 
    If RxByte = 52 Then      ;ascii 52 (Decimal 4 from terminal)
        Bin2ascii ReadAD10(AN1)
    End if   
    XMIT_Print ("  ")
    goto start 

    Sub Ser_Init
    ;slight adjustment was required for 9600bps delay value 
    #define baud 103
    #define halfbaud 52    ;place Read of SerRx in middle of bit 
    #define SerTxHigh Set PortC.6 On
    #define SerTxLow Set PortC.6 Off
    #define SerRx PortC.7
    dir PortC.7 in    ;Rx
    dir PortC.6 out    ;Tx 
    SerTxHigh ;Initial RS232 idle state
    end sub 

    Sub RCV_RS232
    IF SerRx On Then goto RCV_RS232  ;wait for start bit
    wait halfbaud us              ;do half bit time delay
    If SerRx On Then goto RxWait
    RxByte = 0
    For RxBit = 1 to 8                ;set up to read 8 bits
    wait baud us
    Rotate RxByte Right
    If SerRx On then Set RxByte.7 1
    If SerRx Off Then Set RxByte.7 0
    wait baud us
    End sub

    sub XMIT_PRINT (PrintData$) 
    PrintLen = PrintData(0) 
    if PrintLen = 0 then exit sub 
    'Write Data 
    for SysPrintTemp = 1 to PrintLen 
    end sub 

    Sub XMIT_RS232(Xmit_Byte)#NR 
    wait baud us 
    For cntr = 1 to 8 
    Rotate Xmit_Byte Right 
    If Status.C ON Then SerTxHigh
    If Status.C Off Then SerTxLow
    wait baud us
    wait baud us 
    end sub 

    sub Bin2ascii(LCDValue as word )#NR
    dim LCDValueTemp as word 
    SERMIL = 0
    SERCEN = 0
    SERDEC = 0
    SERUN = 0
    LCDValueTemp = 0
    IF LCDValue >= 10000 then
    LCDValueTemp = LCDValue / 10000
    SERDECMIL = LCDValueTemp + 48
    Xmit_RS232(SERDECMIL) 'SerSend (1,SERDECMIL)
    LCDValue = LCDValue - LCDValueTemp * 10000
    End if
    If LCDValueTemp > 0 Or LCDValue >= 1000 Then
    LCDValueTemp = LCDValue / 1000
    SERMIL = LCDValueTemp + 48
    Xmit_RS232(SERMIL) 'SerSend (1,SERMIL)
    LCDValue = LCDValue - LCDValueTemp * 1000
    End if
    IF LCDValueTemp > 0 OR LCDValue >= 100 then  
    LCDValueTemp = LCDValue / 100
    SERCEN = LCDValueTemp + 48 
    Xmit_RS232(SerCen) 'SerSend (1,SERCEN) 
    LCDValue = LCDValue - LCDValueTemp * 100 
    end if 
    IF LCDValueTemp > 0 OR LCDValue >= 10 then 
    LCDValueTemp = LCDValue / 10  
    SERDEC = LCDValueTemp + 48 
    Xmit_RS232(Serdec) 'SerSend (1,SERDEC) 
    LCDValue = LCDValue - LCDValueTemp * 10 
    end if 
    SERUN = LCDValue + 48 
    Xmit_RS232(Serun) 'SerSend (1,SERUN) 
    end sub 

  • For inverted output:

    Sub RCV_RS232
        IF SerRx Off then
            goto RCV_RS232 ;wait for start bit
        end if
        wait halfbaud us ;do half bit time delay
        If SerRx Off then
            goto RxWait
        end if
        RxByte = 0
        For RxBit = 1 to 8 ;set up to read 8 bits
            wait baud us
            Rotate RxByte Right
            If SerRx On then
                Set RxByte.7 0
            end if
            If SerRx Off then
                Set RxByte.7 1
            end if
        wait baud us
    End Sub


    If SerRx Off Then [line 3&5]
    Set RxByte.7 0/1 [line 15&19]
  • Sorry, unconsciously modified and saved these lines,
    This line should be changed for inverted output.

    #define SerTxHigh Set PortB.5 Off
    #define SerTxLow Set PortB.5 On