title "LcdTerminal 16F873 or 16F628" ; ; ; Hardware Notes: ; 20 MHz osc 115200 baud ; ; Ted Rossin: 04-13-2003 ; 06-05-2003 ; ************************************************ ; ** ** ; ** Special characters: ** ; ** 0x01 Home ** ; ** 0x08 Back space ** ; ** 0x09 Tab ** ; ** 0x0a Line feed ^J ** ; ** 0x0c Form feed (Clear display) ** ; ** 0x0d Carriage return ^M ** ; ** 0x10 Move to X,Y (X and Y are the ** ; ** next two characters ** ; ** 0x1b Escape ** ; ** 0x00 Disable cursor ** ; ** 0x01 Enable underscore cursor ** ; ** 0x02 Enable block cursor ** ; ** 0x03 Scroll display left ** ; ** 0x04 Scroll display right ** ; ** 0x05 Enable Display ** ; ** 0x06 Disable Display ** ; ** 0x07 Raw LCD controller command ** ; ** 0x08 Disable vertical scroll ** ; ** 0x09 Enable vertical scroll(default)** ; ** 0x10 Write Custom character 0 ** ; ** Next 8 bytes form character ** ; ** 0x11 Write Custom character 1 ** ; ** Next 8 bytes form character ** ; ** 0x12 Write Custom character 2 ** ; ** Next 8 bytes form character ** ; ** 0x13 Write Custom character 3 ** ; ** Next 8 bytes form character ** ; ** 0x14 Write Custom character 4 ** ; ** Next 8 bytes form character ** ; ** 0x15 Write Custom character 5 ** ; ** Next 8 bytes form character ** ; ** 0x16 Write Custom character 6 ** ; ** Next 8 bytes form character ** ; ** 0x17 Write Custom character 7 ** ; ** Next 8 bytes form character ** ; ** 0x80 Display custom character 0 ** ; ** 0x81 Display custom character 1 ** ; ** 0x82 Display custom character 2 ** ; ** 0x83 Display custom character 3 ** ; ** 0x84 Display custom character 4 ** ; ** 0x85 Display custom character 5 ** ; ** 0x86 Display custom character 6 ** ; ** 0x87 Display custom character 7 ** ; ** ** ; ************************************************ LIST R=DEC ; Used for development #ifdef __16F873 #include "p16f873.inc" #define LCD_RS PORTC,2 #define LCD_E PORTC,3 #define BAUD_SELECT PORTC,4 __CONFIG _CP_OFF & _DEBUG_OFF & _HS_OSC & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _WRT_ENABLE_ON & _BODEN_ON & _CPD_OFF #endif ; Used for product #ifdef __16F628 #include "P16f628.inc" #define LCD_RS PORTA,2 #define LCD_E PORTA,3 #define BAUD_SELECT PORTA,5 #define _CPD_OFF 0x3fff ; Missing define #define _CPD_ON 0x3eff ; Missing define __CONFIG _HS_OSC & _CP_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF #endif #define PRESCALE_TIMER_MODE 0xD0 #define PRESCALE_VALUE (PRESCALE_TIMER_MODE | 0x8) ; Set prescaler to 1/1 #define TERM_HOME 0x01 ; SOH #define TERM_BS 0x08 ; BS #define TERM_TAB 0x09 ; HTAB #define TERM_LF 0x0a ; LF (Line feed) ^J #define TERM_CLEAR 0x0c ; FF #define TERM_CR 0x0d ; CR (Carriage return) ^M #define TERM_POS 0x10 ; Move to X,Y #define TERM_ESC 0x1b ; More fun ahead #define ESC_DISABLE_CURSOR 0x00 #define ESC_ENABLE_UNDER_CURSOR 0x01 #define ESC_ENABLE_BLOCK_CURSOR 0x02 #define ESC_SCROLL_LEFT 0x03 #define ESC_SCROLL_RIGHT 0x04 #define ESC_ENABLE_DISPLAY 0x05 #define ESC_DISABLE_DISPLAY 0x06 #define ESC_RAW_COMMAND 0x07 #define ESC_DISABLE_VERT_SCROLL 0x08 #define ESC_ENABLE_VERT_SCROLL 0x09 #define ESC_WRITE_CUSTOM 0x10 #define DISPLAY_CUSTOM_MASK 0xf8 #define DISPLAY_CUSTOM_VALUE 0x80 #define STATE_NORMAL 0 #define STATE_WAIT_X 1 #define STATE_WAIT_Y 2 #define STATE_WAIT_COMMAND 3 #define FLAGS_DISABLE_VERT_SCROLL 0 ; RAM #define FIFO_START 0xA0 #define FIFO_END 0xEF #define REG_START 0x020 cblock REG_START _w, _status, _fsr ; Context Register Save Values Amount DelayL,DelayH LcdX,LcdY Tmp,Tmp2 Loop1 Count State Flags LastRS232Byte CgRamAddr Param:2 ; Parameters ; Input FIFO Empty FifoWritePtr FifoReadPtr ; Scroll buffer ScrollBuffer1:20 ScrollBuffer2:20 ScrollBuffer3:20 endc org 0 Startup: nop nop nop goto Main ; ************************************************ ; ** ** ; ** Interrupt handler ** ; ** ** ; ************************************************ org 4 Int: movwf _w ; Save Context Registers movf STATUS, w ; - Assume TMR0 is the only enabled Interrupt movwf _status movf FSR,w movwf _fsr bcf PIR1,RCIF ; Clear the recieved data flag ; Write byte to FIFO to full check movf FifoWritePtr,w movwf FSR movf RCREG,w movwf INDF bcf Empty,0 ; Clear the empty flag incf FifoWritePtr,f movf FifoWritePtr,w sublw FIFO_END btfss STATUS,Z ; Skip if need to wrap write pointer goto IntEnd movlw FIFO_START movwf FifoWritePtr IntEnd: movf _fsr,w ; Restore registers movwf FSR movf _status, w movwf STATUS swapf _w, f swapf _w, w retfie ; ************************************************************************ ; ** ** ; ** GetRS232Byte: Waits for a byte to be received and then returns it ** ; ** in w. ** ; ** ** ; ************************************************************************ GetRS232Byte: btfsc Empty,0 ; Wait for byte to show up. goto $-1 movf FifoReadPtr,w ; Fetch byte from FIFO movwf FSR movf INDF,w movwf Tmp incf FifoReadPtr,f movf FifoReadPtr,w sublw FIFO_END btfss STATUS,Z ; Skip if need to wrap write pointer goto GetRS232ByteEnd movlw FIFO_START movwf FifoReadPtr GetRS232ByteEnd movf FifoReadPtr,w subwf FifoWritePtr,w btfss STATUS,Z goto GetRS232ByteSkip bsf Empty,0 ; Set the empty flag GetRS232ByteSkip movf Tmp,w return ; ************************************************************************ ; ** ** ; ** SendRS232Byte: Waits for transmit to be ready and then sends byte ** ; ** in w to the RS-232 port ** ; ** ** ; ************************************************************************ SendRS232Byte: btfss PIR1,TXIF goto $-1 movwf TXREG return; ; ************************************************************************ ; ** ** ; ** InitRS232: ** ; ** Initialize the RS-232 port for recieve interrupt operation. ** ; ** ** ; ************************************************************************ InitRS232: bcf STATUS,RP0 bcf STATUS,RP1 ; Select Bank 0 bsf STATUS,RP0 ; Select Bank 1 registers #ifdef __16F873 bsf TRISC^0x80,7 ; PORTC[RX] is Input bcf TRISC^0x80,6 ; PORTC[TX] is Output #else bsf TRISB^0x80,1 ; PORTB[RX] is Input bcf TRISB^0x80,2 ; PORTB{TX] is Output #endif bcf STATUS,RP0 ; Select Bank 0 movlw 129 ; Set baud to 9600 btfss BAUD_SELECT ; Check to see if High baud rate movlw 10 ; Set baud to 115200 bsf STATUS,RP0 ; Select Bank 1 registers ; movlw 20 ; Set baud to 57600 movwf SPBRG^0x80 ; movlw (1<