Ok enough of my whining, here is some workable code, thanks to Darrel Taylor, well mostly about 99 percent. He wrote it for something else, I added a monkey wrench and a screwdriver and it works for my needs, could it be better? Well you can judge for your self, it works good enough for my application as is
Thanks Once again Darrel for the post I extracated this code from.
As I gain coding experience I will revisit this code unless someone else does so first ;-)Code:@ DEVICE pic16F628A, HS_OSC @ DEVICE pic16F628A, WDT_OFF ' Watchdog Timer @ DEVICE pic16F628A, PWRT_ON ' Power-On Timer @ DEVICE pic16F628A, MCLR_ON ' Master Clear Options (Internal) @ DEVICE pic16F628A, BOD_OFF ' Brown-Out Detect @ DEVICE pic16F628A, LVP_OFF ' Low-Voltage Programming @ DEVICE pic16F628A, CPD_OFF ' Data Memory Code Protect ' Set to CPD_OFF for Development Copy ' Set to CPD_ON for Release Copy @ DEVICE pic16F628A, PROTECT_OFF ' Program Code Protection ' Set to PROTECT_OFF for Development Copy ' Set to PROTECT_ON for Release Copy trisb = %00000010 trisA = %11110011 include "modedefs.bas" ' Define LCD registers and bits Define LCD_DREG PORTB Define LCD_DBIT 4 Define LCD_RSREG PORTA Define LCD_RSBIT 0 Define LCD_EREG PORTA Define LCD_EBIT 1 DEFINE LCD_LINES 4 'Define using a 2 line LCD DEFINE LCD_COMMANDUS 2000 'Define delay time between sending LCD commands DEFINE LCD_DATAUS 50 'Define delay time between data sent. DEFINE OSC 20 DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0 DEFINE HSER_SPBRG 32 ' FOR 20MHZ 129 = 2400, 32=9600,25 @ 4 for 2400 DEFINE HSER_CLROERR 1 ' Clear overflow automatically RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty) TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full) LED VAR PORTA.4 OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag) CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable) buffer_size CON 64 ' Sets the size of the ring buffer, set up from 32 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 Col VAR BYTE ' Stores location on LCD for text wrapping errflag VAR BYTE ' Holds error flags index_in = 0 index_out = 0 i = 0 col = 1 'RxData var byte CMCON = 7 ' PORTA is digital Pause 100 ' Wait for LCD to startup high PortA.2 ' power for backlight low PortA.3 ' backlight ground INTCON = %11000000 ' Enable interrupts ON INTERRUPT GoTo serialin ' Declare interrupt handler routine PIE1.5 = 1 ' Enable interrupt on USART pause 1500 lcdout $FE,1 lcdout $FE,2 LCDOUT "Your Text Goes Here" PAUSE 2000 ' * * * * * * * * * * * * * Main program starts here - blink an LED at 1Hz ' I removed some code here, it seems to require what's left loop: For i = 0 to 10 ' Delay for .02 seconds (10*2mS) Pause 2 ' Use a short pause within a loop Next i ' instead of one long pause For i = 0 to 10 ' Delay for .02 seconds (10*2mS) Pause 2 ' Use a short pause within a loop Next i ' instead of one long pause display: ' dump the buffer to the LCD IF errflag Then error ' Handle error if needed IF index_in = index_out Then loop ' loop if nothing in buffer GoSub getbuf ' Get a character from buffer LCDOut bufchar ' Send the character to LCD IF col > 20 Then ' Check for end of line col = 1 ' Reset LCD location LCDOut $fe,$c0,REP " "\20 ' Clear line-2 of LCD LCDOut $FE,2 ' Tell LCD to return home EndIF 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) ' Reset pointer if outside of buffer IF index_out > (buffer_size-1) Then index_out = 0 bufchar = buffer[index_out] ' Read buffer location Return error: ' Display error message if buffer has overrun IF errflag.1 Then ' Determine the error LCDOut $FE,$c0,"Clearing Display Buffer" ' Display buffer error on ' line-2 and 3 Buff overrun Else LCDOut $FE,$D4,"USART Overrun" ' Display usart error on line-4 EndIF 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 ' $14 = 20 DEC. errflag = 0 ' Reset the error flag CREN = 0 ' Disable continuous receive to clear overrun flag CREN = 1 ' Enable continuous receive GoTo display ' Errors cleared, time to work. ' * * * * * * * * * * * * * * * Interrupt handler serialin: ' Buffer the character received 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 IF RCIF Then serialin ' Check for another character while we're here Resume ' Return to program 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 End
JS
Oh, what I really like about this is you use the standard formatting commands as used with LCDOUT, no funky commands.




Bookmarks