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

📄 lcd4.asm

📁 温度测量
💻 ASM
字号:
;************************************************
;* LCD driver routines for AVR's		*
;*						*
;* For 2x16 LCD Panel (4bit Mode)		*
;*						*
;* Copyright 2001-2003 Wayne Peacock		*
;* First Revision 7th July 2001			*
;* Version 1.02					*
;*						*
;************************************************
;* See Schematic for Hardware connections	*
;* 						*
;* Disclaimer:					*
;* The Author takes no responsibility for the 	*
;* use of this code. Use at your own risk!	*
;* 						*
;* This code or part there of is licensed only 	*
;* for private use and NOT for commercial use.	*
;* 						*
;* Please contact the Author 'Wayne Peacock' 	*
;* <wpeacock@senet.com.au> before using the code*
;* or part there of in a commercial project.	*
;************************************************

; Requires the following registers 
;.def	text	= r0		; Text MUST be 0
;.def	data	= r17
;.def	temp	= r16
;	Zreg			;Pointer to Data

;************************************************
;* LCD Panel in 4 bit mode			*
;* 						*
;* Pin Connections				*
;*						*7
;* PD0 --> D4					*
;* PD1 --> D5					*
;* PD2 --> D6					*
;* PD3 --> D7					*
;*						*
;* PB0 --> E  					*
;* PB1 --> R/W (1 = READ, 0 = WRITE)		*
;* PB2 --> RS  (1 = DATA, 0 = CONTROL)		*
;************************************************


;************************************************
;* LCD CONSTANTS				*
;************************************************


.equ	E	= 0
.equ	RS	= 2
.equ	RW	= 1
.equ	BF	= 3			; Busy Flag D7
;.equ	LCDD	= $11			; (LCD Data Direction) DDRD
;.equ	LCDP	= $12	 		; (LCD Data Port) PORTD
;.equ	LCDI	= $10			; (LCD Data Input) PIND

;************************************************
;* Setup LCD Panel Port				*
;* Input: 	None				*
;* Returns:	None				*
;* Requires:	data				*
;************************************************

lcdinit:
	; Clear LCD Pins
	cbi	PORTB,E			; LCD Enable Inactive 

	; Make Control Pins Output
	
	in 	data, DDRB
	ori	data, 0b00000111	; Make all LCD pins outputs
	out	DDRB, data
	
	; Make Data pins Outputs
	
	in 	data, DDRD
	ori	data, 0b00001111
	out	DDRD,data
	
	; 4 Bit Mode
	
	andi	data, 0b11110000	; Clear Data Nibble
	ori	data, 0x02		; One 4bit instruction (2 = 4bit)
	out	PORTD, data		; Load command onto LCD Data Port
	
	cbi	PORTB,RW		; Write Mode
	cbi	PORTB,RS		; RS low (command)
	
	sbi	PORTB,E			; Enable --> high (50us to next instruction) 
	nop				; 375ns delay (Must be greater than 220ns)
	nop
	nop
	nop
	cbi	PORTB,E			; Enable Inactive
	; Repeat Command to make sure panel got it!
	;nop
	;nop
	;nop
	nop
	nop
	nop
	;cbi	PORTB,RW		; Write Mode
	;cbi	PORTB,RS		; RS low (command)
	;sbi	PORTB,E			; Enable --> high (50us to next instruction) 
	nop				; 375ns delay (Must be greater than 220ns)
	nop
	nop
	nop
	nop
	nop
	;cbi	PORTB,E			; Enable Inactive
	ret
	
	
;************************************************
;* Command(char data)				*
;* Input: 	data				*
;* Returns:	None				*
;* Requires:	temp				*
;*						*
;* 0x38	8 bits / 2Lines				*
;* 0x0F	Display On / Cursor Flashing		*
;* 0x0C	Display on - No Cursor			*
;* 0x01	Clear Display				*
;* 0xC0	Bottom Line				*
;* 0x80	Top Line				*
;************************************************	

command:
	rcall	waitlcd			; Make sure it's ready
	
	cbi	PORTB,RS		; RS low (command)
	rcall	lcdwr_0
	ret
	
	
lcdwr_0:
	mov	store, data		; Push data into store
	swap	data
	andi	data, 0x0F
	
	in	temp, PORTD
	andi	temp, 0b11110000	; Clear Data Nibble
	or	temp, data		; One 4bit instruction (2 = 4bit)
	out	PORTD, temp		; Load command onto LCD Data Port
	
	cbi	PORTB,RW		; Write Mode
	sbi	PORTB,E			; Enable --> high (50us to next instruction) 
	nop				; 375ns delay (Must be greater than 220ns)
	nop
	nop
	nop
	nop
	nop
	cbi	PORTB,E			; Enable Inactive
	nop
	nop
	nop

	; second nibble to send
	mov	data, store
	;swap	data
	andi	data, 0x0F
	
	in	temp, PORTD
	andi	temp, 0b11110000	; Clear Data Nibble
	or	temp, data		; One 4bit instruction (2 = 4bit)
	out	PORTD, temp		; Load command onto LCD Data Port

	nop
	nop
	sbi	PORTB,E			; Enable --> high (50us to next instruction) 
	nop				; 375ns delay (Must be greater than 220ns)
	nop
	nop
	nop
	nop
	nop
	cbi	PORTB,E			; Enable Inactive				
	ret

;************************************************
;* OneChar(char data)				*
;* Input: 	data				*
;* Returns:	None				*
;* Requires:	temp				*
;*						*
;************************************************

onechar:
	rcall	waitlcd			; Make sure it's ready
	sbi	PORTB,RS		; RS high (data)
	rcall	lcdwr_0
	ret
	
;************************************************
;* WriteData(*Zreg) 0x00 to End			*
;* Input: 	Data at address of Z pointer	*
;* Returns:	None				*
;* Requires:	temp				*
;*		data				*
;*		text (Must be r0)		*
;************************************************

writedata:			
	;lpm				; Load Program Memory (Get from Flash!)
	tst 	text			; Check for more characters
	breq	wd2			; End of Text
	
	rcall	waitlcd			; Wait for LCD ready
	sbi	PORTB,RS		; RS high (data)
	mov	data, text
	rcall	lcdwr_0
	;adiw	ZL,1			; Inc Z Pointer
	rjmp	writedata		; Repeat until 00		

wd2:	ret				; Finished here
	
				
;************************************************
;* Wait for LCD panel 				*
;* Input: 	None				*
;* Output:	None				*
;* Requires:	temp				*
;************************************************
	
waitlcd:
	; Make data pins input
	in 	temp, DDRD
	andi	temp, 0b11110000	; Make all Data LCD pins inputs
	out	DDRD, temp
	
	sbi	PORTB,RW			; Read Mode
	cbi	PORTB,RS			; RS Low (command)
	sbi	PORTB,E			; Enable --> High 
	nop				; ns delay
	nop
	sbic	PIND,BF			; Check for Busy flag, Skip if ready
	rjmp	wlcd2			; Not ready!
	
	cbi	PORTB,E			; Enable Inactive
	; Second nibble
	nop
	nop
	sbi	PORTB,E			; Enable --> High 
	nop				; ns delay
	nop
	nop
	cbi	PORTB,E			; Enable Inactive	
	
	; Make data pins output
	cbi	PORTD, RW			; Write Mode
	in 	temp, DDRD
	ori	temp, 0b00001111	; Make all LCD pins outputs
	out	DDRD, temp	
	
	ret
	
wlcd2:  cbi	PORTB,E			; Enable Inactive
	; Second nibble
	nop
	nop
	sbi	PORTB,E			; Enable --> High 
	nop				; ns delay
	nop
	nop
	cbi	PORTB,E			; Enable Inactive
	rjmp	waitlcd			; Repeat until ready
	
; End of File!

⌨️ 快捷键说明

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