历史上的今天
返回首页

历史上的今天

今天是:2024年10月21日(星期一)

正在发生

2019年10月21日 | 以汇编语言写的PIC18Fxxxx的LCD驱动程序

2019-10-21 来源:eefocus

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

;* 18F_LCD.asm *

;* Microchip Taiwan                             *

;* Date : Sept. 24 2002 *

;* Version : v1.00 *

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

;* Contains subroutines to control an external  *

;* lcd panel in 4-bit mode.  These routines     *

;* were designed specifically for the panel on  *

;* the PICdemo 2 Plus demo board, but should    *

;* work with other LCDs with a HD44780 type     *

;* controller.                                  *

;* Routines include:                            *

;*   - InitLCD to initialize the LCD panel      *

;*   - putcLCD to write a character to LCD      *

;*   - SendCmd to write a command to LCD        *

;*   - clrLCD to clear the LCD display          *

;*   - L1homeLCD to return cursor to line 1 home*

;*   - L2homeLCD to return cursor to line 2 home*

;*   - PutHexLCD to write a HEX Code to LCD     *

;*   - Hex2ASCII to convert 4 bits to ASCII Code*

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

;

list p=18F452

#include

;

global InitLCD

global putcLCD

global clrLCD

global L1homeLCD

global L2homeLCD

global  Send_Cmd

global PutHexLCD

global Hex2ASCII

global Delay_xMS

global Delay_1mS

;

; Defines for I/O ports that provide LCD data & control

; PORTD[0:3]-->DB[4:7]: Higher order 4 lines data bus with bidirectional

;   : DB7 can be used as a BUSY flag

; PORTA,1 --> [E] : LCD operation start signal control 

; PORTA,2 --> [RW]: LCD Read/Write control

; PORTA,3 --> [RS]: LCD Register Select control

;   : "0" for Instrunction register (Write), Busy Flag (Read)

;   : "1" for data register (Read/Write)

;

;

LCD_CTRL equ TRISD

LCD_DATA equ PORTD

#define LCD_E_DIR TRISA,1

#define LCD_RW_DIR TRISA,2

#define LCD_RS_DIR TRISA,3

#define LCD_E PORTA,1

#define LCD_RW PORTA,2

#define LCD_RS PORTA,3  



; LCD Module commands

CLR_DISP equ b'00000001' ; 1.64mS , Clear display and return cursor to home

Cursor_Home equ b'00000010' ; 1.64mS , Return cursor to home position


ENTRY_DEC equ b'00000100' ; 40uS , Decrement cursor position & No display shift

ENTRY_DEC_S equ b'00000101' ; 40uS , Decrement cursor position & display shift

ENTRY_INC equ b'00000110' ; 40uS , Increment cursor position & No display shift

ENTRY_INC_S equ b'00000111' ; 40uS , Increment cursor position & display shift


DISP_OFF equ b'00001000' ; 40uS , Display off

DISP_ON equ b'00001100' ; 40uS , Display on control

DISP_ON_C equ b'00001110' ; 40uS , Display on, Cursor on

DISP_ON_B equ b'00001111' ; 40uS , Display on, Cursor on, Blink cursor


FUNC_SET equ b'00101000' ; 40uS , 4-bit interface , 2-lines & 5x7 dots

CG_RAM_ADDR equ b'01000000' ; 40uS , Least Significant 6-bit are for CGRAM address

DD_RAM_ADDR equ b'10000000' ; 40uS , Least Significant 7-bit are for DDRAM address

;


; Directs linker to provide 4 variables in GPR memory

UDATA

LCD_Byte RES 1

LCD_Temp RES 1

Count_uS RES 1

Count_mS RES 1

W_BUFR RES 1

Hex_Bfr RES 1

;

LCD_CODE CODE

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

;* The LCD Module Subroutines                                      *

;* Command sequence for 2 lines of 5x16 characters                 *

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

InitLCD

bcf LCD_E ; Clear LCD control line to Zero

bcf LCD_RW 

bcf LCD_RS 

;

bcf LCD_E_DIR ;configure control lines for Output pin

bcf LCD_RW_DIR

bcf LCD_RS_DIR

;

movlw b'00001110' ; configure AN0 as A/D inut pin

movwf ADCON1

movf LCD_CTRL,W ; get I/O directional settng

andlw 0xF0

movwf LCD_CTRL ; set LCD bus  DB[4:7] for output

;

movlw .50 ; Power-On delay 50mS

rcall Delay_xMS

;

movlw   b'00000011' ; #1 , Init for 4-bit interface

rcall Send_Low_4bit

;

movlw .10 ;  Delay 10 mS

rcall Delay_xMS

;

movlw b'00000011' ; #2 , Fully Initial LCD module 

rcall Send_Low_4bit ; Sent '0011' data  

rcall Delay_1mS

;

movlw b'00000011' ; #3 , Fully Initial LCD module 

rcall Send_Low_4bit ; Sent '0011' data  

