News:

;) This forum is the property of Proton software developers

Main Menu

Hserin timing Help

Started by Jamie, Jul 11, 2024, 01:32 AM

Previous topic - Next topic

Jamie

Hello Forum,

I'm having an issue with Hserin waiting for 3 identification bytes followed by 3 data bytes.
I've coded it this way but am having trouble with missed keypresses.

when you press the "increment" key on the display I'm using it sends 6 bytes $07,$06,$2B,$00,$00,$2A
and the "Decrement" key sends $07,$06,$2E,$00,$00,$2F

What's happening is, as I press the up key sometimes it will increment and other times it wont and I have to kit the key several times.
Same with the down key.

I know there must be a timing issue here but haven't been able to solve it and was wondering a better way to code it.
I've tried the delay but the bigger the delay the worse the issue gets.
I would appreciate any assistance.

I'm using a 4d systems ulcd-43dt display and a Pic18F46K22 fuses are set correctly baud is 19,200
Xtal is internal @ 8Mhz

Thanks
Jamie



While RunFlag <> 1
    GoSub Readup
    GoSub ReadDn
wend

Readup:

HSerIn 50, NoDATA, [Wait($07,$06,$2B) , A,B,C]  ' read up
If C = $2A Then
    TestCnt = TestCnt + 1
    Return
EndIf

NoDATA:
Return


ReadDn:
HSerIn 50, NoDATA1, [Wait($07,$06,$2E) , A,B,C]  ' read up
If C = $2F Then
    TestCnt = TestCnt - 1
    Return
EndIf

NoDATA1:
Return

SCV

One method is to drop received bytes into an array, after 6 bytes have been received, check for out of phase data in the array using a FIFO ripple routine to drop the oldest byte until the sequence aligns. In the main loop check the RX1_FLAG and process as needed.


At INIT ensure to reset INDEX_IN1 and RX1_FLAG to 0.
INT routine is for 16bit dsPIC but the principle is the same for 8bit.

Tim.

SYMBOL FERR_1   = U1STAL.3
SYMBOL UR1DA    = U1STAH.1
DIM DATA_IN1 [8]   AS BYTE
DIM BUFFER_1 [8]   AS BYTE
DIM iTEMP1_U1      AS BYTE
DIM GASH           AS BYTE
DIM INDEX_IN1      AS BYTE
DIM RX1_FLAG       AS BIT


'================================================
'INTERRUPT ROUTINES HERE
'================================================
'Data arrives as
' BUFFER_1 [1] 07
' BUFFER_1 [2] 06
' BUFFER_1 [3] xx
' BUFFER_1 [4] 00
' BUFFER_1 [5] 00
' BUFFER_1 [6] xx

ISR U1RXINTERRUPT

SERIN_U1:       IF FERR_1 = 1 THEN                  'Check for frame error
                    FERR_1 = 0                     
                    GASH = U1RXREG                  'Bin the data
                    GOTO ISR_END1                   'Done
                ENDIF

                INDEX_IN1 = INDEX_IN1 + 1          'Next slot pointer
                IF INDEX_IN1 > 6 THEN              'Ripple data up
                    INDEX_IN1 = 6
                    FOR iTEMP1_U1 = 1 TO 5
                        BUFFER_1 [iTEMP1_U1] = BUFFER_1 [(iTEMP1_U1 + 1)]
                    NEXT
                ENDIF
                BUFFER_1 [INDEX_IN1] = U1RXREG 'Copy byte into buffer slot

                IF INDEX_IN1 < 6 THEN ISR_END1 'Only after 6 bytes
               
IF BUFFER_1 [1] <> $07 THEN ISR_END1 'Filter out errors
                IF BUFFER_1 [2] <> $06 THEN ISR_END1
                IF BUFFER_1 [4] > $00 THEN ISR_END1
                IF BUFFER_1 [5] > $00 THEN ISR_END1
               
                INDEX_IN1 = 0 'Reset pointer
                FOR iTEMP1_U1 = 1 TO 6 'Copy data to working array
                    DATA_IN1 [iTEMP1_U1] = BUFFER_1 [(iTEMP1_U1 + 1)]
                    BUFFER_1 [iTEMP1_U1] = 0 'Clear buffer
                NEXT
                RX1_FLAG = 1 'Raise flag data to read
               
ISR_END1:       IF UR1DA = 0 THEN SERIN_U1 '...more data in buffers
                U1RXIF = 0                          'Clear int flag

ENDISR

'================================================

Jamie

Thank you Tim, greatly appreciated I'll give this a try!

Jamie