📄 lcd20x4.s
字号:
.module LCD20x4.c
.area text(rom, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
.dbfunc e delay_us _delay_us fV
; time -> R16,R17
.even
_delay_us::
.dbline -1
.dbline 40
; /*=======================================================
; 20x4字符液晶主程序,编译软件(ICCAVR_6.31)
; CPU内部晶振8M
; 数据线B0~B7接PORTB, E=D7 RW=D6 RS=D5
; =========================================================
; 接线图如下:
; _______________ ______________
; | --1|GND |
; | --2|+5V |
; | --3|V0 |
; | | |
; PD5|--------4|RS |
; PD6|--------5|RW |
; PD7|--------6|E |
; CPU | | |
; ATmmega16L PB0|--------7|D0 LCD20x4 |
; PB1|--------8|D1 |
; PB2|--------9|D2 |
; PB3|-------10|D3 |
; PB4|-------11|D4 |
; PB5|-------12|D5 |
; PB6|-------13|D6 |
; PB7|-------14|D7 |
; | | |
; | --15|+LED |
; | --16|-LED |
; _______________| |______________|
; ========================================================*/
; #include<ioM16V.h> //CPU_ATmmega16L
; #define RS_H asm("sbi 0x12,5") //RS设为高电平
; #define RS_L asm("cbi 0x12,5") //RS设为低电平
; #define RW_H asm("sbi 0x12,6") //RW设为高电平
; #define RW_L asm("cbi 0x12,6") //RW设为低电平
; #define E_H asm("sbi 0x12,7") //E设为高电平
; #define E_L asm("cbi 0x12,7") //E设为低电平
;
; //=======================================================
; //微秒级延时程序
; void delay_us(int time)
; {
L2:
.dbline 42
; do
; {
.dbline 43
; time--;
subi R16,1
sbci R17,0
.dbline 44
; }
L3:
.dbline 45
; while (time > 1);
ldi R24,1
ldi R25,0
cp R24,R16
cpc R25,R17
brlt L2
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbsym r time 16 I
.dbend
.dbfunc e delay_ms _delay_ms fV
; time -> R20,R21
.even
_delay_ms::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 50
; }
; //=======================================================
; //毫秒级延时程序
; void delay_ms(unsigned int time)
; {
xjmp L7
L6:
.dbline 52
.dbline 53
ldi R16,1000
ldi R17,3
xcall _delay_us
.dbline 54
subi R20,1
sbci R21,0
.dbline 55
L7:
.dbline 51
; while(time != 0)
cpi R20,0
cpc R20,R21
brne L6
X0:
.dbline -2
L5:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r time 20 i
.dbend
.dbfunc e Lcd_Busy _Lcd_Busy fc
; r -> R20
.even
_Lcd_Busy::
xcall push_gset1
.dbline -1
.dbline 61
; {
; delay_us(1000);
; time--;
; }
; }
;
; //=======================================================
; //读取lcd是否内部操作(忙碌)状态
; char Lcd_Busy()
; {
.dbline 63
; char r;
; DDRB = 0x00; //端口B设为输入方式
clr R2
out 0x17,R2
.dbline 64
; E_L;RS_L;RW_H; //E=0(致能),RS=0(命令),RW=1(读)
cbi 0x12,7
.dbline 64
cbi 0x12,5
.dbline 64
sbi 0x12,6
.dbline 65
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 66
; E_H;
sbi 0x12,7
.dbline 67
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 68
; r = PINB & 0x80; //读取lcd_data第八位
in R20,0x16
andi R20,128
.dbline 69
; E_L;
cbi 0x12,7
.dbline 70
; DDRB=0xff; //端口B设为输出方式
ldi R24,255
out 0x17,R24
.dbline 71
; return r; //读取结果返回
mov R16,R20
.dbline -2
L9:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r r 20 c
.dbend
.dbfunc e Lcd_Command _Lcd_Command fV
; Command -> R20
.even
_Lcd_Command::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 77
; }
;
; //=======================================================
; //向Lcd发送命令程序
; void Lcd_Command(unsigned char Command)
; {
L11:
.dbline 78
L12:
.dbline 78
; while(Lcd_Busy()); //判断lcd是否内部操作状态
xcall _Lcd_Busy
tst R16
brne L11
.dbline 79
; E_L;RS_L;RW_L; //E=0(致能),RS=0(命令),RW=0(写)
cbi 0x12,7
.dbline 79
cbi 0x12,5
.dbline 79
cbi 0x12,6
.dbline 80
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 81
; E_H;
sbi 0x12,7
.dbline 82
; PORTB = Command; //向Lcd发送命令
out 0x18,R20
.dbline 83
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 84
; E_L;
cbi 0x12,7
.dbline -2
L10:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r Command 20 c
.dbend
.dbfunc e Lcd_Write _Lcd_Write fV
; Data -> R20
.even
_Lcd_Write::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 90
; }
;
; //=======================================================
; //向lcd写入一个字符程序
; void Lcd_Write(unsigned char Data)
; {
L15:
.dbline 91
L16:
.dbline 91
; while(Lcd_Busy()); //判断lcd是否内部操作状态
xcall _Lcd_Busy
tst R16
brne L15
.dbline 92
; E_L;RS_H;RW_L; //E=0(致能),RS=1(数据),RW=0(写)
cbi 0x12,7
.dbline 92
sbi 0x12,5
.dbline 92
cbi 0x12,6
.dbline 93
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 94
; E_H;
sbi 0x12,7
.dbline 95
; PORTB = Data; //向lcd写入一个字符
out 0x18,R20
.dbline 96
; delay_us(2); //液晶延时子程序
ldi R16,2
ldi R17,0
xcall _delay_us
.dbline 97
; E_L;
cbi 0x12,7
.dbline -2
L14:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r Data 20 c
.dbend
.dbfunc e Lcd_Init _Lcd_Init fV
.even
_Lcd_Init::
.dbline -1
.dbline 109
; }
;
;
; /*=======================================================
; LCD第1行显示地址1~20(0x80~0x93)
; LCD第2行显示地址1~20(0xc0~0xd3)
; LCD第3行显示地址1~20(0x94~0xa7)
; LCD第4行显示地址1~20(0xd4~0xe7)
; =======================================================*/
; //初始化LCD_8位接口,4行x20字符的工作方式
; void Lcd_Init()
; {
.dbline 110
; DDRB = 0xff; //端口B设为输出方式
ldi R24,255
out 0x17,R24
.dbline 111
; DDRD = 0xff; //端口D设为输出方式
out 0x11,R24
.dbline 112
; Lcd_Command(0x38); //
ldi R16,56
xcall _Lcd_Command
.dbline 113
; Lcd_Command(0x38); //
ldi R16,56
xcall _Lcd_Command
.dbline 114
; Lcd_Command(0x38); //
ldi R16,56
xcall _Lcd_Command
.dbline 115
; Lcd_Command(0x38); //
ldi R16,56
xcall _Lcd_Command
.dbline 116
; Lcd_Command(0x08); //令显示器off
ldi R16,8
xcall _Lcd_Command
.dbline 117
; Lcd_Command(0x01); //清除显示器
ldi R16,1
xcall _Lcd_Command
.dbline 118
; Lcd_Command(0x06); //令LCD每接收到1Byte数据后,AC自动加1
ldi R16,6
xcall _Lcd_Command
.dbline 119
; Lcd_Command(0x0C); //令光标,0x0c=不显示,0x0d=显示闪动.
ldi R16,12
xcall _Lcd_Command
.dbline -2
L18:
.dbline 0 ; func end
ret
.dbend
.dbfunc e asc_tran _asc_tran fV
; asc -> R20,R21
.even
_asc_tran::
xcall push_gset1
movw R20,R16
.dbline -1
.dbline 125
; }
;
; //=======================================================
; //写ASCII字符串程序
; void asc_tran(unsigned char *asc)
; {
xjmp L21
L20:
.dbline 127
.dbline 128
movw R30,R20
ldd R16,z+0
xcall _Lcd_Write
.dbline 129
subi R20,255 ; offset = 1
sbci R21,255
.dbline 130
L21:
.dbline 126
; while((*asc) != 0) //判断字是否结束
movw R30,R20
ldd R2,z+0
tst R2
brne L20
.dbline -2
L19:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r asc 20 pc
.dbend
.dbfunc e main _main fV
.even
_main::
.dbline -1
.dbline 136
; {
; Lcd_Write(*asc); //向lcd写入字符串
; asc++; //移下一个字符
; }
; }
;
; //=======================================================
; //测试主LCD主程序
; void main()
; {
.dbline 137
; Lcd_Init(); //初始化LCD
xcall _Lcd_Init
xjmp L25
L24:
.dbline 139
.dbline 140
ldi R16,131
xcall _Lcd_Command
.dbline 141
ldi R16,<L27
ldi R17,>L27
xcall _asc_tran
.dbline 142
ldi R16,151
xcall _Lcd_Command
.dbline 143
ldi R16,<L28
ldi R17,>L28
xcall _asc_tran
.dbline 144
ldi R16,1000
ldi R17,3
xcall _delay_ms
.dbline 145
ldi R16,1
xcall _Lcd_Command
.dbline 147
ldi R16,131
xcall _Lcd_Command
.dbline 148
ldi R16,<L29
ldi R17,>L29
xcall _asc_tran
.dbline 149
ldi R16,148
xcall _Lcd_Command
.dbline 150
ldi R16,<L30
ldi R17,>L30
xcall _asc_tran
.dbline 151
ldi R16,1000
ldi R17,3
xcall _delay_ms
.dbline 152
ldi R16,1
xcall _Lcd_Command
.dbline 153
L25:
.dbline 138
xjmp L24
X1:
.dbline -2
L23:
.dbline 0 ; func end
ret
.dbend
.area data(ram, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
L30:
.blkb 20
.area idata
.byte 'h,'t,'t,'p,58,47,47,53,57,46,51,54,46,57,54,46
.byte 49,57,54,0
.area data(ram, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
L29:
.blkb 15
.area idata
.byte 45,45,45,45,45,94,95,94,45,45,45,45,45,45,0
.area data(ram, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
L28:
.blkb 15
.area idata
.byte 'w,'w,'w,46,'o,'u,'r,'a,'v,'r,46,'c,'o,'m,0
.area data(ram, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
L27:
.blkb 15
.area idata
.byte 45,45,45,45,45,64,95,64,45,45,45,45,45,45,0
.area data(ram, con, rel)
.dbfile E:\AVR应用实例\LCD20x4\LCD20x4.c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -