Interrupt driven Buffered Serial Input


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    May 2007
    Posts
    17


    Did you find this post helpful? Yes | No

    Default

    I tried this code:

    Code:
    ReadSerial:
       DataRec var byte[5] : DataRec = 0
       KP var byte : KP = 0
       
       While CounterA <= 70
          IF index_in > (buffer_size-1) Then index_in = 0	'Reset pointer if outside of buffer
    	  HSerin [buffer[index_in]]		   ' Read USART and store character to next empty location
    
    	  if buffer(index_in) = $20 then 
             index_in = 0
             for CounterB=0 to buffer_size
                 buffer(CounterB) = 0
                 next CounterB
             KP = 1
          ENDIF
    
          if ((KP = 1) AND (index_in=4)) THEN
             SerData(CounterA)=buffer(4)
             CounterA = CounterA + 1
             KP = 1
          ENDIF
          index_in = index_in + 1
       WEND
    and it still doesn't work any ideea why.

    PS: Is it better than the old one?
    Last edited by johnmiller; - 11th May 2007 at 19:30.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Code:
    @ DEVICE PIC16F628A , HS_OSC , WDT_OFF , PWRT_ON , MCLR_ON , BOD_ON , LVP_OFF , PROTECT_OFF
    'HS 20mhz external, watchdog off, powerup timer on, mclr external, brown out detect on, low volt program off , code protect off
    
    resetplaceholder: 'wordpad, arial black, size 8, reg, 1600x1200 screen, 16f628A code
    DEFINE	OSC	20	'20mhz
    
    serdata var byte : serialbuffer var byte[255] : bufferpointer var byte
    bufferdata var byte : temp var byte
    
    startupholder:	goto skipsubs	'skip over all the commonly used subroutines
    
    ON INTERRUPT GOTO INTHANDLER
    DISABLE INTERRUPT
    INTHANDLER:	if pir1.5 = 1 then	'if serial data rx'd
    			serdata = rcreg		'save serial port data right away
    			pir1.5 = 0		'reset the RX flag
    			if ( rcsta.1 = 1 ) or ( rcsta.2 = 1 ) then	'check for err, if frame/overrun error,
    				rcsta.4 = 0 : rcsta.4 = 1 : serdata = rcreg : serdata = 0
    			else
    				if bufferpointer = 255 then goto intfinish	'buffer is full
    				serialbuffer[bufferpointer] = serdata
     : bufferpointer = bufferpointer + 1	'save data in buffer, bump pointer
    				goto INTHANDLER	're-check serial port just in case another character came in while saving data
                                                            '...not likely
    			endif
    		endif
    
    intfinish:	RESUME
    ENABLE INTERRUPT
    
    getcharacterfrombuffer:
    		if bufferpointer = 0 then return	'no data in buffer in the first place!
    		buffereddata = serialbuffer[0]	'get oldest data from buffer
    		for temp = 0 to 254 : serialbuffer[temp] = serialbuffer[temp+1] : next temp	'shift data down one byte
    		serialbuffer[255]=0	'set last byte in buffer to zero since it's meaningless
    		bufferpointer = bufferpointer - 1	'one less byte in buffer
    		return
    
    skipsubs:	option_reg=8 : pie1=32 : trisa=0 : porta=0 : trisb=$ef : portb=16 : t1con=0 : t2con=0
    		cmcon=7 : ccp1con=0 : vrcon=0 : txsta=0 : rcsta=$90 : pir1.5=0 : spbrg=33 : intcon=$e0	'9600 baud
    		for temp = 0 to 255 : serialbuffer[temp] = 0 : next temp
    
    mainloop:	goto mainloop	'do it all over again
    
    END
    I modified one of my old programs. This works for me, always has.
    I've left the character handling and the modifications of the program to you to make work for your PIC.
    Last edited by skimask; - 11th May 2007 at 20:15.

  3. #3
    Join Date
    May 2007
    Posts
    17


    Did you find this post helpful? Yes | No

    Default

    I tried your code, and now I tried the one above and still does not work
    Code:
    ReadSerial:
       DataRec var byte[5] : DataRec = 0
       
       While CounterA <= 55
            HSERIN [serdata2]        'save serial port data right away
            if serdata2 = $13 THEN
               i = 0
               KP = 1
            ENDIF
            if KP = 1 then
               IF i=5 THEN
                  SerData(CounterA) = serdata2
                  CounterA = CounterA + 1
                  KP = 0
               endif
               i = i + 1
            endif
       wend
    
       FOR COUNTERB=42 to COUNTERA
            SEROUT2 SerWRT,16416,[hex2 SerDATA(COUNTERB), 10, 13]
            NEXT COUNTERB
       Goto ReadReadyLoop
    I tried this one as well and it doesn't work either:
    Code:
        SERIN2 [WAIT($20), SKIP 4, serdata2]
    PS: it doesn't work it's like the pic is not reading the data fast enough. It is supposed to read the data from a keyboard that than sends data over a serial link at 19200 (not inverted).

    All the above codes work if I press a key once per second or so, but they don't work any faster. Anybody can tell me why
    Last edited by johnmiller; - 12th May 2007 at 08:23.

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    I think I can reasonably assume that what you posted isn't all of your code that you are trying to make work.
    And if it is...well, where's the beef?
    Post the whole thing if you want any useful help...

  5. #5
    Join Date
    May 2007
    Posts
    17


    Did you find this post helpful? Yes | No

    Default

    Well this is the only part where I have problems and I don't think pasting 80 pages will do any good
    I really don't understand what's wrong with my code

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by johnmiller View Post
    I don't think pasting 80 pages will do any good
    I really don't understand what's wrong with my code
    And I guess neither will we...

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


    Did you find this post helpful? Yes | No

    Default

    here's the plan... shrink the whole thing and test with a simple program... simple main loop (led blinking or else) + USART INT. Then post the whole new thing.
    Steve

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

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