rcall Delay_1mS

;

movlw b'00000010' ; #4 , Fully Initial LCD module 

rcall Send_Low_4bit ; Sent '0010' data  

rcall Delay_1mS

;

movlw FUNC_SET ; #5,#6 , Set 4-bit mode , 2 lines & 5 x 7 dots

rcall Send_Cmd

rcall Delay_1mS

;

movlw DISP_ON ; #7,#8 , Turn display on (0x0C)

rcall Send_Cmd

rcall Delay_1mS

;

movlw CLR_DISP ; #9,#10 , Clear LCD Screen

rcall Send_Cmd

movlw .5 ; Delay 5mS for Clear LCD Command execution

rcall Delay_xMS

;

movlw ENTRY_INC ; #11,#12 , Configure cursor movement

rcall Send_Cmd

rcall Delay_1mS

;

movlw DD_RAM_ADDR ; Set writes for display memory

rcall Send_Cmd

rcall Delay_1mS

;

return

;

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

;*SendChar - Sends character to LCD                                *

;*This routine splits the character into the upper and lower       * 

;*nibbles and sends them to the LCD, upper nibble first.           *

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

putcLCD

movwf LCD_Byte ; Save WREG in Byte variable

rcall Send_High_LCD ; Send upper nibble first

movf LCD_Byte,W

rcall Send_Low_LCD ; Send lower nibble data

rcall Delay_100uS

return

;

Send_High_LCD 

swapf WREG,W ; swap high/low nibble

Send_Low_LCD 

bcf LCD_RW ; set LCD Write Mode

andlw 0x0F ; Clear high nibble

movwf LCD_Temp

movf LCD_DATA,W ; Read back PORT

andlw 0xF0 ; keep data for PORTD[4:7]

iorwf LCD_Temp,W

movwf LCD_DATA ; Write data to LCD bus for low nibble bus DB[4:7]

bsf LCD_RS ; Set for data input

nop

bsf LCD_E ; Clock nibble into LCD

nop

bcf LCD_E

return

;

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

;*      To put the HEX value to LCD Display ,,

;*      High nibble first than Low nibble 

;*      Input : W Reg.

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

PutHexLCD

movwf W_BUFR ; Save W Register !!

swapf W_BUFR,W ; High nibble first !!

rcall Hex2ASCII

rcall putcLCD

;

movf W_BUFR,W

rcall Hex2ASCII

rcall putcLCD

return

;

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

;*       Convert a low nibble to ASCII code

;*       Input : W Reg.

;*       Output: W Reg.

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

Hex2ASCII

andlw 0x0f ; Mask Bit 4 to 7

movwf Hex_Bfr

sublw .09

btfsc STATUS,C ; If W less than A (C=1) --> only add 30h

bra _Add_W_30  

_Add_W_37 movlw 0x37

bra _Hex_cont 

_Add_W_30 movlw 0x30

_Hex_cont addwf Hex_Bfr,W ; The correct ASCII code for this char !!

return

;

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

;* SendCmd - Sends command to LCD                                  *

;* This routine splits the command into the upper and lower        * 

;* nibbles and sends them to the LCD, upper nibble first.          *

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

;     _    ______________________________

; RS  _>--<______________________________

;     _____

; RW       _____________________________

;                  __________________

; E   ____________/                  ___

;     _____________                ______

; DB  _____________>--------------<______

;

Send_Cmd

movwf LCD_Byte ; Save WREG in Byte variable

rcall Send_High_4bit ; Send upper nibble first

movf LCD_Byte,W

rcall Send_Low_4bit ; Send lower nibble data

rcall Delay_100uS

return

;

Send_High_4bit

swapf WREG,W ; swap high/low nibble

Send_Low_4bit

bcf LCD_RW ; set LCD Write Mode

andlw 0x0F ; Clear high nibble

movwf LCD_Temp

movf LCD_DATA,W ; Read back PORT

andlw 0xF0 ; keep data for PORTD[4:7]

iorwf LCD_Temp,W

movwf LCD_DATA ; Write data to LCD bus for low nibble bus DB[4:7]

bcf LCD_RS ; Clear for command inut

nop

bsf LCD_E ; Clock nibble into LCD

nop

bcf LCD_E

return

;

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

;* clrLCD - Clear the contents of the LCD                          *

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

clrLCD

movlw CLR_DISP ; Clear LCD screen

rcall Send_Cmd

movlw .5 ; Delay 5mS for Clear LCD Command execution

bra Delay_xMS

;

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

;* L1homeLCD - Moves the cursor to home position on Line 1         *

推荐阅读

史海拾趣

Cal Crystal Lab Inc / Comclok Inc公司的发展小趣事

在激烈的市场竞争中,品牌建设和形象提升对于企业的发展至关重要。Comclok Inc深知品牌建设的重要性,从产品设计、生产到销售服务,都注重塑造公司的品牌形象。公司注重产品的品质和用户体验,不断提升产品的性能和稳定性。同时,Comclok Inc还积极参加各类行业展会和交流活动,展示公司的技术实力和产品优势,提升了公司在行业内的知名度和影响力。

