Menu

serial receive problem

Help
2010-05-05
2013-05-30
  • Nobody/Anonymous

    why does this not work?
    #chip 12F683, 8 'mhz
    #config INTRC_OSC_NOCLKOUT, MCLRE=off, WDT=off
    #define RecAHigh GPIO.1 off
    #define RecALow GPIO.1 on
    InitSer 1,r9600,1,8,1,none,normal
    dir gpio.0 out
    dir gpio.1 in
    Ser_Init
    main:
    serreceive 1, temp
    if temp = 49 then
    xmit_print ("works")
    wait 2 s
    end if
    goto main

    Sub Ser_Init
    #define baud 104 ;the us delay for 9600 baud rate 
    #define SerTxHigh Set gpio.0 on
    #define SerTxLow Set gpio.0 off
    SerTxhigh ;Initial RS232 idle state
    end sub

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

    Sub XMIT_RS232(Xmit_Byte)#NR
    SerTxLow ;Start bit
    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
    Next
    SerTxHigh ;Stop bit
    wait baud us
    end sub

     
  • kent_twt4

    kent_twt4 - 2010-05-06

    Unfortunately the syntax for the built in software uart and the alternate rs232 functions have been intermingled.  Try the help file from the GCBasic folder and follow the clear examples.

    If you wish to explore the alternate soft uart routine, then look at the below example code with all supporting subs.  The NEW bin2ascii routines are just a different name for Hugh's GCBasics Print() subs from lcd.h. But  if we used that sub name here, the compiler would get confused.  All the setup for I/O and baud rate is in the Ser_Init sub.

    'Updated Alternate Soft RS232 routine example with new bin2ascii
    'overloaded subs.  kent_twt4
    #chip 12F683, 8 'mhz
    #config INTRC_OSC_NOCLKOUT, MCLRE=off, WDT=off
    dim num as word
    Ser_Init
    start:
    ;Pic waits for signal from terminal/computer
    RCV_RS232 ;Waits for terminal input, and return value is RxByte
    Xmit_RS232 RxByte ;echo back received ascii value
    If RxByte = 49 Then ;ascii 49 (Decimal 1 from terminal)
        XMIT_PRINT (" hello")
        For num = 0 to 500
            Bin2ascii num  ;send out binary value as ascii
            XMIT_Print (" ")
        next
    end if
    Xmit_RS232 10:Xmit_RS232 13  ;line feed, carriage return
    goto start
    Sub Ser_Init
    ;slight adjustment was required for 9600bps delay value
    #define baud 103  ;expressed as frequency in us
    #define halfbaud 52 ;place Read of SerRx in middle of bit
    #define SerTxHigh Set GPIO.0 On
    #define SerTxLow Set GPIO.0 Off
    #define SerRx GPIO.1
    dir GPIO.0 out ;Tx
    dir GPIO.1 in ;Rx
    SerTxHigh ;Initial RS232 idle state
    end sub
    Sub RCV_RS232 
    RxWait:
    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
    Next
    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
    XMIT_RS232(PrintData(SysPrintTemp))
    next
    end sub
    Sub XMIT_RS232(Xmit_Byte)#NR
    SerTxLow
    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
    Next
    SerTxHigh
    wait baud us
    end sub
    sub bin2ascii (LCDValue) #NR
    LCDValueTemp = 0
    LCDShowChar = 0
    SERCEN = 0
    SERDEC = 0
    SERUN = 0
    If LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100 
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    Xmit_RS232 (SERCEN)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10 
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    Xmit_RS232 (SERDEC)
    End If
    SERUN = LCDValue + 48
    Xmit_RS232 (SERUN)
    End Sub
    sub bin2ascii (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
    Xmit_RS232 (SERDECMIL)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 1000 then
    LCDValueTemp = LCDValue / 1000 [word]
    LCDValue = SysCalcTempX
    SERMIL = LCDValueTemp + 48
    Xmit_RS232 (SERMIL)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100 [word]
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    Xmit_RS232 (SERCEN)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10 [word]
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    Xmit_RS232 (SERDEC)
    End If
    SERUN = LCDValue + 48
    Xmit_RS232 (SERUN)
    End Sub
    
     
  • kent_twt4

    kent_twt4 - 2010-05-06

    Well that is a bummer, code tags on the blink again, or I just can't figure it out.  Even dumped it in BBCode playground to check it first.  About to give up on this, will try again.

    'Updated Alternate Soft RS232 routine example with new bin2ascii
    'overloaded subs.  kent_twt4
    #chip 12F683, 8 'mhz
    #config INTRC_OSC_NOCLKOUT, MCLRE=off, WDT=off
    dim num as word
    Ser_Init
    start:
    ;Pic waits for signal from terminal/computer
    RCV_RS232 ;Waits for terminal input, and return value is RxByte
    Xmit_RS232 RxByte ;echo back received ascii value
    If RxByte = 49 Then ;ascii 49 (Decimal 1 from terminal)
        XMIT_PRINT (" hello")
        For num = 0 to 500
            Bin2ascii num  ;send out binary value as ascii
            XMIT_Print (" ")
        next
    end if
    Xmit_RS232 10:Xmit_RS232 13  ;line feed, carriage return
    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 GPIO.0 On
    #define SerTxLow Set GPIO.0 Off
    #define SerRx GPIO.1
    dir GPIO.0 out ;Tx
    dir GPIO.1 in ;Rx
    SerTxHigh ;Initial RS232 idle state
    end sub
    Sub RCV_RS232 
    RxWait:
    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
    Next
    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
    XMIT_RS232(PrintData(SysPrintTemp))
    next
    end sub
    Sub XMIT_RS232(Xmit_Byte)#NR
    SerTxLow
    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
    Next
    SerTxHigh
    wait baud us
    end sub
    sub bin2ascii (LCDValue) #NR
    LCDValueTemp = 0
    LCDShowChar = 0
    SERCEN = 0
    SERDEC = 0
    SERUN = 0
    If LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100 
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    Xmit_RS232 (SERCEN)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10 
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    Xmit_RS232 (SERDEC)
    End If
    SERUN = LCDValue + 48
    Xmit_RS232 (SERUN)
    End Sub
    sub bin2ascii (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
    Xmit_RS232 (SERDECMIL)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 1000 then
    LCDValueTemp = LCDValue / 1000 [word]
    LCDValue = SysCalcTempX
    SERMIL = LCDValueTemp + 48
    Xmit_RS232 (SERMIL)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 100 then
    LCDValueTemp = LCDValue / 100 [word]
    LCDValue = SysCalcTempX
    SERCEN = LCDValueTemp + 48
    Xmit_RS232 (SERCEN)
    LCDShowChar = TRUE
    End If
    If LCDShowChar > 0 or LCDValue >= 10 then
    LCDValueTemp = LCDValue / 10 [word]
    LCDValue = SysCalcTempX
    SERDEC = LCDValueTemp + 48
    Xmit_RS232 (SERDEC)
    End If
    SERUN = LCDValue + 48
    Xmit_RS232 (SERUN)
    End Sub
    
     
  • kent_twt4

    kent_twt4 - 2010-05-06

    That's it, I give, unless someone can explain it to me.

     
  • Nobody/Anonymous

    thanks,
    that works great

     

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.