Interrupt driven Buffered Serial Input


Results 1 to 16 of 16

Threaded View

  1. #1
    Join Date
    May 2007
    Posts
    17

    Default Interrupt driven Buffered Serial Input

    Hi, I'm going mad for the past few days here since my interrupts are not working. I am trying to make a interrupt driven buffered serial input but without success since my program doesn't do anything when it should deal with the interrupts (it just stays on that "While CounterA <= 70 : WEND" forever) (nothing is printed out when I send something to it's serial port). Here is my code (part of it at least that deals with the serial part)

    Code:
        DEFINE OSC 20
        
        DEFINE HSER_TXSTA 24h
        DEFINE HSER_RCSTA 90h
        DEFINE HSER_BAUD 19200
        DEFINE HSER_SPBRG 64 ' 19200 Bauds
        DEFINE HSER_CLROERR 1
    
        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	PORTD.0 	' Alias LED to PORTD.0
    
        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		' Stores the character retrieved from the buffer
        i		VAR	BYTE		' loop counter 
        errflag	VAR	BYTE		' Holds error flags
    
        CMCON=%00000111		' Disable Comparators
        TRISA=%00000000		' PORTA all set to Output 
        TRISB=%11111111		' PORTB all set to Input
        T0CON=%00000111     ' OFF,16BIT,PRESCALE=256
        ADCON0 = 0
        CMCON=7
        INTCON.5=1          'T0IE : TMR0 Overflow Interrupt Enable bit
    
    ON interrupt GOTO GestIntrerupts        ' Deal with interrupts
    
    ReadSerial:
       DataRec var byte[5] : DataRec = 0
       PIE1.5 = 1   			' Enable interrupt on USART
       While CounterA <= 70 : WEND
       PIE1.5 = 0				' Disable interrupt on USART
            
       FOR COUNTERB=0 to COUNTERA
            SEROUT2 SerWRT,16416,[SerDATA(COUNTERB)]
            NEXT COUNTERB
    
       Goto ReadReadyLoop 
    
    
    ' 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
    	bufchar = buffer[index_out]			' Read buffer location
    Return
    
    error:					' Display error message if buffer has overrun
    	errflag = 0			' Reset the error flag
    	CREN = 0			' Disable continuous receive to clear overrun flag
    	CREN = 1			' Enable continuous receive
    	GoTo display		' Carry on
    
    buffer_error:
    	errflag.1 = 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.0 = 1		' Set the error flag for hardware
    Resume					' Return to program
    
    GestIntrerupts:
        IF INTCON.2 = 1 then
            T0CON.0 = 0 ' Disable timer
            TMR0L = 0   ' Reset timer
            TMR0H = 0   ' Reset timer
            TimeCount = TimeCount + 1
            T0CON.0 = 1 ' Enabled timer
            INTCON.2 = 0
        ELSE : IF RCIF = 1 THEN
            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]]			' Read USART and store character to next empty location
            SEROUT2 SerWRT,16416,[buffer(index_in)]
            SerData(CounterA)=buffer(index_in)
            CounterA = CounterA + 1	    
    	    IF RCIF Then GestIntrerupts   		' Check for another character while we're here 
        ENDIF : ENDIF
        resume
    ENABLE
    Any input is appreciated!
    Last edited by johnmiller; - 10th May 2007 at 18:06.

Similar Threads

  1. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  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. 18F2480 asm interrupt
    By Richard Storie in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th March 2009, 19:40
  4. Serial Question + General Review
    By Freman in forum General
    Replies: 2
    Last Post: - 20th June 2008, 22:27
  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