Programmable LCD counter

 
  • Designed by: Abdulla M. Abdul-Hadi
  • Project: Programmable LCD counter
  • Date: 11/11/2008

-------------------------------------------------------------------------------------------

     Hi, this is a programmable LCD counter tutorial, you can use this tutorial with many applications like display the speed of motors in a motor control system, I wrote the program in asm. language, the Code and schematic shown below, if you have any questions about this tutorial please leave a comment or contact me on my e-mail.

 The Schematic:

 
 
 
 
Short Video:
 

YouTube Video

;Name:Abdulla M. Abdul-Hadi        ;

;Project: Programable LCD Counter  ;

;Date: 11/11/2008                  ;

;**********************************;

 

PROCESSOR 16F877A
         ; Clock = XT 4MHz, standard fuse settings
 __CONFIG 0x3731
 
 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
    ;     LABEL EQUATES      ;
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
 
 INCLUDE  "P16F877A.INC"               ; Standard labels
 Timer1  EQU 20                          ; 1ms count register
 TimerX  EQU 21                          ; Xms count register
 Var   EQU 22                          ; Output variable
 Point   EQU 23                          ; Program table pointer
 Select  EQU 24                          ; Copy of RS bit
 OutCod  EQU 25                          ; Temp store for output
 RS    EQU 1                              ; Register select bit
 E        EQU 2                               ; Display enable
                               
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;                           
    ;    PROGRAM BEGINS        ;
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
    
    
  ORG 0                                ; Place machine code
  NOP                                  ; for ICD mode
  BANKSEL TRISB                          ; Select bank 1
  CLRW                                  ; All outputs
  MOVWF TRISB                           ; Initialise display port
  BANKSEL PORTB                         ; Select bank 0
  CLRF PORTB                            ; Clear display outputs
  GOTO Start                            ; Jump to main program
                                
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;                            
    ;     SUBROUTINES        ;
    ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
                                
                                
                                           ; 1ms delay with 1us cycle time (1000 cycles)-------------------
 Onems
    MOVLW D'249'                    ; Count for 1ms delay
    MOVWF Timer1                         ; Load count
 Loop1
    NOP                               ; Pad for 4 cycle loop
    DECFSZ Timer1                         ; Count
    GOTO Loop1                            ; until Z
    RETURN                                 ; and finish
                                
                                  
                                        ; Delay Xms, X received in W -----------------------------------

    Xms
   MOVWF TimerX                         ; Count for X ms
 LoopX
   CALL Onems                       ; Delay 1ms
   DECFSZ TimerX                          ; Repeat X times
   GOTO LoopX                             ; until Z
   RETURN                               ; and finish
                               
                                  
                                       ; Generate data/command clock siganl E -------------------------

    PulseE
      BSF PORTB,E                      ; Set E high
   CALL Onems                            ; Delay 1ms
   BCF PORTB,E                            ; Reset E low
   CALL Onems                             ; Delay 1ms
   RETURN                                 ; done
                               
                               
                                       ; Send a command byte in two nibbles from RB4 - RB7 ------------

 Send
   MOVWF OutCod                ; Store output code
   ANDLW 0F0                        ; Clear low nybble
   MOVWF PORTB                      ; Output high nybble
   BTFSC Select,RS                  ; Test RS bit
   BSF PORTB,RS                     ; and set for data
   CALL PulseE                      ; and clock display
   CALL Onems                       ; wait 1ms for display
   SWAPF OutCod                     ; Swap low/high nybbles
   MOVF OutCod,W                    ; Retrieve output code
   ANDLW 0F0                        ; Clear low nybble
   MOVWF PORTB                      ; Output low nybble
   BTFSC Select,RS                  ; Test RS bit
   BSF PORTB,RS                     ; and set for data
   CALL PulseE                      ; and clock display
   CALL Onems                       ; wait 1ms for display
   RETURN                           ; done
                               
                                
                                 ; Table of fixed characters to send ----------------------------

  
 Line1
   ADDWF PCL                  ; Modify program counter
   RETLW 'C' ; Pointer = 0
   RETLW 'O' ; Pointer = 1
   RETLW 'N' ; Pointer = 2
   RETLW 'S' ; Pointer = 3
   RETLW 'T' ; Pointer = 4
   RETLW ':' ; Pointer = 5
   RETLW '0' ; Pointer = 6
   RETLW '1' ; Pointer = 7
   RETLW '2' ; Pointer = 8
   RETLW '3' ; Pointer = 9
      RETLW '4' ; Pointer = 10
   RETLW '5' ; Pointer = 11
   RETLW '6' ; Pointer = 12
      RETLW '7' ; Pointer = 13
   RETLW '8' ; Pointer = 14
   RETLW '9' ; Pointer = 15
  
  Line2
   ADDWF PCL ; Modify program counter

   RETLW 'V' ; Pointer = 0
   RETLW 'A' ; Pointer = 1
   RETLW 'R' ; Pointer = 2
   RETLW 'I' ; Pointer = 3
   RETLW 'A' ; Pointer = 4
   RETLW 'B' ; Pointer = 5
   RETLW 'L' ; Pointer = 6
   RETLW 'E' ; Pointer = 7
   RETLW ' ' ; Pointer = 8
   RETLW '=' ; Pointer = 9
   RETLW ' ' ; Pointer = 10
                              
 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;                             
 ;    Initialise the display     ;
 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

  Init
    MOVLW D'100'              ; Load count 100ms delay
   CALL Xms                       ; and wait for display
   MOVLW 0F0                      ; Mask for select code
   MOVWF Select                   ; High nybble not masked
   MOVLW 0x30                     ; Load initial nibble
   MOVWF PORTD                    ; and output it to display
   CALL PulseE                    ; Latch initial code
   MOVLW D'5'                     ; Set delay 5ms
   CALL Xms                       ; and wait
   CALL PulseE                    ; Latch initial code again
   CALL Onems                     ; Wait 1ms
   CALL PulseE                    ; Latch initial code again
   BCF PORTB,4                    ; Set 4-bit mode
   CALL PulseE                    ; Latch it
   MOVLW 0x28                     ; Set 4-bit mode, 2 lines
   CALL Send                      ; and send code
   MOVLW 0x08                     ; Switch off display
   CALL Send                      ; and send code
   MOVLW 0x01                     ; Clear display
   CALL Send                      ; and send code
   MOVLW 0x06                     ; Enable cursor auto inc
   CALL Send                      ; and send code
   MOVLW 0x80                     ; Zero display address
   CALL Send                      ; and send code
   MOVLW 0x0C                     ; Turn on display
      CALL Send                      ; and send code
   RETURN                         ; Done
                              
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
;   Send the fixed message to the display    ;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

 OutMes
   CLRF Point              ; Reset table pointer
   BSF Select,RS                  ; Select data mode
  
    Mess1
   MOVF Point,W             ; and load it
   CALL Line1                     ; Get ASCII code from table
      CALL Send                      ; and do it
   INCF Point                     ; point to next character
   MOVF Point,W                   ; and load the pointer
   SUBLW D'16'                    ; check for last table item
   BTFSS STATUS,Z                 ; and finish if 16 done
   GOTO Mess1                     ; Output character code
   MOVLW 0xC0                     ; Move cursor to line 2
   BCF Select,RS                  ; Select command mode
   CALL Send                      ; and send code
   CLRF Point                     ; Reset table pointer
 
   Mess2
      MOVF Point,W             ; and load it
   CALL Line2                     ; Get fixed character
      BSF Select,RS                  ; Select data mode
   CALL Send                      ; and send code
      INCF Point                     ; next character
   MOVF Point,W                   ; Reload pointer
   SUBLW D'11'                    ; and check for last
   BTFSS STATUS,Z                 ; Skip if last
   GOTO Mess2                     ; or send next
   RETURN                         ; done
                             
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
; Output variable count to display (0-9) endlessly   ;
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

 OutVar
   CLRF Var                ; Clear variable number
   MOVLW 0X30                     ; Load offset to be added
   ADDWF Var                      ; to make ASCII code (30-39)

    Next
      MOVF Var,W                ; Load the code
   BSF Select,RS                  ; Select data mode
   CALL Send                      ; and send code
   MOVLW 0xCB                     ; code to move cursor back
   BCF Select,RS                  ; Select command mode
   CALL Send                      ; and send code
   MOVLW D'250'                   ; Load count to wait 250ms
   CALL Xms                       ; so numbers are visible
   INCF Var                       ; Next number
   MOVF Var,W                     ; Load number
   SUBLW 0x3A                     ; Check for last (10=A)
   BTFSS STATUS,Z                 ; and skip if last
   GOTO Next                      ; or do next number
   GOTO OutVar                    ; Repeat from number Z
      
 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;                             
 ;     MAIN PROGRAM       ;
 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

    Start
      CALL Init                ; Initialise the display
   CALL OutMes                    ; Display fixed characters
   GOTO OutVar                    ; Display an endless count

END                            ; of source code ;;;;;;;;;

 

Comments