Receiving Packet Array In Usart


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    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.

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


    Did you find this post helpful? Yes | No

    Talking

    Sorry Darrel for spelling your name wrong, I have only tried your blinky program so far but I am really impressed and exited.
    Now I just have to do the same but with the interrupt from the USART receiver.

    Thanks

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


    Did you find this post helpful? Yes | No

    Unhappy

    Dear Darrel,

    If you are still there, please read this adaption of my code to use your instant interrupts.
    Something is still not right, it does not seem to get the values.

    '************************************************* ***************

    'JUST NEED TO SEND THE VALUE IN EACH BYTE ONCE THE PACKET HAS BEEN COLLECTED

    '************************************************* ***************
    '@ device hs_OSC, wdt_on, pwrt_on, protect_off
    define osc 20
    Include "modedefs.bas"
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

    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
    start var bit

    BUFFER VAR BYTE[8] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
    PELCO VAR BYTE[8]
    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
    GOT VAR BIT 'USED TO PREVENT REPEATED SENDING OF SAME PACKET VIA SEROUT2

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag? GOTO MAIN:
    INT_Handler RX_INT, _GETDATA, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor disable
    ENDASM

    @ INT_ENABLE RX_INT ; enable RX USART interrupts



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



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

    MAIN:



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

    IF GOT=1 THEN 'IF THE PACKET IS NEW THEN SEND ELSE JUST KEEP LOOPING
    serout2 test,BAUD,[HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],",",HEX BUFFER[6],10,13] 'JUST CHECK 1ST 2 VALUES
    GOT=0
    ENDIF
    toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
    counter=0
    else
    counter=counter+1
    endif

    goto main

    end


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

    GETDATA:

    TOGGLE PORTD.0
    IF RCIF=1 THEN

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


    IF BYTECOUNTER < 8 THEN
    BUFFER[BYTECOUNTER] = VALUE 'ADD BYTE TO PACKET
    BYTECOUNTER=BYTECOUNTER+1 'INCREMENT COUNTER
    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

    '*** PACKET MUST START WITH $A0 AND HAVE $AF AS IT'S 7TH BYTE. ******
    IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
    GOT=1 'INDICATE THAT THIS IS A NEW PACKET
    GOTO MAIN 'RETURN TO MAIN PROGRAM
    ELSE
    GOTO GETDATA 'GO BACK AND WAIT FOR NEXT PACKET
    ENDIF
    ENDIF


    @ INT_RETURN
    '================================================= ====================================

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Which PIC are you using?
    (unless I missed something, I don't think that's ever been established...)

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


    Did you find this post helpful? Yes | No

    Default

    I am using the PIC16F977A

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crhomberg View Post
    I am using the PIC16F977A
    I missed those datasheets

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Dt Interrupts

    http://www.picbasic.co.uk/forum/showthread.php?t=3251

    Unless I misread your code as follows:
    Code:
    @ device hs_OSC, wdt_on, pwrt_on, protect_off
    You are using PM as the assembler, and according to this link, DT interrupts require MPASM.
    HTH
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default

    i know i miss something here, but i feel lazy to read that carefully... Anyways is the SEROUT2 baudrate should be really 38400 baud, Driven, Inverted, No parity?
    Last edited by mister_e; - 24th April 2007 at 01:03.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    Oh my, look what happened while I was out trying to make a buck, (a.k.a. at work).

    That's the most "unless I missed something"'s I've ever seen in a row.

    Chris,

    You can't jump out of the Interrupt handler back to the main loop.
    Instead, you should just use your GOT flag to indicate reception, then let the main loop handle it at it's leisure.
    Code:
     IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
         GOT=1  'INDICATE THAT THIS IS A NEW PACKET                    
        GOTO MAIN   'RETURN TO MAIN PROGRAM
     ELSE
        GOTO GETDATA   'GO BACK AND WAIT FOR NEXT PACKET
     ENDIF
    ENDIF
                    
    @ INT_RETURN
    I think this might work better for the Main loop ...
    Code:
    MAIN:
        IF GOT=1 THEN  'IF THE PACKET IS NEW THEN SEND ELSE JUST KEEP LOOPING
            GOT=0
            GOSUB ShowBuffer
        ENDIF
    
        if counter= 5000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS
            counter=0 
            GOSUB ShowBuffer
            toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
        ENDIF
    
        counter=counter+1
    goto main
    
    
    '==========================================================================
    ShowBuffer:
        serout2 test,BAUD,[HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2], _
        ",",HEX BUFFER[3],",",HEX BUFFER[6],10,13]  'JUST CHECK 1ST 2 VALUES
    return
    But, you'll still need to fix the Interrupt handler.

    HTH,
    DT

  10. #10
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default Consider this

    Hi,

    In your code you are assuming that the first byte at buffer[0] is the pelco-p header.
    Code:
    IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
    GOT=1 'INDICATE THAT THIS IS A NEW PACKET 
    GOTO MAIN 'RETURN TO MAIN PROGRAM
    ELSE
    GOTO GETDATA 'GO BACK AND WAIT FOR NEXT PACKET
    ENDIF
    ENDIF
    This might not be the case as you may have a dataset misaligned say for one missed byte or corrupted data. So you actually do not know that if your buffer is perfectly aligned. I think you should consider a better algo for checking the PELCO strings.

    I assume are using serout2 for your debug purposes. Why not use the TX part of the USART? Yes limitation are that you need to use the same speed as the RX. DT's INT routines takes a finite time to take action in your PBP routine as it saves all the variables for you and checks all the flags and execution orders (aka all the hardwork that we were supposed to do) but please note that it responds to the interrupt instantly. This may screw up the soft serial out routines of PBP as it will introduce timing jitters in the output serial streams. Hope Darrel can enlighten more.

    I wish that I actually find some spare time this weekend to get something useful for you. Hope you succeed before that.
    Regards

    Sougata

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


    Did you find this post helpful? Yes | No

    Talking

    It's finally running just as I wanted it,

    Thanks very much to skimask, Suagata ans especially Darrel Taylor for your help. I could never have got it right. I now can use this code as a "plug in" for all my 8byte protocol coms projects.

    I hope I will be able to help you someday as you helped me.

    Best Regards

    Chris Rhomberg

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