Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603678
  • 博文数量: 99
  • 博客积分: 5128
  • 博客等级: 大校
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-27 19:40
文章分类

全部博文(99)

文章存档

2012年(3)

2011年(5)

2010年(4)

2009年(31)

2008年(56)

分类:

2008-11-26 00:23:00

好了,我承认当时写这程序的时候比较脑残 -v- ,对于学51单片机的同学对1602液晶应该不会陌生,这个差不多是作为必学的模块。这模块比较好驱动,并口送数据很简单。之前AVR的代码上面写的比较详细的注释,EMC单片机部分基本上没啥注释,当时调试通了就算了,呵呵。对从事EMC单片机的同行应该有所帮助,虽然估计EMC单片机基本不会配这个液晶吧?毕竟是价格敏感领域。

本程序是本人原创,基于开源的思想(open resources),你可以任意下载使用和修改本代码。暂时不加GPL约束,自由的使用吧。如果你有更好的修改意见,可以发一份给我。

/*
 * 项目:驱动1602液晶程序
 * 作者:Etual / Etual@163.com
 * 日期:2008-11-25
 */

1602.dt

 

/*
                * 1602的建立比较慢,上电需要比较长的时间
                */

               call Delay500MS
               /*
                * 初始化动作,芯片资料有详细说明
                */

               mov a,@00111000b
               call WRITE_INIT
               call Delay5MS
               mov a,@00111000b
               call WRITE_INIT
               call Delay5MS
               mov a,@00111000b
               call WRITE_INIT
               call Delay5MS
               /*
                * 初始化1602的寄存器等
                */

               CALL LCD_1602_INIT
               /*
                * 这里是测试功能,让液晶显示字符
                */

               mov a,@0X80 ; ADDRESS
               call WRITE_REG
               
               mov a,@0XBE
               call WRITE_DAT
               mov a,@0XB2
               call WRITE_DAT
               mov a,@0XDA
               call WRITE_DAT
               mov a,@0XB2
               call WRITE_DAT
               
               mov a,@0XC0 ; ADDRESS
               call WRITE_REG
               /*
                * 功能测试
                */

               mov a,@0X45
               call WRITE_DAT
               mov a,@0X74
               call WRITE_DAT
               mov a,@0X75
               call WRITE_DAT
               mov a,@0X61
               call WRITE_DAT
               mov a,@0X6C
               call WRITE_DAT
               mov a,@0X40
               call WRITE_DAT
               mov a,@0X31
               call WRITE_DAT
               mov a,@0X36
               call WRITE_DAT
               mov a,@0X33
               call WRITE_DAT
               mov a,@0X2E
               call WRITE_DAT
               mov a,@0X63
               call WRITE_DAT
               mov a,@0X6F
               call WRITE_DAT
               mov a,@0X6D
               call WRITE_DAT
               
/*
 * 死循环延时5ms,测试用,简单为重
 */

Delay5MS:
               clr temp0
               mov a,@1
               mov temp1,a
d5ms_loop:
               jz temp0
               jmp $-1
               djz temp1
               jmp d5ms_loop
ret

/*
 * 延时500ms
 */

Delay500MS:
               clr temp0
               mov a,@255
               mov temp1,a
d500ms_loop:
               jz temp0
               jmp $-1
               djz temp1
               jmp d500ms_loop
ret

/*
 * 液晶连接脚定义
 */

CNT_PORT == 0X05
LCD_RS == 2
LCD_RW == 1
LCD_EN == 0
DAT_PORT == 0X06

LCD_COUNT == 0x20

/*
 * 液晶初始化程序
 */

LCD_1602_INIT:
               mov a,@00111000b ; function set
               CALL WRITE_REG

               MOV A,@0X08
               CALL WRITE_REG

               MOV A,@0X01
               CALL WRITE_REG
               CALL Delay5MS
               
               MOV A,@00000110b ; entire mode
               CALL WRITE_REG

               MOV A,@00001101b ; display on./ off
               CALL WRITE_REG

RET


/*
 * 检测是否忙
 */

LCD_CHECKBUSY:
               mov a,@0xff
               mov DAT_PORT,a
               iow DAT_PORT
CB_LOOP:
               nop
               bc CNT_PORT,LCD_EN
               bc CNT_PORT,LCD_RS
               bs CNT_PORT,LCD_RW
               nop
               BS CNT_PORT,LCD_EN
               nop
               mov a,DAT_PORT
               nop
               bc CNT_PORT,LCD_EN
               and a,@0x80 ; 读出来的数据位bit7 = 1 busy ,=0 free
               jbs psw,z
               jmp CB_LOOP
               mov a,@0
               iow DAT_PORT
               mov DAT_PORT,a
RET

/*
 * 写命令初始化
 */

WRITE_INIT:
               bc CNT_PORT,LCD_EN
               bc CNT_PORT,LCD_RS
               BC CNT_PORT,LCD_RW
               nop
               BS CNT_PORT,LCD_EN
               NOP
               MOV DAT_PORT,A
               NOP
               NOP
               BC CNT_PORT,LCD_EN
RET

/*
 * 写寄存器
 */

WRITE_REG:
               mov temp0,a
               call LCD_CHECKBUSY
               mov a,temp0
               bc CNT_PORT,LCD_EN
               bc CNT_PORT,LCD_RS
               BC CNT_PORT,LCD_RW
               nop
               BS CNT_PORT,LCD_EN
               NOP
               MOV DAT_PORT,A
               NOP
               NOP
               BC CNT_PORT,LCD_EN
RET

/*
 * 写数据
 */

WRITE_DAT:
               mov temp0,a
               call LCD_CHECKBUSY
               mov a,temp0
               
               bc CNT_PORT,LCD_EN
               bs CNT_PORT,LCD_RS
               BC CNT_PORT,LCD_RW
               nop
               BS CNT_PORT,LCD_EN
               NOP
               MOV DAT_PORT,A
               NOP
               NOP
               BC CNT_PORT,LCD_EN
               NOP
RET
     

阅读(1292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~