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

    Default Receiving Packet Array In Usart

    I have a system that uses a control protocol of 8 bytes where it has a start byte which is always $A0 and an End byte which is always $AF. The bytes in between are for control and the 8th one is the checksum (I am not worried about that.
    I got my USART recieving the data fine byte by byte but cannot but them in separate variables in correct order or in an array with 8 values.
    I wrote this code but it just stays dead.
    I need the interrupts as the program has to use the bytes to control a camera after it receives the packet and maybe another may come to cancel the previous.

    My Code:
    '************************************************* *******
    '* PELCO PROTOCOL RECEIVER TO RECEIVE 8 BYTES WHEN INTERRUPTED *
    '* PACKETS ALAYS START WITH BYTE1 = $A0 AND BYTE7 = $AF *
    '************************************************* ********
    @ device hs_OSC, wdt_on, pwrt_on, protect_off
    define osc 20
    Include "modedefs.bas"

    RCSTA = %10010000 'Enable the Usart receive function
    SPBRG = 64 'Set to 4800 bps
    INTCON = %11000000 'Enable Global & Periferal Interrups
    PIE1.5=1 'enable receive interrupt flag

    trisc.6=0
    TRISC.7=1 'usart input pin
    TRISA.0=0


    test var porta.0 'Output pin to PC to test values received
    counter var word 'Used in the keep-busy loop
    BAUD CON 16390



    PELCO VAR BYTE[8] 'Packet of data received from the input
    BYTECOUNT VAR BYTE 'Counter to put each byte in it's correct position

    BYTECOUNT=0
    PELCO=0

    '---------------------------------jump over your interrupt routines
    goto main
    '-----------------------------------------------------------------

    on interrupt goto myroutine


    '******************* interrupt routine *********************
    disable

    myroutine:
    if PIR1.5=1 then
    PELCO[BYTECOUNT]=RCREG
    IF RCREG=$AF THEN
    GOSUB SENDATA
    PIR1.5=0
    ELSE
    BYTECOUNT=BYTECOUNT+1
    GOTO MYROUTINE
    ENDIF
    ENDIF

    resume
    enable
    '************************************************* **

    main:

    '****************** Loop to keep the PIC busy *************

    if counter=5000 THEN 'just to keep the pic busy to make sure the
    toggle portd.0 'Toggle LED connected to this pin.
    counter=0 ' interrupts are working.
    else
    counter=counter+1
    endif

    goto main

    end

    '********** Send data to PC to check if correct ********

    SENDATA:
    serout2 test,BAUD,[HEX PELCO[0],HEX PELCO[1],HEX PELCO[2],HEX PELCO[3],HEX PELCO[4],HEX PELCO[5],10,13]
    RETURN

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

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    SPBRG = 64 'Set to 4800 bps
    -------------double check this...

    -------------Are your A/D pins set to analog or digital?

    BAUD CON 16390
    -------------double check this too...

    myroutine:.................
    GOSUB SENDATA
    ...............................
    resume
    enable

    -----------Not good programming practice to jump out of an interrupt routine and hope it comes back into it every time...might not....

    if counter=5000 THEN 'just to keep the pic busy to make sure the
    toggle portd.0 'Toggle LED connected to this pin.
    counter=0 ' interrupts are working.
    else
    counter=counter+1
    endif

    --------------Is the LED toggling?

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


    Did you find this post helpful? Yes | No

    Unhappy

    The value of SPBRG at 64 works fine when I tested byte by byte.

    My serout2 on PORTA.0 is working fine and the loop is looping (led toggling).
    The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.

    Regards


    Chris

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by crhomberg View Post
    The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.
    Which input? The PICs input, PC's input, camera's input...

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


    Did you find this post helpful? Yes | No

    Smile The old way

    I used to receive the packets using SERIN2 and it works quite well but when the data comes one after another too fast I start loosing packets while the program is out doing other things after the serin2 times out.


    '*************** Variables for Receiving Pelco-P data *********

    data_1 VAR BYTE ' Header
    data_2 VAR BYTE ' ID Camera
    data_3 VAR BYTE ' 1st Data
    data_4 VAR BYTE ' 2nd Data
    data_5 VAR BYTE ' 3th DAta
    data_6 VAR BYTE ' 4th DAta
    data_7 VAR BYTE ' Trailer
    data_8 VAR BYTE ' Checksum

    '*************** CONSTANTS ***********************
    baud con 16390'Baud rate for test pin output



    START:


    '**RECEIVE DATA FROM RS485 AND SEE IF IT IS A PELCO-P COMMAND ****

    Receiving:


    SERin2 DATAIN,188,1000,timeout,[data_1,data_2,data_3,data_4,data_5,data_6,data_7,d ata_8]

    timeout:

    if (data_1 <> $A0) then 'Check if the 1st byte is Pelco ($A0).
    goto receiving
    endif

    etc.... etc do the decoding....

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


    Did you find this post helpful? Yes | No

    Default

    I have my PIC receiving data from a camera PTZ control console using RS485 which sends out data every time I move the joystick telling the PIC what to do. I have a RS485 to TTL converter on the input of the PIC

    Regards

    Chris

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Send out bytes using serout one byte at a time instead of a whole string of bytes and in between sending out bytes, do a quick serin check to see if anything is there.

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


    Did you find this post helpful? Yes | No

    Default

    I don't have a choice, the data always comes in groups of 8.
    The serout is just for debugging, not really needed finally.
    I must be able to read in 8 bytes and then check if the 1st one is $A0 and the 7th $AF. When that is true I can use the data. The packets come at 4800bps and in bursts relatively far apart (That is how I got away with serin2 until now, it just lost packets from time to time)
    I need to stop what the program is doing when that 1st byte arrives and then channel in the next 7 into an array or sepparate vars.

    Regards

    Chris

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