ON INTERRUPT USAR! variable


Closed Thread
Results 1 to 2 of 2
  1. #1
    egds's Avatar
    egds Guest

    Default ON INTERRUPT USAR! variable

    Hello,

    I need to save in a variable some ASCII character.
    I´m actually with USAR interrrupt using ON INTERRUPT PIC BASCI COMMAND.

    I want to save all serial characters that i received in a variable and after be able to send with hserout command.
    My code it here: This code is actually run well, but just send the latest character and not all the characters recieved.
    *********************************************
    INCLUDE "bs2defs.bas"
    DEFINE LOADER_USER 1
    DEFINE OSC 20
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_BAUD 9600



    ADCON1 = 6 ' Set PortA to digital operation
    RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag)
    OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag)
    CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable)
    LED VAR PORTB.7 ' Alias LED to PORTB.7

    buffer_size CON 32 ' Sets the size of the ring buffer
    buffer VAR BYTE[Buffer_size] ' Array variable for holding received characters
    index_in VAR BYTE ' Pointer - next empty location in buffer
    index_out VAR BYTE ' Pointer - location of oldest character in buffer
    bufchar VAR BYTE
    bufchar1 VAR BYTE
    bufchar2 var byte
    posicion VAR BYTE
    cont var byte
    ' Stores the character retrieved from the buffer
    i VAR BYTE ' loop counter
    errflag VAR BYTE





    ' Holds error flags

    ' Initialize variables
    cont=0
    index_in = 0
    index_out = 0


    INTCON = %11000000 ' Enable interrupts
    On INTERRUPT GoTo serialin ' Declare interrupt handler routine
    PIE1.5 = 1 ' Enable interrupt on USART

    ' Main program starts here - blink an LED at 1Hz

    Loop:

    High LED ' Turn on LED connected to PORTD.0
    For i = 0 To 250 ' Delay for .5 seconds (250*2mS)
    Pause 2 ' Use a short pause within a loop
    Next i ' instead of one long pause
    Low LED ' Turn off LED connected to PORTD.0
    For i = 0 To 250 ' Delay for .5 seconds (250*2mS)
    Pause 2 ' Use a short pause within a loop
    Next i
    if porta.2=1 then calle

    display:

    IF errflag Then error

    If index_in = index_out Then Loop ' loop if nothing in buffer



    'gosub getbuf ':
    'leemos buffer hasta encontrar las primeras "
    'gosub getbuf
    'hserout[bufchar]
    'PAUSE 1000
    'hserout[bufchar1]

    'GOTO display

    ' Check for more characters in buffer
    ' Subroutines

    Disable ' Don't check for interrupts in this section

    getbuf: ' move the next character in buffer to bufchar
    index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
    If index_out > (buffer_size - 1) Then index_out = 0 ' Reset pointer if outside of buffer
    bufchar1=buffer[index_out]
    goto display
    calle:
    hserout["AT+CMFG=",34,"67741418",34,bufchar1]
    goto display




    'Return
    error: ' Display error message if buffer has overrun
    IF errflag.1 Then error1 ' Determine the error
    error1:
    hserout["Buffer Overrun"] ' Display buffer error on line-2
    'Else
    hserout["USART Overrun"] ' Display usart error on line-2
    'End If

    'LCDOut $fe,2 ' Send the LCD cursor back to line-1 home
    'For i = 2 To col ' Loop for each column beyond 1
    'LCDOut $fe,$14 ' Move the cursor right to the right column
    'Next i

    errflag = 0 ' Reset the error flag
    CREN = 0 ' Disable continuous receive to clear overrun flag
    CREN = 1 ' Enable continuous receive

    GoTo display ' Carry on


    ' Interrupt handler


    serialin:
    disable
    HIGH PORTB.3
    ' Buffer the character receive
    If OERR Then usart_error ' Check for USART errors
    index_in = (index_in + 1) ' Increment index_in pointer (0 to 63)
    If index_in > (buffer_size - 1) Then index_in = 0 'Reset pointer if outside of buffer
    If index_in = index_out Then buffer_error ' Check for buffer overrun
    HSerin [buffer[index_in]]
    LOW PORTB.3
    IF RCIF then serialin ' Read USART and store character to next empty locationIf RCIF Then serialin ' Check for another character while we're here

    Resume
    enable ' Return to program

    buffer_error:
    errflag = 1 ' Set the error flag for software
    ' Move pointer back to avoid corrupting the buffer. MIN insures that it ends up within the buffer.
    index_in = (index_in - 1) MIN (buffer_size - 1)
    HSerin [buffer[index_in]] ' Overwrite the last character stored (resets the interrupt flag)
    usart_error:
    errflag = 1 ' Set the error flag for hardware

    Resume ' Return to program


    End
    *************************
    So i can´t save more than 1 character.

    Some suggestions?

    Thanks a lot.

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Edgs,

    Doing a VERY quick lookthrough on your code, I see one major problem (unless I am missing something)

    You are calling a routine thorugh a interupt, then using disable. (AS the code below).

    then you are looping out of this disable interupt without calling the enable again. Is that what you want??? For a guess, it will only work 1 time (first time) and fail all other times.

    Here is the code... Notice the Disable is not commented out, so that tells me the interupt is negated. then the 7th line down you branch out of it to "Display", and from Display you go to "Loop"

    Are you sure you don't want to enable it again sometime?

    E>>
    Disable ' Don't check for interrupts in this section

    getbuf: ' move the next character in buffer to bufchar
    index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
    If index_out > (buffer_size - 1) Then index_out = 0 ' Reset pointer if outside of buffer
    bufchar1=buffer[index_out]
    goto display
    calle:
    hserout["AT+CMFG=",34,"67741418",34,bufchar1]
    goto display
    <<
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. Interrupt Problem
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th November 2005, 20:58
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

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