You are on page 1of 19

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

; This file is a basic code template for assembly code generation *


; on the PICmicro PIC16F877A. This file contains the basic code *
; building blocks to build upon. *
; *
; If interrupts are not used all code presented between the ORG *
; 0x004 directive and the label main can be removed. In addition *
; the variable assignments for 'w_temp' and 'status_temp' can *
; be removed. *

; *
; Refer to the MPASM User's Guide for additional information on *
; features of the assembler (Document DS33014). *
; *
; Refer to the respective PICmicro data sheet for additional *
; information on the instruction set. *
; *
;**********************************************************************
; Author:Free electronics Porojects
*
; Filename: LCD877A.asm *
; Date: 25/02/2003 *
; File Version: 1.2 *
; *
; *
; Company :This is the program for the fault anounciator,this is the hardware
control side of ;it.this program checks the temperature & control other
fucntions.so go thru code to ;understand.
*
;**********************************************************************
; *
; Notes: THIS IS TEST PROGRAM FOR LCD
*
; PD0 TO PD7 --> LCD DATA
; RS --> PE
; EN --> PE
;**********************************************************************

list p=16f877A ; list directive to define processor


#include <p16f877A.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_OFF & _XT_OSC & _WRT_OFF &
_LVP_OFF & _CPD_OFF

; '__CONFIG' directive is used to embed configuration data within .asm file.


; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

;******************************************************************
;***** VARIABLE DEFINITIONS
w_temp EQU 0x71 ; variable used for context saving
status_temp EQU 0x72 ; variable used for context saving
pclath_temp EQU 0x73 ; variable used for context saving

;**** TEMP RAM


cblock 0x25
COUNTH
COUNTL
COUNTH1
COUNTL1
count
ch
DH
Dl
dsh1
dsh
dsl
dsp2
dsp1
dsp3
dsp4
dsp22
dsp11
dsp33
dsp44
R0;
R1
R2
H_byte
L_byte
temp
ch1
CAL2
DL
count1
MK1
MK2
MK3
MK4
MK5
MK6
COMP

tmp11
cnt11
DH1
DL1
KBD
DDR2
L_BYTE
H_BYTE
MK

endc

TENMSH equ D'13'


TENMSL equ D'250'

;**********************************************************************
ORG 0x000 ; processor reset vector
clrf PCLATH
nop ; nop required for icd
goto start ; go to beginning of program
ORG 0x004 ; interrupt vector location

movwf w_temp ; save off current W register contents


movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,w ; move pclath register into w register
movwf pclath_temp ; save off contents of PCLATH register

; isr code can go here or be located as a call subroutine elsewhere

movf pclath_temp,w ; retrieve copy of PCLATH register


movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt

; Start at the reset vector

start
GOTO ST
banksel TRISC
;clrf TRISC
movlw 0x00
movwf TRISC
banksel TRISB
movlw 0x00
movwf TRISB
banksel TRISD
movlw 0x00
movwf TRISD
banksel ADCON1
MOVLW 0X02
MOVWF ADCON1
BANKSEL TRISE
movlw 0x00
movwf TRISE
banksel TRISA ;assembler directive to select bank
movlw 0xff
movwf TRISA ;set all PORTA pins as inpu

MOVLW 0X00
MOVWF PORTC
MOVWF PORTB

ST
BANKSEL TRISA
MOVLW 0xFF
MOVWF PORTA
MOVLW 0x00
MOVWF PORTB
MOVLW 0x00
MOVWF PORTC
MOVLW 0x00
MOVWF PORTD
MOVLW 0x02
MOVWF ADCON1

MOVLW 0X00
MOVWF PORTE
BANKSEL ch
MOVLW 0x00
MOVWF PORTC
MOVWF MK
BCF PORTE,0
BCF PORTE,1
BCF PORTE,2
MOVLW 0x01
MOVWF PORTC

main1

banksel ch1

movlw b'01001001'
movwf ch1
CALL ADC

movf DH,w

movwf H_byte
;MOVWF DH1 ; ADC HIGHER VALUE
movf DL,w

movwf L_byte
; MOVWF DL1 ; ADC LOWER VALUE
;MOVWF PORTB
call B2_BCD

call conv11 ;
call initlcd

call tenms1

;;;;;;;;;;;;;;;;;;;;

movlw b'01000001'
movwf ch1
CALL ADC

movf DH,w
movwf H_byte
MOVWF DH1 ; ADC HIGHER VALUE
movf DL,w
movwf L_byte
MOVWF DL1 ; ADC LOWER VALUE

call B2_BCD
call conv1 ; DISPLAY ADJ FOR LCD

call lcd0

CALL COMP1

call tenms1
call tenms1

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

main
banksel ch1

movlw b'01001001'
movwf ch1
CALL ADC

movf DH,w

movwf H_byte
;MOVWF DH1 ; ADC HIGHER VALUE
movf DL,w

movwf L_byte
; MOVWF DL1 ; ADC LOWER VALUE
;MOVWF PORTB
call B2_BCD

call conv11 ;
call lcd0

call tenms1

;;;;;;;;;;;;;;;;;;;;

movlw b'01000001'
movwf ch1
CALL ADC

movf DH,w
movwf H_byte
MOVWF DH1 ; ADC HIGHER VALUE
movf DL,w
movwf L_byte
MOVWF DL1 ; ADC LOWER VALUE

call B2_BCD

call conv1 ; DISPLAY ADJ FOR LCD

call lcd0

CALL COMP1

call tenms1
goto main

here goto here

initlcd

call tenms
call tenms
call tenms

loop
movlw 0x30
call lcdout
call tenms
call tenms

movlw 0x30
call lcdout
call tenms

movlw 0x30
call lcdout
call tenms

movlw 0x3C
call lcdout
call tenms

movlw 0x0E
call lcdout
call tenms

movlw 0x06
call lcdout
call tenms

movlw 0x01
call lcdout
call tenms

lcd0
movlw 0x80 ; ADDRESS OF RAM LOCATION OF LCD
call lcdout ; ( STARTING ADDRESS = 80 )
call tenms

movlw 'W' ;
call asc
call tenms

movlw 'I' ;
call asc
call tenms

movlw 'R' ; Z
call asc
call tenms
movlw 'E' ;
call asc
call tenms

movlw 'L' ;
call asc
call tenms

movlw 'E' ; Z
call asc
call tenms

movlw 'S' ;
call asc
call tenms

movlw 'S' ;
call asc
call tenms

movlw 0x20 ; Z
call asc
call tenms

movlw 'R' ;
call asc
call tenms

movlw 'O' ; Z
call asc
call tenms

movlw 'B' ;
call asc
call tenms

movlw 'O' ;
call asc
call tenms

movlw 'T' ;
call asc
call tenms

movlw ' ' ;


call asc
call tenms

;movf dsp11,W
; movlw 0x54 ; T
; call asc
;call tenms

;;;;;;;;;;;;;;;;;;;;;;;;;;

movlw 0xc1 ; COMMAND MODE


call lcdout
call tenms

movlw 0x54 ; R
call asc
call tenms

movlw 0x45 ; A
call asc
call tenms

; movf dsp4,W
movlw 0x4d ;
call asc
call tenms

movlw 0x50 ;
; movf dsp3,W
call asc
call tenms

movlw 0x20 ;
;movf dsp2,W
call asc
call tenms

;movlw 0x2a ;
movf dsp2,W
call asc
call tenms

movf dsp1,W
; movlw 0x20 ; BLANK
call asc
call tenms

movlw 0x20 ;
call asc
call tenms

movlw 0xde ; A
call asc
call tenms

movlw 0x43 ; B
call asc
call tenms

movlw 0x20 ;
call asc
call tenms

movlw 0x20;
call asc
call tenms

movlw 0x20 ;
call asc
call tenms

return

lcdout
movwf PORTD
bcf PORTE,0 ; 2 - PORTC
bsf PORTE,1 ; 3 - PORTC
bcf PORTE,1 ; 3 - PORTC
call tenms
bsf PORTE,0 ; 2 - PORTC
return

asc
movwf PORTD
bsf PORTE,0 ; 2 - PORTC
bsf PORTE,1 ; 3 - PORTC
bcf PORTE,1 ; 3 - PORTC
call tenms
bcf PORTE,0 ; 2 - PORTC
return

tenms
;RETURN
nop
movlw .13
movwf COUNTH
movlw .250
movwf COUNTL

ten
;RETURN
decfsz COUNTL,F
goto ten
decfsz COUNTH,F
goto ten
return

tenms1
;RETURN
nop
movlw .20
movwf COUNTH1
movlw .245
movwf COUNTL1

ten1
; RETURN
decfsz COUNTL,F
goto ten1
decfsz COUNTH,F
goto ten1
return
;----------------------------------------------------------------------

B2_BCD
bcf STATUS,0 ; clear the carry bit
movlw .16
movwf count
clrf R0
clrf R1
clrf R2
loop16 rlf L_byte, F
rlf H_byte, F
rlf R2, F
rlf R1, F
rlf R0, F
;
decfsz count, F
goto adjDEC
RETLW 0
;
adjDEC
movlw R2
movwf FSR
call adjBCD
;
movlw R1
movwf FSR
call adjBCD
;
movlw R0
movwf FSR
call adjBCD
;
goto loop16
;
adjBCD movlw 3
addwf 0,W
movwf temp
btfsc temp,3 ; test if result > 7
movwf 0
movlw 30
addwf 0,W
movwf temp
btfsc temp,7 ; test if result > 7
movwf 0 ; save as MSD
RETLW 0
return
;

;********************************************************************
; Test Program
;*********************************************************************
;main1
; banksel H_byte
; movlw 0x00
; movwf H_byte
; movlw 0x08
; movwf L_byte ; The 16 bit binary number = FFFF
; call B2_BCD ; After conversion the Decimal Number
; return ; in R0,R1,R2 = 06,55,35
ADC
banksel ADCON1 ;assembler directive to select bank
movlw b'10000010'
movwf ADCON1 ;RA0,1,2,3,5 analog, VREF = VDD

banksel ADCON0 ;assembler directive to select bank


movf ch1,w
movwf ADCON0 ; channel 0, FOSC/8, enable A/D

;20us delay loop with 4MHz oscillator frequency

banksel count1 ;select bank


movlw 0x06
movwf count1 ;initialize count
loop4
decfsz count1,F ;dec count, store in count
goto loop4 ;not finished

banksel ADCON0 ;select bank


bsf ADCON0,GO ;initiate conversion

banksel ADCON0 ;select bank


test
btfsc ADCON0,GO ;conversion done?
goto test ;not finished

banksel ADRESH ;select bank


movf ADRESH,W ;move result into working register
movwf DH

banksel ADRESL ;select bank


movf ADRESL,W
movwf DL
banksel ADRESH ;select bank
RETURN

conv1
banksel R1

;call main1
; movlw 0x56
; movwf R1
; movlw 0x24
;movwf R2
movf R1,w
movwf dsh
movf R2,w
movwf dsl
andlw 0xF0

movwf dsh1

rrf dsh1,w
movwf dsh1
rrf dsh1,w
movwf dsh1
rrf dsh1,w
movwf dsh1
rrf dsh1,w
addlw 0x30
movwf dsp2 ; seperating nibble ( right side only)
movf dsl,w
andlw 0x0F
addlw 0x30
movwf dsp1 ; to digit end
movf R1,w
andlw 0x0F
addlw 0x30
movwf dsp3

movf R1,w
andlw 0xF0
movwf dsp4

rrf dsp4,w
movwf dsp4
rrf dsp4,w
movwf dsp4
rrf dsp4,w
movwf dsp4
rrf dsp4,w
addlw 0x30
movwf dsp4

return

;====================================================

COMP1
;MOVF MK1,W
;BTFSC MK1,0
; GOTO P14

movf R2,W
MOVWF COMP ; COMP == ADC TEMP
MOVLW 0x30
XORWF COMP,0
BTFSC STATUS,Z
GOTO P1
MOVLW 0x30
SUBWF COMP,0
BTFSS STATUS,C
GOTO P1 ; TEMP < 30 DEG
GOTO P2 ; TEMP > 30 DEG
call tenms
goto main
P2
MOVLW 0x40;28
XORWF COMP,0
BTFSC STATUS,Z
GOTO P3
MOVLW 0x40;28
SUBWF COMP,0
BTFSS STATUS,C
GOTO P4 ; TEMP < 40 DEG
GOTO P5 ; TEMP > 40 DEG
GOTO main
P5
MOVLW 0x50
XORWF COMP,0
BTFSC STATUS,Z
GOTO P6
MOVLW 0x50;3C
SUBWF COMP,0
BTFSS STATUS,C
GOTO P7 ; TEMP < 50 DEG
GOTO P8 ; TEMP > 50 DEG
GOTO main
P8
MOVLW 0x55
XORWF COMP,0
BTFSC STATUS,Z
GOTO P9
MOVLW 0x55
SUBWF COMP,0
BTFSS STATUS,C
GOTO P10 ; TEMP < 55 DEG
GOTO P11 ; TEMP > 55 DEG
GOTO main
P11
;
MOVLW 0x60
XORWF COMP,0
BTFSC STATUS,Z
GOTO P12
MOVLW 0x60
SUBWF COMP,0
BTFSS STATUS,C
GOTO P13 ; TEMP < 60 DEG
MOVLW 0x10;
MOVWF PORTC
CALL tenms
goto main
;==========================================
; = EQUAL VALUES

P1
MOVLW 0x01
MOVWF PORTC
CALL tenms
CALL LDLY
goto main
P3
MOVLW 0x02 ; temp == 40
MOVWF PORTC
CALL tenms
CALL LDLY
goto main
P6
MOVLW 0x04 ; temp == 50 deg
MOVWF PORTC
CALL tenms
CALL LDLY
goto main
P9
MOVLW 0x08 ; temp == 55 deg
MOVWF PORTC
CALL LDLY
CALL tenms
goto main

P12
MOVLW 0x10 ; temp == 60 deg
MOVWF PORTC
;MOVWF PORTB
CALL tenms
CALL LDLY
goto main

