ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - SERBUFX.pbp

PICBASIC PRO program to demonstrate an interrupt-driven input buffer for Hserin using On Interrupt. Pin definitions compatible with LAB-X1 and PIC16F877.
001' Name        : SERBUFX.pbp
002' Compiler    : PICBASIC PRO Compiler 2.6
003' Assembler   : PM or MPASM
004' Target PIC  : 40-pin 16F877, 877A or similar
005' Hardware    : LAB-X1 Experimenter Board
006' Oscillator  : 4MHz external crystal
007' Keywords    : HSERIN, ON INTERRUPT, LCDOUT
008' Description : PICBASIC PRO program to demonstrate an interrupt-driven
009' input buffer for Hserin using On Interrupt. Pin definitions compatible
010' with LAB-X1 and PIC16F877.
011'
012 
013' Defines for LCD and USART
014Define HSER_RCSTA 90h
015Define HSER_TXSTA 24h
016Define HSER_BAUD 9600
017 
018Define LCD_DREG  PORTD
019Define LCD_DBIT  4
020Define LCD_RSREG PORTE
021Define LCD_RSBIT 0
022Define LCD_EREG  PORTE
023Define LCD_EBIT  1
024 
025RCIF  VAR  PIR1.5       ' Alias RCIF (USART Receive Interrupt Flag)
026OERR  VAR  RCSTA.1      ' Alias OERR (USART Overrun Error Flag)
027CREN  VAR  RCSTA.4      ' Alias CREN (USART Continuous Receive Enable)
028LED   VAR  PORTD.0      ' Alias LED to PORTD.0
029 
030buffer_size  CON  32    ' Sets the size of the ring buffer
031buffer       VAR  BYTE[buffer_size] ' Array variable for holding received characters
032index_in     VAR  BYTE  ' Pointer - next empty location in buffer
033index_out    VAR  BYTE  ' Pointer - location of oldest character in buffer
034bufchar      VAR  BYTE  ' Stores the character retrieved from the buffer
035i            VAR  BYTE  ' loop counter
036col          VAR  BYTE  ' Stores location on LCD for text wrapping
037errflag      VAR  BYTE  ' Holds error flags
038 
039   ADCON1 = 7           ' Set PortA and E to digital operation
040 
041   ' Initialize variables
042   index_in = 0
043   index_out = 0
044   i = 0
045   col = 1
046 
047   Low PORTE.2     ' LCD R/W line low (W)
048   Pause 100       ' Wait for LCD to start
049 
050   LCDOut $fe,1    ' Clear LCD
051 
052   INTCON = %11000000         ' Enable interrupts
053   ON INTERRUPT GoTo serialin ' Declare interrupt handler routine
054   PIE1.5 = 1                 ' Enable interrupt on USART
055 
056' Main program starts here - blink an LED at 1Hz
057mainloop:
058   High LED          ' Turn on LED connected to PORTD.0
059   For i = 0 to 250  ' Delay for .5 seconds (250*2mS)
060     Pause 2         ' Use a short pause within a loop
061   Next i            ' instead of one long pause
062 
063   Low LED           ' Turn off LED connected to PORTD.0
064   For i = 0 to 250  ' Delay for .5 seconds (250*2mS)
065     Pause 2         ' Use a short pause within a loop
066   Next i            ' instead of one long pause
067 
068display:             ' dump the buffer to the LCD
069   IF errflag Then error ' Handle error if needed
070   IF index_in = index_out Then mainloop ' loop if nothing in buffer
071   GoSub getbuf      ' Get a character from buffer  
072   LCDOut bufchar    ' Send the character to LCD
073                 
074   col = col + 1     ' Increment LCD location
075   IF col > 20 Then  ' Check for end of line
076      col = 1        ' Reset LCD location
077      LCDOut $fe,$c0,REP " "\20 ' Clear line-2 of LCD
078      LCDOut $FE,2   ' Tell LCD to return home
079   EndIF
080   GoTo display      ' Check for more characters in buffer
081 
082' Subroutines
083Disable              ' Don't check for interrupts in this section
084 
085getbuf:              ' move the next character in buffer to bufchar
086   index_out = (index_out + 1) ' Increment index_out pointer (0 to 63)
087   IF index_out > (buffer_size-1) Then index_out = 0 ' Reset pointer if outside of buffer
088   bufchar = buffer[index_out] ' Read buffer location
089   Return
090 
091error:               ' Display error message if buffer has overrun
092   IF errflag.1 Then ' Determine the error
093      LCDOut $FE,$c0,"Buffer Overrun" ' Display buffer error on line-2
094   Else
095      LCDOut $FE,$c0,"USART Overrun"  ' Display usart error on line-2
096   EndIF
097         
098   LCDOut $fe,2      ' Send the LCD cursor back to line-1 home
099   For i = 2 to col  ' Loop for each column beyond 1
100     LCDOut $fe,$14  ' Move the cursor right to the right column
101   Next i
102         
103   errflag = 0       ' Reset the error flag
104   CREN = 0          ' Disable continuous receive to clear overrun flag
105   CREN = 1          ' Enable continuous receive
106   GoTo display      ' Carry on     
107         
108' Interrupt handler
109serialin:            ' Buffer the character received
110   IF OERR Then usart_error    ' Check for USART errors
111   index_in = (index_in + 1)   ' Increment index_in pointer (0 to 63)
112   IF index_in > (buffer_size-1) Then index_in = 0 ' Reset pointer if outside of buffer
113   IF index_in = index_out Then buffer_error       ' Check for buffer overrun
114   HSerin [buffer[index_in]]   ' Read USART and store character to next empty location
115   IF RCIF Then serialin       ' Check for another character while we're here  
116   Resume                      ' Return to program
117 
118buffer_error:
119   errflag.1 = 1           ' Set the error flag for software
120 
121' Move pointer back to avoid corrupting the buffer. MIN insures that it ends up within the buffer.     
122   index_in = (index_in - 1) MIN (buffer_size - 1)
123   HSerin [buffer[index_in]] ' Overwrite the last character stored (resets the interrupt flag)
124 
125usart_error:
126   errflag.0 = 1           ' Set the error flag for hardware
127   Resume                  ' Return to program
128 
129   End

Download the program file.