Hi, > Hi guys. > > Has anyone got a short piece of code that works off a 16c84 on a 16x1 > LCD module? > I need to test my LCD module, since I can't seem to get it running with > the PIC. > > Using code thats known to work should tell me whether the LCD or my code > is wrong. > Here is a piece of code that works. It uses the LCD in 4 bit mode with D4-D7 connected to RB0-RB3, RS connected to RB7, EN connected to RA3 and RW grounded. The delays are calibrated for a 4MHz crystal. Hope this helps. Niki LIST P=16C84 #INCLUDE P16CXX.INC __FUSES _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC RADIX DEC ;========================================================================= ;Variable Definitions ;========================================================================= C1 equ 0Ch ;General Purpose Counter - Used in Delay C2 equ 0Dh ;General Purpose Counter - Used in Delay Temp equ 0Eh ;General Purpose Temporary Register Flags equ 0Fh ;Holds General bit flags ;========================================================================= ;Flag bits ;========================================================================= LCD_RS equ 0 ;LCD RS line to use on next LCD write ;========================================================================= ;Defines ;========================================================================= #define LCD_EN PORTA, 3 ;========================================================================= ;Code starts here ;========================================================================= ORG 0 bsf STATUS, RP0 ;Bank 1 movlw 00h movwf TRISA^80h ;Set PORTA to all outputs movlw 070h movwf TRISB^80h ;Set PORTB directions movlw 077h movwf OPTION_REG^80h ;Enable pull ups and set prescaler ;to 1:64 TMR0 bcf STATUS, RP0 ;Back to bank 0 clrf PORTA clrf PORTB clrf Flags ;Clear Timer routine variables call Init_LCD ;Initialize the LCD module call WriteString ;Write Hello World goto $ ;========================================================================= ;Writes a string out to the LCD. The string must be terminated by 0 ;========================================================================= WriteString: bsf Flags, LCD_RS clrf C2 Write_Loop: movf C2,W call Get_Char xorlw 0 btfsc STATUS,Z return call LCDWrite2 incf C2,F goto Write_Loop Get_Char: addwf PCL, F My_String: retlw 'H' retlw 'e' retlw 'l' retlw 'l' retlw 'o' retlw ' ' retlw 'W' retlw 'o' retlw 'r' retlw 'l' retlw 'd' retlw '!' retlw 0 ;========================================================================= ;Delays about C1*(768/Fcyc) seconds) ;========================================================================= Delay: clrf C2 Loop1: decfsz C2,F goto Loop1 decfsz C1,F goto Loop1 return ;========================================================================= ;Writes a single nibble to the LCD. Used by the Init_LCD routine. ;========================================================================= LCDWrite1: movwf PORTB ;Write Data nibble to PORT B bsf LCD_EN ;Set enable line bcf LCD_EN ;Clear enable line return ;========================================================================= ;Writes a byte to the LCD. The byte is written upper nibble first, lower ;nibble second. ;Registers used : W, Temp ;========================================================================= LCDWrite2: movwf Temp ;Use Temp to hold value swapf Temp,F movlw 0Fh andwf Temp,W ;Mask out upper 4 bits of Temp btfsc Flags,LCD_RS ;Must RS line be set? iorlw 80h ;Set RS line movwf PORTB ;Write data Nibble to PORT B bsf LCD_EN ;Set enable line bcf LCD_EN ;Clear enable line swapf Temp,F movlw 0Fh andwf Temp,W ;Mask out lower 4 bits of Temp btfsc Flags,LCD_RS ;Must RS line be set? iorlw 80h ;Set RS line movwf PORTB ;Write data nibble to PORT B bsf LCD_EN ;Set enable line bcf LCD_EN ;Clear enable line movlw 52 movwf Temp ;Delay 150us on 4MHz XTAL LCD_Delay: decfsz Temp,F goto LCD_Delay return ;========================================================================= ;This subroutine initializes the LCD module according to the procedure ;described in the Hitachi docs. The LCD is never read from, so all ;operations are spaced slightly further apart than the maximum time ;that operation can take. ;Registers used : W, C1, C2, Temp ;========================================================================= Init_LCD: movlw 20 ;This will delay just more than 15ms movwf C1 call Delay movlw 03h ;First Init Byte call LCDWrite1 movlw 7 ;Delay at least 5ms movwf C1 call Delay movlw 03h ;Second Init Byte call LCDWrite1 movlw 1 ;Delay at least 150us movwf C1 call Delay movlw 03h ;Third Init Byte call LCDWrite1 movlw 1 ;Delay at least 150us movwf C1 call Delay movlw 02h ;Choose 4 bit interface call LCDWrite1 movlw 1 ;Delay at least 150us movwf C1 call Delay bcf Flags, LCD_RS ;RS line must be low for the following movlw 20h ;Select 1 line 5X7 call LCDWrite2 ;Write byte as two nibbles movlw 08h ;Display off Command call LCDWrite2 movlw 0Fh ;Display on, Cursor On, Blink on call LCDWrite2 movlw 06h ;Increment Curser, no shift call LCDWrite2 movlw 01h ;Make sure display is cleared call LCDWrite2 movlw 7 ;Delay at least 5ms, to clear display movwf C1 call Delay return end