RS232 Software letal bug solved

Erdy
2013-03-02
2013-12-12
  • Erdy

    Erdy - 2013-03-02

    http://fr.wikipedia.org/wiki/UART (in french, the english version is different and less clear)

    RS232 Software TX has one BIG letal bug.
    The sleep, idle, no signal state of an RS232 TX line is ON / 1 / Vdd
    The Start Bit is a 0
    The Stop Bit is a 1

    GCBASIC RS232 Software is all wrong on these
    This can be solved by changing two lines in rs232.h

    line 157 : start bit SerTxLow ' instead of SerTxHigh
    line 184 : stop bit SerTxHigh ' instead of SerTxLow

    With these two changes, my PC receives correctly my PIC16F628A with it's internal 4MHz clock up to 4800 baud, on an USB RS232 TTL using prolific It must be the same on RX side ???? …

    see also messages in Great Cow Graphical Forum

    I am proud of me :-)

     
  • Erdy

    Erdy - 2013-03-02

    And here is the RX fix
    In gcbasic/include/lowlevel/rs232.h modify the followinglines

    line 200
    Wait Until SerQuickSample = False ' instead of Wait Until  = TRUE

    line 202
    If SerQuickSample = True Then Exit Sub ' instead of  If SerQuickSample = False Then Exit Sub

    line  238 to 243
            must be uncommented   :-)

    line 243
    Wait Until SerQuickSample = True      ' instead of  Wait Until SerQuickSample = FALSE

    With these changes, PIC16F628A with it's internal 4MHz clock receives my PC up to 2400 baud, on an USB RS232 TTL using prolific


    other things :

    There must be an error in Delays as someone told me in an other forum here,  when the 4MHz INT OSC of the PIC in replaced by a 20 MHz cristal, the maximum baud rate received by the PC does not change. I don't understand how the time delay is computed ????

    On the Sub SerSend side shouldn't there be an IntOff/IntOn too ?

     
  • Erdy

    Erdy - 2013-03-02

    Oops ! With a 20 MHz cristal ext osc, tha maximum TX speed is 19200 baud and the maximum RX speed is 4800 baud with the PC ! Using the corrected rs232.h

    Why 4800 only for RX ???

     
  • Erdy

    Erdy - 2013-03-02

    Codes
    gcbasic RX/TX  Hard/Soft  RS232 INT OSC/EXT OSC  FAST/SLOW Soft RS232  Code

    ' ==============================================================
    ' TEST CODE
    ' PIC16F628A RX/TX, Hardware/Software RS232,  OSC INT/EXT
    ' Two Speed  fast/Slow for RS232 Software on jumper, need reset
    ' RX/TX on Jumper need reset
    ' Test Materials :
    ' Windows, INT OSC/20MHz EXT OSC, PIC on Prolific-USB-Serial-Com, GCBASIC 0.9
    ' RS232 to USB TTL interface, no MAX232 required
    ' Erdemal                                        March 1st, 2013
    ' ==============================================================
    ' input PortA.0  (10k pulled up) sets   RX/TX mode    RX if ON = no Jumper,
    '                 TX if OFF = Jumper to Vss
    ' input PortA.1  (10k pulled up) sets   High/Low baud rate only for RS232 Software 
    '                Low Baud Rate = ON = no jumper,  high Baud rate = Off = Jumper to Vss
    '
    ' TX mode   on PortB.2     alternativly sends 50 then 100 every 200 ms
    '           Led on TX to Vdd via 4.8K (to see data flow)
    ' -------
    ' RX mode   on PortB.1
    ' -------
    ' output PortB.4  ON >> LED GREEN  ON    In RX mode ON when 100 is received
    ' output PortB.5  ON >> LED RED    ON    In RX mode ON when neither
    '                                        50 nor 100 is received (= RX error)
    ' Output PortB.6  ON >> LED YELLOW ON    In RX Mode ON when  50 is received
    ' (50 and 100 choosen for no good/bad reason)
    ' RX OK when flashing Green/Yellow, if RX error LedRed is on
    ' --------------------------------------------------------------
    ' ISSUE
    ' - In GCBASIC RS232 software with PIC transmitting to PC, RX data is always Halve the TXed value.
    '   It looks like the 8th bit is not sent or the first bit is the last start bit ??????????
    '   When two PIC communicate together with GCBASIC RS232 software, it works up to 4800 bauds
    ' - partly solved, still a Delay error
    '   https://sourceforge.net/projects/gcbasic/forums/forum/596084/topic/6877271/index/page/1
    ' --------------------------------------------------------------
    ' With GCBASIC RS232 Hardware communication with a computer it works perfectly
    '  - up to 19200 bauds with 16F628A 4MHz Osc Int +-1.5%
    '  - with a 20 MHz EXT OSC quartz 57600 baud obtained
    ' ==============================================================
    #Define OscQuartz       ' not sure these works in gcbasic
    #Define SerialHardware  ' if not, comment the lines
    ' ==============================================================
    #IfNDef OscQuartz ' ++++++++++++++++++++++++++++++++++++++++++++
    #chip 16F628A, 4
    #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    '
    #IfDef OscQuartz ' +++++++++++++++++++++++++++++++++++++++++++++
    #Chip 16F628A, 20
    #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ' --------------
    #config MCLR_OFF
    ' ==============================================================
    ' - Jumpers -    No Jumper : RX and 4800 bds
    ' -----------
    #Define btnRXTX PortA.0  ' On RX   , Off/0v = TX   = jumper placed
    #Define btn4896 PortA.1  ' On 4800 , Off/0v = 9600 = jumper placed
    '
    Dir btnRXTX in
    Dir btn4896 in
    ' ==============================================================
    #IfDef SerialHardware ' ++++++++++++++++++++++++++++++++++++++++
    #Define USART_BAUD_RATE 9600
    #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ' ==============================================================
    #IfnDef SerialHardware ' +++++++++++++++++++++++++++++++++++++++
    ' - Init TX -
    #define SendAHigh Set PORTB.2 ON    ' MUST LINES
    #define SendALow Set PORTB.2 OFF    ' MUST LINES
    '
    Dir PORTB.2 Out
    ' ---------------------------
    ' - Init RX  -
    #define RecAHigh PORTB.1 ON         ' MUST LINES
    #define RecALow PORTB.1 OFF         ' MUST LINES
    '
    Dir PORTB.1 In
    #EndIF   ' ++++++++++++++++++++++++++++++++++++++++++++++++++++++
    '
    #define LedGreen  PORTB.4
    #define LedRed    PORTB.5
    #define LedYellow PORTB.6
    '
    Dir PORTB.4 Out
    Dir PORTB.5 Out
    Dir PORTB.6 Out
    '
    ' ==============================================================
    ' ---- Select  RX or TX ----
    ' --------------------------
    Select Case btnRXTX
        Case Off
            Goto DataTransmitter
        Case On
            Goto DataReceiver
        Case Else
    End Select
    ' ==============================================================
    ' ---- Transmitter ----    
    ' ---------------------
    DataTransmitter:
    '
    #IfnDef SerialHardware
    If btn4896 = On Then
        InitSer 1, r9600, 1+WaitForStart, 8, 1, none, normal
    Else
        InitSer 1, r19200, 1+WaitForStart, 8, 1, none, normal
    End If
    #EndIf
    '
    Do  ' just flash yellow green for OK and RED for junks
        If Temp = 100 Then
            Temp = 50
            Else
            Temp = 100
        End If
        #IfnDef SerialHardware ' +++++++++++++++++++++++++++++++++++
            SerSend 1, Temp     ' TX remains OFF after transfer
        #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++
        #IfDef SerialHardware
            HSerSend Temp       ' TX remains ON after transfer
        #EndIf
        Wait 20 10ms
    Loop
    '
    ' ==============================================================
    ' ---- Receiver ----
    ' ------------------
    DataReceiver:
    '
    set LedGreen off
    set LedRed off
    set LedYellow off
    '
    #IfnDef SerialHardware
    If btn4896 = On Then
        InitSer 1, r9600, 129, 8, 1, none, normal
    Else
        InitSer 1, r19200, 129, 8, 1, none, normal
    End If
    #EndIf
    '
    Do  ' just flash yellow (RX=50) green(RX=100) and RED for junks (RX Else)
        #IfnDef SerialHardware  ' ++++++++++++++++++++++++++++++++++++
            SerReceive 1, Temp
        #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++++
        #IfDef SerialHardware ' ++++++++++++++++++++++++++++++++++++++
            HserReceive Temp
        #EndIf ' +++++++++++++++++++++++++++++++++++++++++++++++++++++
    
        Select Case Temp
            Case 50
                Set LedYellow On 
                Set LedGreen Off
                Set LedRed Off      
            Case 100
                Set LedGreen On
                Set LedYellow Off 
                Set LedRed Off      
            Case Else
                Set LedRed on
                Set LedYellow Off 
                Set LedGreen Off 
        End Select
    Loop
    ' ==============================================================
    '
    

    FREEBASIC  RS232   RX   CODE

    ' =========================================================
    ' FREEBASIC RS232 TX TEST CODE
    ' targetted on gcbasic rs232 with PIC controller
    ' Erdemal                                     March 2, 2013
    ' =========================================================
    '
    #include once "windows.bi"
    '
    Dim As Integer iCom, iFile
    Dim sCom As String
    iFile = FreeFile
    '
    sCom = "COM6:19200,N,8,1,cs0,ds0,cd0,rs"        ' Change ComX and Speed here
    ' Open Com sCom As iFile
    iCom = Open Com (sCom, As iFile)
    If iCom = 0 Then
        Print "OPEN COM OK   Returns 0"
    Else
        MessageBox (0, "ERROR OPEN COM FAILED, I CLOSE", "GCBASIC RS232 RX TEST", 0)
        End   
    End If
    '
    Dim As Byte J, K
    Dim N As integer
    J = 100
    K = 50
    For N = 0 To 10
         Sleep 200
         Print #ifile, Chr(J);    ' ; no CR sent
         Sleep 200
         Print #ifile, Chr(K);    ' ; no CR sent
    Next N
    '
    Print
    Print "    **** FINISHED ****"
    '
    Close iFile
    Sleep
    ' =========================================================
    
    ' =========================================================
    ' FREEBASIC RS232 RX TEST CODE
    ' targetted on gcbasic rs232 with PIC controller
    ' Erdemal                                     March 2, 2013
    ' =========================================================
    '
    #include once "windows.bi"
    '
    Dim As Integer iCom, iFile, iCount = 0
    Dim sCom As String
    iFile = FreeFile
    '
    sCom = "COM6:9600,N,8,1,cs0,ds0,cd0,rs"        ' Change  ComX and Speed here
    ' Open Com sCom As iFile
    iCom = Open Com (sCom, As iFile)
    If iCom = 0 Then
        Print "OPEN COM OK   Returns 0"
    Else
        MessageBox (0, "ERROR OPEN COM FAILED, I CLOSE", "GCBASIC RS232 RX TEST", 0)
        End   
    End If
    '
    Dim bData As uByte
    '
    bData = 0
    Messagebox (0, "CLOSE TO START RS232 RX", "Jack", 0)
    '
    iCount = 0
    Get #iFile, , bData
    While icount < 20
        If bData <> 0 Then
            Print bData
            iCount = iCount + 1
        End If
        bdata = 0
        Sleep 10
        Get #iFile, , bData    
    Wend
    '
    Print
    Print "    **** FINISHED ****"
    '
    Close iFile
    Sleep'
    

    Great SourceForge deserves better forums :-)

    Erdemal

     
  • ic-beau

    ic-beau - 2013-03-05

    RS232 is normally inverted…  IOW… so lines 157 and 184 are correct.  Same with the data it's also supposed to be inverted.

    As far as the baud speed, I'm not sure about how GCB compiler does it because I was using a PIC that wasn't fully supported.  So I had to roll my own in-line assembly bit-banging the serial routines.  On a PIC16F54 I was able to get 57,600 baud running at 20MHz

    Reference:
    http://en.wikipedia.org/wiki/File:Rs232_oscilloscope_trace.svg

     
  • Erdy

    Erdy - 2013-03-05

    I don't know whose source is the good one ?
    - the PIC Rs232 Hardware works as my source
    - the gcbasic RS232 Software samples don't work in my real world

     
  • Anobium

    Anobium - 2013-06-23

    In reality... it is not needed. As you can do everything with the standard code base.

    With this patch I do not have to keep changing my code to much. I do and have got really confused with setting bits ON and OFF etc.

    At least with this method... I 'define' the PC as my terminal device and then just change to 'normal'. It sort of makes sense to me.

    :-)

     
  • Max

    Max - 2013-12-12

    I got the same problem of Erdy (using TX/RX from software serial) after many test I made the modification on RS232.H suggested from Erdy/Anobium in this thread and now it work properly.
    I should made another modification on RS232.H because the first char received was always corrupped.

    this is the other fix on the RS232.H (by Anobyum) that i made for eliminate first corrupped received char

        #ifdef OneOf(SendAHigh, RecAHigh)
                  SendAHigh                <----- insert this line
                  if Ser_Select = 1 THEN
    
        #ifdef OneOf(SendBHigh, RecBHigh)
                  SendBHigh                <----- insert this line
                  if Ser_Select = 2 THEN
    
        #ifdef OneOf(SendCHigh, RecCHigh)
                  SendCHigh                <----- insert this line
                  if Ser_Select = 3 THEN
    

    This is the test code (it does't work with the original RS232.H ... please you test also)


    #chip 16F876A,20
    #config OSC=HS
    
    #define RS232ForPC     <------------
    
    #define SerInPort PORTC.4
    #define SerOutPort PORTC.5
    #define SendAHigh Set SerOutPort On
    #define SendALow Set SerOutPort off
    #define RecAHigh SerInPort on
    #define RecALow SerInPort off
    
    Dir SerOutPort Out
    Dir SerInPort In
    
    'doesn't work more than baud 9600
    InitSer 1, r9600, 1+WaitForStart, 8, 1, None, normal
    
    inizio:
       SerReceive (1, Temp)
       sersend (1, Temp)
    goto inizio
    

    I am compiling with synwrite 6.1.140 & Great Cow BASIC (0.9 22/9/2013)
    the USB serial cable is Prolific

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks