⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.asm

📁 PROTEUS仿真PIC16F877的例子
💻 ASM
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	LCD.ASM		MPB	Ver 1.0		28-8-05
;
;	Outputs fixed and variable characters 
;	to 16x2 LCD in 4-bit mode
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	PROCESSOR 16F877A
;	Clock = XT 4MHz, standard fuse settings
	__CONFIG 0x3731

; LABEL EQUATES	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	INCLUDE "P16F877A.INC"	; Standard register 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 code

RS	EQU	1		; Register select output bit
E	EQU	2		; Display enable

; PROGRAM BEGINS ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	ORG	0		; Place machine code  
	NOP			; for ICD mode

	BANKSEL	TRISD		; Select bank 1 
	CLRW			; All outputs
	MOVWF	TRISD		; Initialise display port 
	BANKSEL PORTD		; Select bank 0
	CLRF	PORTD		; 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	PORTD,E		; Set E high
	CALL	Onems		; Delay 1ms
	BCF	PORTD,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	PORTD		; Output high nybble
	BTFSC	Select,RS	; Test RS bit
	BSF	PORTD,RS	; and set for data
	CALL	PulseE		; and clock display register
	CALL	Onems		; wait 1ms for display 

	SWAPF	OutCod		; Swap low and high nybbles 
	MOVF	OutCod,W	; Retrieve output code
	ANDLW	0F0		; Clear low nybble
	MOVWF	PORTD		; Output low nybble
	BTFSC	Select,RS	; Test RS bit
	BSF	PORTD,RS	; and set for data
	CALL	PulseE		; and clock display register
	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 for 100ms delay
	CALL	Xms		; and wait for display start
	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	PORTD,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		; Code to 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -