Losing data when extracting bytes from words - 16F18855


+ Reply to Thread
Results 1 to 26 of 26

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    Doesn't this WHILE do the same thing? Make sure everything is gone before doing anything else?
    got nothing to do with that , its intent was to give the other end time to setup for reception.

    your handshake method is extremely awkward and incapable of recovery from a missed transmission.


    turns out it was not needed and should be removed
    Last edited by richard; - 15th March 2025 at 03:47.
    Warning I'm not a teacher

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    more robust demand/response handshaking

    Code:
    ;pic18f26k22
    #CONFIG
      CONFIG  FOSC = INTIO67     ; Internal oscillator block
      CONFIG  PLLCFG = OFF        ; Oscillator used directly
      CONFIG  PRICLKEN = OFF     ; Primary clock can be disabled by software
      CONFIG  FCMEN = OFF        ; Fail-Safe Clock Monitor disabled
      CONFIG  IESO = OFF         ; Oscillator Switchover mode disabled
      CONFIG  PWRTEN = OFF       ; Power up timer disabled
      CONFIG  BOREN = SBORDIS    ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
      CONFIG  BORV = 190         ; VBOR set to 1.90 V nominal
      CONFIG  WDTEN = ON         ; WDT is always enabled. SWDTEN bit has no effect
      CONFIG  WDTPS = 32768      ; 1:32768
      CONFIG  CCP2MX = PORTC1    ; CCP2 input/output is multiplexed with RC1
      CONFIG  PBADEN = OFF       ; PORTB<5:0> pins are configured as digital I/O on Reset
      CONFIG  CCP3MX = PORTB5    ; P3A/CCP3 input/output is multiplexed with RB5
      CONFIG  HFOFST = ON        ; HFINTOSC output and ready status are not delayed by the oscillator stable status
      CONFIG  T3CMX = PORTC0     ; T3CKI is on RC0
      CONFIG  P2BMX = PORTB5     ; P2B is on RB5
      CONFIG  MCLRE = INTMCLR    ; MCLR pin enabled, RE3 input pin disabled
      CONFIG  STVREN = ON        ; Stack full/underflow will cause Reset
      CONFIG  LVP = OFF          ; Single-Supply ICSP disabled
      CONFIG  XINST = OFF        ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
      CONFIG  DEBUG = OFF        ; Disabled
      CONFIG  CP0 = OFF          ; Block 0 (000800-001FFFh) not code-protected
      CONFIG  CP1 = OFF          ; Block 1 (002000-003FFFh) not code-protected
      CONFIG  CP2 = OFF          ; Block 2 (004000-005FFFh) not code-protected
      CONFIG  CP3 = OFF          ; Block 3 (006000-007FFFh) not code-protected
      CONFIG  CPB = OFF          ; Boot block (000000-0007FFh) not code-protected
      CONFIG  CPD = OFF          ; Data EEPROM not code-protected
      CONFIG  WRT0 = OFF         ; Block 0 (000800-001FFFh) not write-protected
      CONFIG  WRT1 = OFF         ; Block 1 (002000-003FFFh) not write-protected
      CONFIG  WRT2 = OFF         ; Block 2 (004000-005FFFh) not write-protected
      CONFIG  WRT3 = OFF         ; Block 3 (006000-007FFFh) not write-protected
      CONFIG  WRTC = OFF         ; Configuration registers (300000-3000FFh) not write-protected
      CONFIG  WRTB = OFF         ; Boot Block (000000-0007FFh) not write-protected
      CONFIG  WRTD = OFF         ; Data EEPROM not write-protected
      CONFIG  EBTR0 = OFF        ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR1 = OFF        ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR2 = OFF        ; Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR3 = OFF        ; Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTRB = OFF        ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    #ENDCONFIG
    
    
    
    
    define OSC 64 
                        
                       
        ANSELC = 0 
        OSCCON = $70
        OSCTUNE.6 = 1                
        RCSTA = $90   ' Enable serial port & continuous receive
        TXSTA = $24   ' Enable transmit, BRGH = 1
        SPBRG = 21    ' 57600 Baud @ 64MHz, -0.08%
        SPBRGH = 1
        BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    
    
    
    
    
    
    
    
       
    asm
    ADCcode = _MsgData
    Num0    = _MsgData + 1
    Num1    = _MsgData + 3
    Num2    = _MsgData + 5
    Num3    = _MsgData + 7
    Num4    = _MsgData + 9
    Num5    = _MsgData + 11
    Num6    = _MsgData + 13
    Num7    = _MsgData + 15
    Num8    = _MsgData + 17
    Num9    = _MsgData + 19
    endasm
    
    
        MsgOK               var byte[2]
        MsgLoop             VAR WORD
        MsgData             var byte[21]
        ADCcode             var BYTE ext
        Num0                VAR WORD ext
        Num1                VAR WORD ext
        Num2                VAR WORD ext
        Num3                VAR WORD ext
        Num4                VAR WORD ext
        Num5                VAR WORD ext
        Num6                VAR WORD ext
        Num7                VAR WORD ext
        Num8                VAR WORD ext
        Num9                VAR WORD ext
    
    
    
    
    
    
    Mainloop:
        ADCcode = 2
        for MsgLoop = 0 to 390 step 10                         ' loop by multiples of 10 
            tol:
            hserin 50,tol,[ wait("<OK>") ] 
            gosub TransmitData
        next MsgLoop
    goto mainloop
    end
    
    
    
    
    TransmitData:
        Num0 = MsgLoop
        Num1 = MsgLoop + 1
        Num2 = MsgLoop + 2
        Num3 = MsgLoop + 3
        Num4 = MsgLoop + 4
        Num5 = MsgLoop + 5
        Num6 = MsgLoop + 6
        Num7 = MsgLoop + 7
        Num8 = MsgLoop + 8
        Num9 = MsgLoop + 9
        hserout [   "[",21,str MsgData\21] 
    RETURN
    rx
    Code:
    ;pic18f26k22
    #CONFIG
      CONFIG  FOSC = INTIO67     ; Internal oscillator block
      CONFIG  PLLCFG = ON        ; Oscillator used directly
      CONFIG  PRICLKEN = OFF     ; Primary clock can be disabled by software
      CONFIG  FCMEN = OFF        ; Fail-Safe Clock Monitor disabled
      CONFIG  IESO = OFF         ; Oscillator Switchover mode disabled
      CONFIG  PWRTEN = OFF       ; Power up timer disabled
      CONFIG  BOREN = SBORDIS    ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
      CONFIG  BORV = 190         ; VBOR set to 1.90 V nominal
      CONFIG  WDTEN = ON         ; WDT is always enabled. SWDTEN bit has no effect
      CONFIG  WDTPS = 32768      ; 1:32768
      CONFIG  CCP2MX = PORTC1    ; CCP2 input/output is multiplexed with RC1
      CONFIG  PBADEN = OFF       ; PORTB<5:0> pins are configured as digital I/O on Reset
      CONFIG  CCP3MX = PORTB5    ; P3A/CCP3 input/output is multiplexed with RB5
      CONFIG  HFOFST = ON        ; HFINTOSC output and ready status are not delayed by the oscillator stable status
      CONFIG  T3CMX = PORTC0     ; T3CKI is on RC0
      CONFIG  P2BMX = PORTB5     ; P2B is on RB5
      CONFIG  MCLRE = INTMCLR    ; MCLR pin enabled, RE3 input pin disabled
      CONFIG  STVREN = ON        ; Stack full/underflow will cause Reset
      CONFIG  LVP = OFF          ; Single-Supply ICSP disabled
      CONFIG  XINST = OFF        ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
      CONFIG  DEBUG = OFF        ; Disabled
      CONFIG  CP0 = OFF          ; Block 0 (000800-001FFFh) not code-protected
      CONFIG  CP1 = OFF          ; Block 1 (002000-003FFFh) not code-protected
      CONFIG  CP2 = OFF          ; Block 2 (004000-005FFFh) not code-protected
      CONFIG  CP3 = OFF          ; Block 3 (006000-007FFFh) not code-protected
      CONFIG  CPB = OFF          ; Boot block (000000-0007FFh) not code-protected
      CONFIG  CPD = OFF          ; Data EEPROM not code-protected
      CONFIG  WRT0 = OFF         ; Block 0 (000800-001FFFh) not write-protected
      CONFIG  WRT1 = OFF         ; Block 1 (002000-003FFFh) not write-protected
      CONFIG  WRT2 = OFF         ; Block 2 (004000-005FFFh) not write-protected
      CONFIG  WRT3 = OFF         ; Block 3 (006000-007FFFh) not write-protected
      CONFIG  WRTC = OFF         ; Configuration registers (300000-3000FFh) not write-protected
      CONFIG  WRTB = OFF         ; Boot Block (000000-0007FFh) not write-protected
      CONFIG  WRTD = OFF         ; Data EEPROM not write-protected
      CONFIG  EBTR0 = OFF        ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR1 = OFF        ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR2 = OFF        ; Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTR3 = OFF        ; Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
      CONFIG  EBTRB = OFF        ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    #ENDCONFIG
    
    
    
    
    define OSC 64                
                      
                      
    ANSELC = 0
    
    
                
    include    "strtok.pbpmod"                
                       
        OSCCON = $70
        OSCTUNE.6 = 1   
        RCSTA = $90   ' Enable serial port & continuous receive
    TXSTA = $24   ' Enable transmit, BRGH = 1
    SPBRG = 21    ' 57600 Baud @ 64MHz, -0.08%
    SPBRGH = 1
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    
    
    
    
    asm
    ADCcode = _MsgData
    Num0    = _MsgData + 1
    Num1    = _MsgData + 3
    Num2    = _MsgData + 5
    Num3    = _MsgData + 7
    Num4    = _MsgData + 9
    Num5    = _MsgData + 11
    Num6    = _MsgData + 13
    Num7    = _MsgData + 15
    Num8    = _MsgData + 17
    Num9    = _MsgData + 19
    endasm
    
    
        incoming            var byte[21]
        MsgData             var byte[21]
        ADCcode             var BYTE ext
        Num0                VAR WORD ext
        Num1                VAR WORD ext
        Num2                VAR WORD ext
        Num3                VAR WORD ext
        Num4                VAR WORD ext
        Num5                VAR WORD ext
        Num6                VAR WORD ext
        Num7                VAR WORD ext
        Num8                VAR WORD ext
        Num9                VAR WORD ext
        Pause 1000                           ' Let PIC stabilize and start after RX PIC
       
    Mainloop:
        hserout [   "<OK>"]    
        hserin 50,mainloop,[ wait("[",21), STR incoming \21]
        if incoming[0]   = 2 then
            strcpy  MsgData , incoming ,21
            hserout [   "[", dec4 Num0, _
                        " ", dec4 Num1, " ", dec4 Num2, " ", dec4 Num3, _
                        " ", dec4 Num4, " ", dec4 Num5, " ", dec4 Num6, _
                        " ", dec4 Num7, " ", dec4 Num8, " ", dec4 Num9, "]",13,10]                        
            hserout [   10]                 ' this is just to maintain alignment on Serial Communicator            
        endif
      GOTO Mainloop
    end
    Warning I'm not a teacher

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    1. Why does this compile OK? I mean the asm/endasm block is before defining the variables. Shouldn't the compiler complain about that?

    2. the sub TransmitData transfers data to variables Num0-Num9 from MsgLoop but then sends the str MsgData. Is this correct?

    Ioannis

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    1. Why does this compile OK? I mean the asm/endasm block is before defining the variables. Shouldn't the compiler complain about that?
    why would it complain? the asm block generates no code at all , it only creates labels for the assembler

    2. the sub TransmitData transfers data to variables Num0-Num9 from MsgLoop but then sends the str MsgData. Is this correct?
    yes, MsgData is a 21 byte structure that holds the vars Num0-Num9 + ADCcode
    Warning I'm not a teacher

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    Quote Originally Posted by richard View Post
    more robust demand/response handshaking...


    Once I TX data, I waited till TX complete:

    The TXIF flag does not indicate transmit completion (use TRMT for this purpose instead).
    (16F18855 p.146)


    Then I sit in a loop waiting for RX interrupt:

    To ensure that no actual data is lost, check the RCIDL bit to verify that a receive operation is not in process...
    (16F18855 p.553)


    Code:
    ;--- Interrupts ----------------------------------------------------------------
    
    RXInterrupt:
        hserin [ STR MsgData\3  ]
    
        while BAUDCON1.6 = 0                        ' Check RCIDL bit
        wend
    
        RXoccurred = 1                              ' Set flag
    @ INT_RETURN
        
    ;--- Subroutines ---------------------------------------------------------------
    
    SendData:
    
        hserout [   MsgData[0], MsgData[1], MsgData[2]   ]                             
    
        while TX1STA.1 = 0                           ' Check TRMT bit
        wend
    
        while RXoccurred = 0                       ' Check for interrupt
        wend
    
        RXoccurred = 0                              ' Set flag
        ADCchange = 1
    RETURN

    I turned the pot pretty fast and this is the fastest interval I got from start of 1st TX to start of 2nd TX:

    Name:  Fastest pot movement.png
