Hi All,,
I need to display the voltage at RA0 on LCD..
I tried the following code with 7 segments it worked fine .. the voltage at pin RA0 diplayed correctly on segments.
but when i tried to display the voltage on lcd no things displayed on it
ONLY THE LED CONNECTED ON RC0 BLINKING EVERY 1 SECOND
here is the code
here is my lcd driver LCD.INC
regards
I need to display the voltage at RA0 on LCD..
I tried the following code with 7 segments it worked fine .. the voltage at pin RA0 diplayed correctly on segments.
but when i tried to display the voltage on lcd no things displayed on it
ONLY THE LED CONNECTED ON RC0 BLINKING EVERY 1 SECOND
here is the code
Code:
PROCESSOR 16F877A
INCLUDE <P16F877A.INC>
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
;:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:
CNT EQU 0X20
ADhi EQU 31 ; Binary input high byte
ADlo EQU 32 ; Binary input low byte
thos EQU 33 ; Thousands digit in decimal
huns EQU 34 ; Hundreds digit in decimal value
tens EQU 35 ; Tens digit in decimal value
ones EQU 36 ; Ones digit in decimal value
ADL EQU 37
ADH EQU 38
D11 EQU 39
D2 EQU 40
D3 EQU 41
;START PROGRAM
ORG 0
GOTO MAIN
INCLUDE <LCD.INC>
MAIN:
;CONFIGURE I/O PINS
LCDINIT
BSF STATUS,RP0 ;SELECT BANK1 TO
BCF STATUS,RP1; ACCESS SFRs IN BANK1
BSF TRISA,0
BCF TRISC,0
MOVLW B'10000011'
MOVWF ADCON1
BCF STATUS,RP0 ;RETURN BACK TO BANK0
MOVLW B'11000001'
MOVWF ADCON0
BSF PORTC,0
BEGIN:
MOVLW 007 ; load counter
MOVWF CNT
LOOP
DECFSZ CNT ; and delay 20us
GOTO LOOP
BSF ADCON0,GO ; start ADC..
wait
BTFSC ADCON0,GO ; ..and wait for finish
GOTO wait
CALL CONV2D
MOVF thos,W
ADDLW 0X30
MOVWF W_REG
LCDDATA W_REG
MOVLW '.'
MOVWF W_REG
LCDDATA W_REG
MOVF huns,W
ADDLW 0X30
MOVWF W_REG
LCDDATA W_REG
MOVF tens,W
ADDLW 0X30
MOVWF W_REG
LCDDATA W_REG
MOVF ones,W
ADDLW 0X30
MOVWF W_REG
LCDDATA W_REG
MOVLW ' '
MOVWF W_REG
LCDDATA W_REG
MOVLW 'V'
MOVWF W_REG
LCDDATA W_REG
MOVLW 'O'
MOVWF W_REG
LCDDATA W_REG
MOVLW 'L'
MOVWF W_REG
LCDDATA W_REG
MOVLW 'T'
MOVWF W_REG
LCDDATA W_REG
MOVLW 's'
MOVWF W_REG
LCDDATA W_REG
BSF PORTC,0
CALL DELAY_SECOND
BCF PORTC,0
CALL DELAY_SECOND
LCDCMD _LCD_CLEAR ;CLEAR LCD
LCDSETCURSOR 1,2;MOVE CURSOR TO 1ST ROW
GOTO BEGIN
CONV2D:
MOVF ADRESH,W ; get ADC result
MOVWF ADhi ; high bits
BANKSEL ADRESL ; in bank 1
MOVF ADRESL,W ; get ADC result
BANKSEL ADRESH ; default bank 0
MOVWF ADlo ; low byte
; Multiply by 5 for result 0 - 5120 by shifting left 2 TIMES + THE ORIGINAL VALUE.........
BCF STATUS,C ; rotate 0 into LSB and
RLF ADlo ; shift low byte left
BTFSS STATUS,C ; carry out?
GOTO rot1 ; no, leave carry clear
BSF STATUS,C ; rotate 1 into LSB and
rot1
RLF ADhi ; shift high byte left
BCF STATUS,C ; rotate 0 into LSB
RLF ADlo ; rotate low byte left again
BTFSS STATUS,C ; carry out?
GOTO rot2 ; no, leave carry clear
BSF STATUS,C ; rotate 1 into LSB and
rot2
RLF ADhi ; shift high byte left
MOVF ADRESH,W ; get ADC result
MOVWF ADH ; high bits
BANKSEL ADRESL ; in bank 1
MOVF ADRESL,W ; get ADC result
BANKSEL ADRESH ; default bank 0
MOVWF ADL ; low byte
BCF STATUS,C
MOVF ADL,W
ADDWF ADlo,F
BTFSS STATUS,C
GOTO NC
MOVF ADH,W
ADDWF ADhi,W
ADDLW 0X01
MOVWF ADhi
GOTO HI
NC:
MOVF ADH,W
ADDWF ADhi,F
HI:
; Clear BCD registers........................................
clrbcd
CLRF thos ; zero thousands digit
CLRF huns ; zero hundreds digit
CLRF tens ; zero tens digit
CLRF ones ; zero ones digit
; Calclulate thousands low byte .............................
tholo
MOVF ADhi,F ; check high byte
BTFSC STATUS,Z ; high byte zero?
GOTO hunlo ; yes, next digit
BSF STATUS,C ; set carry for subtract
MOVLW 0E8 ; load low byte of 1000
SUBWF ADlo ; and subtract low byte
BTFSC STATUS,C ; borrow from high bits?
GOTO thohi ; no, do high byte
DECF ADhi ; yes, subtract borrow
; Calculate thousands high byte..............................
thohi
BSF STATUS,C ; set carry for subtract
MOVLW 003 ; load high byte of 1000
SUBWF ADhi ; subtract from high byte
BTFSC STATUS,C ; result negative?
GOTO incth ; no, inc digit and repeat
ADDWF ADhi ; yes, restore high byte
; Restore remainder when done ...............................
BCF STATUS,C ; clear carry for add
MOVLW 0E8 ; load low byte of 1000
ADDWF ADlo ; add to low byte
BTFSC STATUS,C ; carry out?
INCF ADhi ; yes, inc high byte
GOTO hunlo ; and do next digit
; Increment thousands digit and repeat.......................
incth
INCF thos ; inc digit
GOTO tholo ; and repeat
; Calclulate hundreds .......................................
hunlo
MOVLW 064 ; load 100
BSF STATUS,C ; set carry for subtract
SUBWF ADlo ; and subtract low byte
BTFSC STATUS,C ; result negative?
GOTO inch ; no, inc hundreds & repeat
MOVF ADhi,F ; yes, test high byte
BTFSC STATUS,Z ; zero?
GOTO remh ; yes, done
DECF ADhi ; no, subtract borrow
inch
INCF huns ; inc hundreds digit
GOTO hunlo ; and repeat
remh
ADDWF ADlo ; restore onto low byte
; Calculate tens digit......................................
subt
MOVLW D'10' ; load 10
BSF STATUS,C ; set carry for subtract
SUBWF ADlo ; and subtract from result
BTFSS STATUS,C ; and check if done
GOTO remt ; yes, restore remainder
INCF tens ; no, count number of loops
GOTO subt ; and repeat
; Restore remainder.........................................
remt
ADDWF ADlo ; yes, add 10 back on
MOVF ADlo,W ; load remainder
MOVWF ones ; and store as ones digit
RETURN ; done
;DELAY 1 SEC ROUTINE 4MHz oscillator
DELAY_SECOND:
MOVLW .5 ;.10==B'00000101' 8MHz OSC
MOVWF D3
LOOP3:
MOVLW .200 ;.200==B'11111111'
MOVWF D2
LOOP2:
MOVLW .200 ;.200==B'11111111'
MOVWF D11
LOOP1:
NOP
NOP
DECFSZ D11,F
GOTO LOOP1
DECFSZ D2,F
GOTO LOOP2
DECFSZ D3,F
GOTO LOOP3
RETURN
END
here is my lcd driver LCD.INC
Code:
#DEFINE LCD_PORT PORTD
#define LCD_RS PORTD,RD2
#define LCD_RW PORTD,RD1
#define LCD_EN PORTD,RD0
#define LCD_D4 PORTD,RD4
#define LCD_D5 PORTD,RD5
#define LCD_D6 PORTD,RD6
#define LCD_D7 PORTD,RD7
#define LCD_RS_Direction TRISD,RD2
#define LCD_RW_Direction TRISD,RD1
#define LCD_EN_Direction TRISD,RD0
#define LCD_D4_Direction TRISD,RD4
#define LCD_D5_Direction TRISD,RD5
#define LCD_D6_Direction TRISD,RD6
#define LCD_D7_Direction TRISD,RD7
#define _LCD_CLEAR 0X01
#define _LCD_RETURN_HOME 0X02
#define _LCD_CURSOR_OFF 0x0c
#define _LCD_CURSOR_ON 0X0E
#define _LCD_BLINK_CURSOR_ON 0X0F
#define _LCD_MOVE_CURSOR_LEFT 0X10
#define _LCD_MOVE_CURSOR_RIGHT 0X14
#define _LCD_TURN_ON 0X0C
#define _LCD_TURN_OFF 0X08
#define _LCD_SHIFT_LEFT 0X18
#define _LCD_SHIFT_RIGHT 0X1C
#define _LCD_BLINK_CURSOR_OFF 0X0E
CBLOCK 0x70
CNTA ;DELAY COUNTER A
CNTB ;DELAY COUNTER B
CNTC ;DELAY COUNTER C
LCDTEMP
W_REG
ENDC
LCDINIT MACRO
BSF STATUS,RP0 ;SELECT BANK1 TO
BCF STATUS,RP1; ACCESS SFRs IN BANK1
BCF LCD_D4_Direction
BCF LCD_D5_Direction
BCF LCD_D6_Direction
BCF LCD_D7_Direction
BCF LCD_RS_Direction
BCF LCD_RW_Direction
BCF LCD_EN_Direction
BCF STATUS,RP0 ;RETURN BACK TO BANK0
BCF LCD_RS
BCF LCD_EN
BCF LCD_RW
CALL DEL05 ;DO 5MS DELAY
CALL DEL05 ;DO 5MS DELAY
CALL DEL05 ;DO 5MS DELAY
lcdsnibble 0x03
CALL DEL05
lcdsnibble 0x03
CALL DEL05
lcdsnibble 0x03
CALL DEL05
lcdsnibble 0X02 ;// cmd: go into 4-bit mode (only MS 4bits connected)
CALL DEL05
LCDCMD 0x28 ; // 4-bit mode, 2lines, 5*7 matrix
LCDCMD 0x6 ; // enable display
LCDCMD 0xC ; // display ON, cursor & blink OFF
LCDCMD 0x1 ; // clear display, address counter to zero
CALL DEL100
ENDM
lcdsnibble MACRO nibble
;....................
BTFSC nibble,0
GOTO $+3
BCF LCD_PORT,4
GOTO $+2
BSF LCD_PORT,4
;....................
;....................
BTFSC nibble,1
GOTO $+3
BCF LCD_PORT,5
GOTO $+2
BSF LCD_PORT,5
;....................
;....................
BTFSC nibble,2
GOTO $+3
BCF LCD_PORT,6
GOTO $+2
BSF LCD_PORT,6
;;....................
;;..................
BTFSC nibble,3
GOTO $+3
BCF LCD_PORT,7
GOTO $+2
BSF LCD_PORT,7
NOP
BSF LCD_EN
NOP
NOP
BCF LCD_EN
ENDM
LCDDATA MACRO value
BCF LCD_RS
BANKSEL TRISD
BSF LCD_D7_Direction
BANKSEL PORTD
BTFSC LCD_D7
GOTO $-1
BANKSEL TRISD
BCF LCD_D7_Direction
BANKSEL PORTD
BSF LCD_RS ;data
NOP
BCF LCD_RW; ;write
NOP
BCF LCD_EN
SWAPF value,W
MOVWF LCDTEMP
MOVLW 0X0F
ANDWF LCDTEMP,F
lcdsnibble LCDTEMP
MOVF value,W
ANDLW 0X0F
MOVWF LCDTEMP
lcdsnibble LCDTEMP
ENDM
LCDCMD MACRO value
BCF LCD_RS
BSF STATUS,RP0 ;SELECT BANK1 TO
BCF STATUS,RP1; ACCESS SFRs IN BANK1
BSF LCD_D7_Direction
BSF STATUS,RP0 ;RETURN TO BANK0
BTFSC LCD_D7
GOTO $-1
BANKSEL TRISB
BCF LCD_D7_Direction
BANKSEL PORTB
BCF LCD_RS ;COMMAND
NOP
BCF LCD_RW; ;write
NOP
BCF LCD_EN
SWAPF value,W
MOVWF LCDTEMP
MOVLW 0X0F
ANDWF LCDTEMP,F
lcdsnibble LCDTEMP
MOVF value,W
ANDLW 0X0F
MOVWF LCDTEMP
lcdsnibble LCDTEMP
ENDM
LCDSETCURSOR MACRO x,y
;.................... ; if(y!=1)
DECFSZ y,W
GOTO YYT
GOTO YYT1
;.................... ; address=0x40;
YYT:
MOVLW 40
MOVWF LCDTEMP
GOTO YYT2
;.................... ; else
;.................... ; address=0;
YYT1:
CLRF LCDTEMP
;....................
;.................... ;address+=x-1;
YYT2:
MOVLW 01
SUBWF x,W
ADDWF LCDTEMP,F
;....................
;.................... ; address=0x80|address;
BSF LCDTEMP,7
LCDCMD LCDTEMP
ENDM
;DELAY ROUTINES
D16US:
CLRF CNTC
BSF CNTC, 5 ;DELAY 160US
BSF CNTC, 4
DECFSZ CNTC, F
GOTO $-1
RETURN
;SELECT DELAY TIME
DEL250:
MOVLW 0xFA ;DELAY 250MS
GOTO D0
DEL255:
MOVLW 0xFF ;DELAY 255MS
GOTO D0
DEL200:
MOVLW D'200' ;DELAY 200MS
GOTO D0
DEL100:
MOVLW D'100' ;DELAY 100MS
GOTO D0
DEL50:
MOVLW D'50' ;DELAY 50MS
GOTO D0
DEL20:
MOVLW D'20' ;DELAY 20MS
GOTO D0
DEL05:
MOVLW 0x05 ;DELAY 5.000MS (4 MHz clock)
GOTO D0
DEL01:
MOVLW 0x01 ;DELAY 1.000MS (4 MHz clock)
;DO SELECTED DELAY
D0:
MOVWF CNTC
D1:
MOVLW 0xC7 ;DELAY 1MS
MOVWF CNTA
MOVLW 0x01
MOVWF CNTB
DEL_0:
DECFSZ CNTA,F
GOTO $+2
DECFSZ CNTB,F
GOTO DEL_0
DECFSZ CNTC,F
GOTO D1
RETLW 0x00
regards
Last edited by a moderator: