Receiving Packet Array In Usart


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    Thanks for the example code, I modified it to my variable names and included the main loop but it just gives me vales of 0 for BUFFER, what have I done wrong. I checked if it was interrupting and it is.

    Here is my code:

    '************************************************* **
    @ device hs_OSC, wdt_on, pwrt_on, protect_off
    define osc 20
    Include "modedefs.bas"
    trisc.6=0
    TRISC.7=1
    TRISA.0=0


    RCSTA = %10010000 ' ENABLE USART AND SET TO CONTINUOUS RECEIVE
    SPBRG = 64 'SET TO 4800bps
    INTCON = %11000000 'ENABLE GLOBAL & PERIFERAL INTERRUPTS
    RCIF VAR PIR1.5 'FLAG IS THE INTERRUPT WHEN A BYTE IS IN THE USART BUFFER
    RCIE VAR PIE1.5
    OERR VAR RCSTA.1
    CREN VAR RCSTA.4
    test var porta.0
    BAUD CON 16390


    BUFFER VAR BYTE[8] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
    BYTECOUNTER VAR BYTE 'USED TO COUNTER BYTES INTO THE BUFFER
    BFULL VAR BIT 'FLAGS THAT THE BUFFER HAS ALL 8 BYTES
    VALUE VAR BYTE 'A TEMP SINGLE BYTE RECEIVED FROM THE USART
    COUNTER VAR WORD 'USED FOR LOOP IN MAIN PROGRAM


    BUFFER = 0
    BYTECOUNTER = 0
    BFULL = 0
    VALUE = 0
    RCIE=1 'ENABLE USART RECEIVE INTERRUPT FLAG

    ON INTERRUPT GOTO GETDATA

    GOTO MAIN:


    disable


    GETDATA:

    if RCIF=1 then 'IF RECEIVE FLAG IS ON

    VALUE=RCREG 'READ THE VALUE IN THE USART BUFFER

    IF VALUE = $A0 THEN 'CHECK TO SEE IF THE BYTE IS THE START BYTE
    BYTECOUNTER=0 'AND SET THE COUNTER TO FIRST BYTE
    ENDIF

    IF BYTECOUNTER < 8 THEN

    BUFFER[BYTECOUNTER] = VALUE

    BYTECOUNTER=BYTECOUNTER+1 'INCREMENT FOR NEXT BYTE

    IF (BYTECOUNTER = 8) AND (VALUE=$AF) THEN
    TOGGLE PORTD.0
    GOTO MAIN
    ENDIF

    ENDIF

    'CHECK IF OVERRUN HAS OCCURRED:

    IF OERR=1 THEN
    Serout2 test,BAUD,["OERR",10,13] 'FOR DEBUGGING SHOW ERROR
    CREN=0 'STOP CONTINUOUS RECEIVE
    CREN=1 'RESTART CONTINUOS RECIEVE & CLEAR THE FLAG

    BYTECOUNTER=0 'RESET COUNTER
    GOTO GETDATA
    ENDIF

    ENDIF

    RESUME

    ENABLE
    '===============================================


    MAIN:

    ON INTERRUPT GOTO GETDATA

    if counter= 5000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS

    serout2 test,BAUD,["BUFFER=",HEX BUFFER.0,HEX BUFFER.1,10,13]
    '(JUST CHECK 1ST 2 VALUES)

    toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING

    counter=0
    else
    counter=counter+1
    endif

    goto main

    end

  2. #2
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    I modified my code and am getting the 8 bytes but seem to keep running into buffer overflows. Can you see an error in my interrupt routine?

    '************************************************* ***************
    @ device hs_OSC, wdt_on, pwrt_on, protect_off
    define osc 20
    Include "modedefs.bas"
    trisc.6=0
    TRISC.7=1
    TRISA.0=0


    RCSTA = %10010000 ' ENABLE USART AND SET TO CONTINUOUS RECEIVE
    SPBRG = 64 'SET TO 4800bps
    INTCON = %11000000 'ENABLE GLOBAL & PERIFERAL INTERRUPTS
    RCIF VAR PIR1.5 'FLAG IS THE INTERRUPT WHEN A BYTE IS IN THE USART BUFFER
    RCIE VAR PIE1.5
    OERR VAR RCSTA.1
    CREN VAR RCSTA.4
    test var porta.0
    BAUD CON 16390


    BUFFER VAR BYTE[10] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
    BYTECOUNTER VAR BYTE 'USED TO COUNTER BYTES INTO THE BUFFER
    BFULL VAR BIT 'FLAGS THAT THE BUFFER HAS ALL 8 BYTES
    VALUE VAR BYTE 'A TEMP SINGLE BYTE RECEIVED FROM THE USART
    COUNTER VAR WORD 'USED FOR LOOP IN MAIN PROGRAM


    BUFFER = 0
    BYTECOUNTER = 0
    BFULL = 0
    VALUE = 0
    RCIE=1 'ENABLE USART RECEIVE INTERRUPT FLAG

    ON INTERRUPT GOTO GETDATA

    GOTO MAIN:


    disable
    '====================INTERRUPT ROUTINE ==============================

    GETDATA:


    IF RCIF=1 THEN

    VALUE=RCREG
    IF VALUE = $A0 THEN 'CHECK TO SEE IF THE BYTE IS THE START BYTE
    BYTECOUNTER=0
    ENDIF

    RCIF=0

    IF BYTECOUNTER < 8 THEN
    BUFFER[BYTECOUNTER] = VALUE
    BYTECOUNTER=BYTECOUNTER+1
    ENDIF

    'CHECK IF OVERRUN HAS OCCURRED:

    IF OERR=1 THEN
    Serout2 test,BAUD,["OERR",10,13] 'TO SHOW OVERFLOW
    PAUSE 1000
    CREN=0 'STOP CONTINUOUS RECEIVE
    CREN=1 'RESTART CONTINUOS RECIEVE & CLEAR THE OVERRUN FLAG
    BYTECOUNTER=0 'RESET COUNTER AS PART OF PACKET HAS BEEN LOST SO START AGAIN
    BUFFER=0
    GOTO GETDATA
    ENDIF

    IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN

    GOTO MAIN
    ELSE
    GOTO GETDATA
    ENDIF
    ENDIF





    RESUME

    ENABLE
    '================================================= ====================================

    MAIN:

    'ON INTERRUPT GOTO GETDATA

    if counter= 1000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS
    serout2 test,BAUD,["BUFFER=",HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],",",HEX BUFFER[4],",",HEX BUFFER[5],",",HEX BUFFER[6],",",HEX BUFFER[7],10,13] 'JUST CHECK 1ST 2 VALUES
    toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
    counter=0
    else
    counter=counter+1
    endif

    goto main

    end

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

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You've got data coming in at 4800 baud, and data going out a 38400.

    So you can send 8 bytes in the time it takes to receive 1 byte.
    The USART has a 2 byte buffer, so you can only send 16 bytes (or less) to the PC without expecting to see overflows.

    But, In this statement...

    serout2 test,BAUD,["BUFFER=",HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],","
    ,HEX BUFFER[4],",",HEX BUFFER[5],",",HEX BUFFER[6],",",HEX BUFFER[7],10,13] 'JUST CHECK 1ST 2 VALUES


    depending on the values of the data, it will send up to 33 bytes.

    Since you are using ON INTERRUPT, the next interrupt can't happen untill the serout2 statement has finished.
    <br>
    DT

  4. #4
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Talking

    Thanks Darryl,

    You hit my problem on the spot, why are the small simplicities the ones that give the most grief?
    I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well? If not, don't worry.
    I feel I'm on the right track now.

    Thanks All,

    Chris

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well?
    I ... uh ... er ... hem ... haw ...

    Well, there's this ...

    DT-INTS-14
    http://www.darreltaylor.com/DT_INTS-14/intro.html
    <br>
    DT

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crhomberg View Post
    Thanks Darryl,
    You hit my problem on the spot, why are the small simplicities the ones that give the most grief? I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well? If not, don't worry. I feel I'm on the right track now. Thanks All,
    Chris
    Or figure out a way you can send one byte at a time and check for an input in between bytes...

  7. #7
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default

    Tomorrow I want to try Darryl Taylor's instant interrups, I hope that will help.
    When I removed or shortened the serout2 string all worked fine so I think if an assembler interrupt can stop the Serout2 before it completes things should work well. Even If I loose a few serout2 lines it is less important then loosing input packets.

    Regards

    Chris

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Pretty sure DarrEl's routine will help.

    Nothing bad to wait between each character to send... kinda character pacing.. sort of.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  2. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 05:02
  3. Receiving Packet Array In Usart
    By crhomberg in forum Serial
    Replies: 1
    Last Post: - 18th April 2007, 22:31
  4. USART Stops Receiving Characters
    By breesy in forum Serial
    Replies: 7
    Last Post: - 26th November 2006, 03:50
  5. USART Problem Receiving Bytes
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 15th September 2005, 17:50

Members who have read this thread : 0

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