Actel公司的发展小趣事

在激烈的市场竞争中,品牌建设和形象提升对于企业的发展至关重要。Comclok Inc深知品牌建设的重要性,从产品设计、生产到销售服务,都注重塑造公司的品牌形象。公司注重产品的品质和用户体验,不断提升产品的性能和稳定性。同时,Comclok Inc还积极参加各类行业展会和交流活动,展示公司的技术实力和产品优势,提升了公司在行业内的知名度和影响力。

创都(CAX)公司的发展小趣事

企业文化是企业的灵魂和精神支柱。创都公司自创立之初就注重企业文化的建设与发展。他们倡导“创新、协作、务实、进取”的企业精神,鼓励员工勇于创新、敢于担当。同时,公司还注重员工培训和职业发展规划的制定与实施,为员工提供了广阔的发展空间和良好的职业前景。这些措施不仅激发了员工的积极性和创造力还增强了企业的凝聚力和向心力使得创都公司在激烈的市场竞争中始终保持着旺盛的发展势头。

Einfochips公司的发展小趣事

随着硬件设计和验证领域的发展,SystemVerilog逐渐成为行业内的主流语言。Einfochips公司敏锐地捕捉到了这一趋势,并决定为客户提供从其他传统语言和环境转变到SystemVerilog的验证迁移服务。这一服务的推出,不仅帮助客户提高了设计和验证效率,还进一步扩大了Einfochips在硬件设计和验证领域的市场份额。

Alorium Technology公司的发展小趣事

随着硬件设计和验证领域的发展,SystemVerilog逐渐成为行业内的主流语言。Einfochips公司敏锐地捕捉到了这一趋势,并决定为客户提供从其他传统语言和环境转变到SystemVerilog的验证迁移服务。这一服务的推出,不仅帮助客户提高了设计和验证效率,还进一步扩大了Einfochips在硬件设计和验证领域的市场份额。

FILTRONETICS Inc公司的发展小趣事

随着业务的不断增长,FILTRONETICS意识到单一产品线的局限性,开始着手拓展多元化产品线。公司加大了对新产品研发的投入,陆续推出了单片滤波器、声表滤波器、腔体滤波器、带线滤波器等多种类型的产品,广泛应用于通讯、仪器仪表、商业、工业等多个领域。这些新产品的推出不仅丰富了公司的产品线,也进一步提升了公司的市场竞争力。

问答坊 | AI 解惑

Buck变换器的数字模糊PID控制

Buck变换器的数字模糊PID控制 摘要:由Buck电路的状态空间平均法,可得到其电压控制下的动态小信号模型,并应用PID实现其精确控制。为提高控制精度和抗干扰能力,用模糊控制器对PID参数进行实时整定,给出了仿真与实验结果及结论。关键词:Buck变 ...…

查看全部问答>

求助如何求解该放大电路的共模和差模输入阻抗

谁能求解仪用放大器的共模和差模输入阻抗,对了,这论坛怎么才能把我的原理图贴进去啊…

查看全部问答>

嵌入式实时DSP图像监控系统的设计

嵌入式实时DSP图像监控系统的设计…

查看全部问答>

Altera FPGA管脚弱上拉电阻的软件设置方法

本帖最后由 paulhyde 于 2014-9-15 08:55 编辑 Altera FPGA管脚弱上拉电阻的软件设置方法  …

查看全部问答>

混合编程调用子程序疑问?

按照2812开发板的一个混和编程调用子程序做个实验 主程序如下: int add(int a,int b); int x=0,y=0,z=0; main() {            x=3; y=17;         while ( 1 )       &n ...…

查看全部问答>

麻烦大家,初学者求解

#include        \"config.h\" #define                BEEPCON                0x00000080            &n ...…

查看全部问答>

今天面试成功了 说说感想吧

呵呵 昨天接到面试通知     先前也有几个公司来电话  但我都放弃了   这次不知道什么原因我就跑去面试了   因为最近在学习STM32,本来没打算找工作的, 智联招聘系统给我投的简历,我就包着试试 ...…

查看全部问答>

MSP-EXP430FR5739还是不错的

果断购入,和以前自己在TI买的触摸系列一起玩。看看这个数据储存速度能快多少。…

查看全部问答>

求教mplab_ide

我写了一段程序,如下: #include unsigned char led[10]={                                0x3F,/*0*/       & ...…

查看全部问答>

传统GPIB控制

GPIB接口是一种通用总线;它的主要用途是将一台或几台兼容GPIB的仪器连至PC机。GPIB接口能以100k/s至10MB/s的速度在几台不同设备之间一次交换一个字节的数据。GPIB结合了有效的握手协议后就成为测试与测量系统的首选通信方法。在效率很高的GPIB总 ...…

查看全部问答>