P15
MOVLW 0x05
MOVWF MK
MOVLW 0x20
MOVWF PORTC
CALL LDLY
CALL tenms
goto main
;=...>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
P4

MOVLW 0x01
MOVWF PORTC
CALL LDLY
CALL tenms1
GOTO main
P7
MOVLW 0x02
XORWF MK,0
BTFSS STATUS,Z
GOTO P3
MOVLW 0x04
MOVWF PORTC
CALL LDLY
CALL tenms1
GOTO main

P10

MOVLW 0x03
XORWF MK,0
BTFSS STATUS,Z
GOTO P6
MOVLW 0x08
MOVWF PORTC
CALL LDLY
CALL tenms1
GOTO main

P13
MOVLW 0x04
XORWF MK,0
BTFSS STATUS,Z
GOTO P9
MOVLW 0x10
MOVWF PORTC
CALL LDLY
CALL tenms1
GOTO main

P16
MOVLW 0x05
XORWF MK,0
BTFSS STATUS,Z
GOTO P12
MOVLW 0x20
MOVWF PORTC
CALL LDLY
CALL tenms1
GOTO main
;-----==========================
; TEMP > VALUES

P18
MOVLW 0x20
MOVWF PORTC
CALL LDLY
CALL tenms1 ; TEMP > 70
return
P19
MOVLW 0x20
MOVWF PORTC
CALL LDLY
CALL tenms1 ; TEMP > 70
return
;-========================================
LDLY

call tenms1
call tenms1

RETURN

lcd1
call tenms
call tenms
call tenms

;loop
movlw 0x30
call lcdout
call tenms
call tenms

movlw 0x30
call lcdout
call tenms

movlw 0x30
call lcdout
call tenms
movlw 0x3C
call lcdout
call tenms

movlw 0x0E
call lcdout
call tenms

movlw 0x06
call lcdout
call tenms

movlw 0x01
call lcdout
call tenms

movlw 0x80 ; ADDRESS OF RAM LOCATION OF LCD


call lcdout ; ( STARTING ADDRESS = 80 )
call tenms

movlw 0x20 ; F start


call asc
call tenms

movlw 0x20 ; U
call asc
call tenms

movlw 0x20 ; Z
call asc
call tenms

movlw 0x20 ; Z
call asc
call tenms

movlw 0x20; Y
call asc
call tenms

movlw 0x20 ;
call asc
call tenms

movlw 0x20 ; C
call asc
call tenms

movlw 0x20 ; O
call asc
call tenms

movlw 0x20 ; N
call asc
call tenms
movlw 0x20 ; T
call asc
call tenms

movlw 0x20 ; r
call asc
call tenms

movlw 0x20 ; O
call asc
call tenms

movlw 0x20 ; L
call asc
call tenms

movlw 0x20 ; E
call asc
call tenms

movlw 0x20 ; r
call asc
call tenms

;;;;;;;;;;;;;;;;;;;;;;;;;;

movlw 0xc1 ; COMMAND MODE


call lcdout
call tenms

movlw 0x20 ; R
call asc
call tenms

movlw 0x20 ; A
call asc
call tenms

movlw 0x50 ;
call asc
call tenms

movlw 0x48 ;

call asc
call tenms

movlw 0x20 ;
call asc
call tenms

movlw 0x2d ;
; movf dsp4,W
call asc
call tenms

; movf dsp3,W
movlw 0x20 ; BLANK
call asc
call tenms

movf dsp2,W
; movlw 0x20 ;
call asc
call tenms

movf dsp1,W
;movlw 0x20 ; A
call asc
call tenms

movlw 0x20 ; B
call asc
call tenms

movlw 0x20 ;
call asc
call tenms

movlw 0x20;
call asc
call tenms

movlw 0x20 ;
call asc
call tenms
return

conv11
banksel R1

;call main1
; movlw 0x56
; movwf R1
; movlw 0x24
;movwf R2
movf R1,w
movwf dsh
movf R2,w
movwf dsl
andlw 0xF0

movwf dsh1

rrf dsh1,w
movwf dsh1
rrf dsh1,w
movwf dsh1
rrf dsh1,w
movwf dsh1
rrf dsh1,w
addlw 0x30
movwf dsp22 ; seperating nibble ( right side only)
movf dsl,w
andlw 0x0F
addlw 0x30
movwf dsp11 ; to digit end
movf R1,w
andlw 0x0F
addlw 0x30
movwf dsp33

movf R1,w
andlw 0xF0
movwf dsp44

rrf dsp44,w
movwf dsp4
rrf dsp44,w
movwf dsp44
rrf dsp44,w
movwf dsp44
rrf dsp44,w
addlw 0x30
movwf dsp44

return

; remaining code goes here


end