Views: 10041
Size:  37.5 KB


    I'm still studying your TX/RX module. At least now I have a control that I can compare with.
    Last edited by Demon; - 16th March 2025 at 00:43.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    To make sure I wasn't losing data, I returned the data I received. I also used the 45mm slide pot for another speed test; zipping that sucker as fast as I could: 1.234 mSec from start to start of TX.

    These are the signals, in case the text from Saleae probe is not clear enough (they get a little fudgy or some reason):

    Primary PIC:
    - TX

    Secondary PIC:
    - RX-INT
    - LCDout ADC values
    - TX confirmation
    - LCDout "X" indicator (this is for testing only, to see if other pots "jitter")

    Note that data is not lost even if bottom LCDout overlaps into next message.

    Name:  Fastest pot movement v2.png
Views: 8976
Size:  94.8 KB
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Losing data when extracting bytes from words - 16F18855

    I figure I'd push the envelope a bit faster with that 45mm slide pot; 1.228 mSec from start to start of TX.

    I also took out that 2nd LCDout with the "x" indicator, it was just for testing anyways.

    This time I went faster than the previous ADC increment changes of 3, I managed to go to 7 and still no data lost:

    Name:  Fastest pot movement v3.png
Views: 8914
Size:  100.5 KB
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

Similar Threads

  1. Decoding serial data bytes and nibbles.
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th January 2018, 08:22
  2. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  3. ORing / ANDing bytes or words
    By mmargolis in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 19th March 2011, 20:12
  4. Success! Are they 'words' or are they 'bytes'?
    By BitHead in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 27th December 2009, 16:54
  5. Codespace Saving Tip... Don't mix Words and Bytes
    By Melanie in forum Code Examples
    Replies: 0
    Last Post: - 18th June 2004, 10:23

Members who have read this thread : 14

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts