📄 lcd.s
字号:
.module lcd.c
.area text(rom, con, rel)
.dbfile G:\Acceleratoion\Acceleration\lcd.c
.dbfunc e delay1us _delay1us fV
.even
_delay1us::
.dbline -1
.dbline 8
; //LCD.c
; #include "lcd.h"
; #include <iom16v.h>
; #include <macros.h>
;
; //保证平时LCD不在输出
; void delay1us() //4MHz Crystal
; {
.dbline 9
; NOP();
nop
.dbline 10
; NOP();
nop
.dbline 11
; NOP();
nop
.dbline 12
; NOP();
nop
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e lcdWriteInstruction _lcdWriteInstruction fV
; testBusyFlag -> R22
; instruction -> R20
.even
_lcdWriteInstruction::
xcall push_gset2
mov R22,R18
mov R20,R16
.dbline -1
.dbline 16
; }
;
; void lcdWriteInstruction(char instruction, char testBusyFlag)
; {
.dbline 17
; if(testBusyFlag)
tst R22
breq L3
.dbline 18
; lcdWaitUntilReady();
xcall _lcdWaitUntilReady
L3:
.dbline 20
; //1. Write Control Line
; DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E; //set control output
in R24,0x1a
ori R24,28
out 0x1a,R24
.dbline 21
; PORT_LCDCTL &= ~(LCD_RS|LCD_RW|LCD_E);
in R24,0x1b
andi R24,227
out 0x1b,R24
.dbline 24
; //RS=0(instruction); RW=0(write); E=0;
; //2. Write Data Line
; DDR_LCDDATA = 0xff;
ldi R24,255
out 0x11,R24
.dbline 25
; PORT_LCDDATA = instruction;
out 0x12,R20
.dbline 27
;
; delay1us();//wait for address set (RW) (140ns)
xcall _delay1us
.dbline 29
;
; PORT_LCDCTL |= LCD_E; //E=1;
sbi 0x1b,4
.dbline 30
; delay1us();//wait for pwen (450ns)
xcall _delay1us
.dbline 31
; PORT_LCDCTL &= ~LCD_E; //E=0;
cbi 0x1b,4
.dbline 33
;
; delay1us();//wait for E cycle
xcall _delay1us
.dbline -2
L2:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r testBusyFlag 22 c
.dbsym r instruction 20 c
.dbend
.dbfunc e lcdWriteData _lcdWriteData fV
; testBusyFlag -> R22
; data -> R20
.even
_lcdWriteData::
xcall push_gset2
mov R22,R18
mov R20,R16
.dbline -1
.dbline 37
; }
;
; void lcdWriteData(char data, char testBusyFlag)
; {
.dbline 38
; if(testBusyFlag)
tst R22
breq L6
.dbline 39
; lcdWaitUntilReady();
xcall _lcdWaitUntilReady
L6:
.dbline 41
; //1. Write Control Line
; DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E; //set control output
in R24,0x1a
ori R24,28
out 0x1a,R24
.dbline 42
; PORT_LCDCTL &= ~(LCD_RW|LCD_E);
in R24,0x1b
andi R24,231
out 0x1b,R24
.dbline 44
; //RW=0(write); E=0;
; PORT_LCDCTL |= LCD_RS;
sbi 0x1b,2
.dbline 47
; //RS=1(data)
; //2. Write Data Line
; DDR_LCDDATA = 0xff;
ldi R24,255
out 0x11,R24
.dbline 48
; PORT_LCDDATA = data;
out 0x12,R20
.dbline 50
;
; delay1us();//wait for address set (RW) (140ns)
xcall _delay1us
.dbline 52
;
; PORT_LCDCTL |= LCD_E; //E=1;
sbi 0x1b,4
.dbline 53
; delay1us();//wait for pwen (450ns)
xcall _delay1us
.dbline 54
; PORT_LCDCTL &= ~LCD_E; //E=0;
cbi 0x1b,4
.dbline 56
; //wait for E cycle
; delay1us();//wait for E cycle
xcall _delay1us
.dbline -2
L5:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r testBusyFlag 22 c
.dbsym r data 20 c
.dbend
.dbfunc e lcdWaitUntilReady _lcdWaitUntilReady fV
; bf -> <dead>
.even
_lcdWaitUntilReady::
.dbline -1
.dbline 60
; }
;
; void lcdWaitUntilReady()
; {
.dbline 63
; char bf;
; //1. HiZ Data Line
; DDR_LCDDATA = 0x00;
clr R2
out 0x11,R2
.dbline 64
; PORT_LCDDATA = 0x00;
out 0x12,R2
.dbline 67
;
; //2. Write Control Line
; DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E; //set control output
in R24,0x1a
ori R24,28
out 0x1a,R24
.dbline 68
; PORT_LCDCTL &= ~(LCD_RS|LCD_E);
in R24,0x1b
andi R24,235
out 0x1b,R24
.dbline 70
; // RS=0(instruction); E=0;
; PORT_LCDCTL |= LCD_RW;
sbi 0x1b,3
.dbline 73
; //RW=1(read);
;
; delay1us();//wait for address set (RW) (140ns)
xcall _delay1us
.dbline 76
;
; //3. Enable E and read
; PORT_LCDCTL |= LCD_E; //E=1;
sbi 0x1b,4
.dbline 77
; delay1us();//wait for pwen (450ns)
xcall _delay1us
L9:
.dbline 79
L10:
.dbline 79
; //4. test busy flag
; while(PIN_LCDDATA&0x80);//loop until bf=0;
sbic 0x10,7
rjmp L9
.dbline 82
;
; //5. Disable E
; PORT_LCDCTL &= ~LCD_E; //E=0;
cbi 0x1b,4
.dbline 83
; PORT_LCDCTL &= ~(LCD_RS|LCD_RW|LCD_E);//set RW=0(write)
in R24,0x1b
andi R24,227
out 0x1b,R24
.dbline 85
;
; delay1us();//wait for E period
xcall _delay1us
.dbline -2
L8:
.dbline 0 ; func end
ret
.dbsym l bf 1 c
.dbend
.dbfunc e lcdClearScreen _lcdClearScreen fV
.even
_lcdClearScreen::
.dbline -1
.dbline 94
; }
;
;
; //================================================================
; //==================Basic Operation================================
; //===================================================================
;
; void lcdClearScreen()
; {
.dbline 96
; //wait for idle
; lcdWriteInstruction(0x01,1);
ldi R18,1
ldi R16,1
xcall _lcdWriteInstruction
.dbline -2
L12:
.dbline 0 ; func end
ret
.dbend
.dbfunc e lcdWriteChar _lcdWriteChar fV
; ADD -> R20
; ch -> y+4
; col -> R20
; row -> R22
.even
_lcdWriteChar::
xcall push_gset2
mov R20,R18
mov R22,R16
.dbline -1
.dbline 102
; //Screen clear
; //AC=0, auto increase
; }
;
; void lcdWriteChar(char row, char col, char ch)
; {
.dbline 105
; char ADD;
; //row=0:top; row=1:bottom; col=0:right; col=7
; ADD = (row*0x40)|col;
ldi R24,64
mul R24,R22
mov R2,R0
or R2,R20
mov R20,R2
.dbline 106
; lcdWriteInstruction(0x80|ADD,1);
ldi R18,1
mov R16,R2
ori R16,128
xcall _lcdWriteInstruction
.dbline 107
; lcdWriteData(ch,1);
ldi R18,1
ldd R16,y+4
xcall _lcdWriteData
.dbline -2
L13:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r ADD 20 c
.dbsym l ch 4 c
.dbsym r col 20 c
.dbsym r row 22 c
.dbend
.dbfunc e lcdWriteString _lcdWriteString fV
; ADD -> R20
; cnt -> R20
; pCh -> R22,R23
; num -> R10
; col -> R20
; row -> R12
.even
_lcdWriteString::
xcall push_gset4
mov R20,R18
mov R12,R16
ldd R10,y+8
ldd R22,y+10
ldd R23,y+11
.dbline -1
.dbline 111
; }
;
; void lcdWriteString(char row, char col, char num, const char* pCh)
; {
.dbline 115
; char ADD;
; char cnt;
; //row=0:top; row=1:bottom; col=0:right; col=7
; ADD = (row*0x40)|col;
ldi R24,64
mul R24,R12
mov R2,R0
or R2,R20
.dbline 116
; lcdWriteInstruction(0x80|ADD,1);
ldi R18,1
mov R16,R2
ori R16,128
xcall _lcdWriteInstruction
.dbline 117
; for(cnt=0;cnt<num;cnt++)
clr R20
xjmp L18
L15:
.dbline 118
ldi R18,1
mov R30,R20
clr R31
add R30,R22
adc R31,R23
lpm R16,Z
xcall _lcdWriteData
L16:
.dbline 117
inc R20
L18:
.dbline 117
cp R20,R10
brlo L15
.dbline -2
L14:
xcall pop_gset4
.dbline 0 ; func end
ret
.dbsym r ADD 20 c
.dbsym r cnt 20 c
.dbsym r pCh 22 pkc
.dbsym r num 10 c
.dbsym r col 20 c
.dbsym r row 12 c
.dbend
.dbfunc e lcdWriteNumber _lcdWriteNumber fV
; ADD -> R22
; leadingZero -> R20
; digit -> R22
; b -> R10,R11
; i -> R12
; x -> R14,R15
; n -> y+10
; col -> R22
; row -> R10
.even
_lcdWriteNumber::
xcall push_gset5
mov R22,R18
mov R10,R16
ldd R14,y+12
ldd R15,y+13
.dbline -1
.dbline 122
; lcdWriteData(pCh[cnt],1);
; }
;
; void lcdWriteNumber(char row, char col, char n, unsigned short x)
; {// n<=5!!! //if right aglined, col+n==max digits.
.dbline 127
; char ADD;
; char i;
; unsigned short b; //b=pow(10,j);
; char digit;
; char leadingZero = 1;
ldi R20,1
.dbline 131
;
; //row=0:top; row=1:bottom; col=0:right; col=7
; //Move cursor
; ADD = (row*0x40)|col;
ldi R24,64
mul R24,R10
mov R2,R0
or R2,R22
mov R22,R2
.dbline 132
; lcdWriteInstruction(0x80|ADD,1);
ldi R18,1
mov R16,R2
ori R16,128
xcall _lcdWriteInstruction
.dbline 134
; //Calculate pow(10,n-1)
; b=1;
ldi R24,1
ldi R25,0
movw R10,R24
.dbline 135
; for(i=1;i<n;i++)
clr R12
inc R12
xjmp L23
L20:
.dbline 136
ldi R16,10
ldi R17,0
movw R18,R10
xcall empy16s
movw R10,R16
L21:
.dbline 135
inc R12
L23:
.dbline 135
ldd R0,y+10
cp R12,R0
brlo L20
.dbline 138
; b *= 10;
; //Get digit n..1
; for(i=n;i>=1;i--)
mov R12,R0
xjmp L27
L24:
.dbline 139
; {
.dbline 140
; digit = x/b; // x\pow(10,n-1..0);
movw R18,R10
movw R16,R14
xcall div16u
mov R22,R16
.dbline 141
; if (leadingZero && (digit==0) && (i>1))
tst R20
breq L28
tst R16
brne L28
ldi R24,1
cp R24,R12
brsh L28
.dbline 142
; lcdWriteData(' ',1);
ldi R18,1
ldi R16,32
xcall _lcdWriteData
xjmp L29
L28:
.dbline 144
; else
; {
.dbline 145
; leadingZero = 0;
clr R20
.dbline 146
; lcdWriteData(digit+48,1);
ldi R18,1
mov R16,R22
subi R16,208 ; addi 48
xcall _lcdWriteData
.dbline 147
; }
L29:
.dbline 148
mov R16,R22
clr R17
movw R18,R10
xcall empy16s
sub R14,R16
sbc R15,R17
.dbline 149
ldi R18,10
ldi R19,0
movw R16,R10
xcall div16u
movw R10,R16
.dbline 150
L25:
.dbline 138
dec R12
L27:
.dbline 138
mov R24,R12
cpi R24,1
brsh L24
.dbline -2
L19:
xcall pop_gset5
.dbline 0 ; func end
ret
.dbsym r ADD 22 c
.dbsym r leadingZero 20 c
.dbsym r digit 22 c
.dbsym r b 10 s
.dbsym r i 12 c
.dbsym r x 14 s
.dbsym l n 10 c
.dbsym r col 22 c
.dbsym r row 10 c
.dbend
.dbfunc e lcdSwichOn _lcdSwichOn fV
.even
_lcdSwichOn::
.dbline -1
.dbline 157
; x -= digit*b;
; b /= 10;
; }
; }
;
;
;
;
; void lcdSwichOn()
; {
.dbline 159
; //wait for idle
; lcdWriteInstruction(0xC0,1);
ldi R18,1
ldi R16,192
xcall _lcdWriteInstruction
.dbline -2
L30:
.dbline 0 ; func end
ret
.dbend
.dbfunc e _delay_ms __delay_ms fV
; cnt -> R20,R21
; t -> R20
.even
__delay_ms::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 165
; //Screen clear
; //AC=0, auto increase
; }
;
; void _delay_ms(char t)
; {
.dbline 166
; int cnt = t*1000;
mov R18,R20
clr R19
ldi R16,1000
ldi R17,3
xcall empy16s
movw R20,R16
.dbline 167
xjmp L35
L32:
.dbline 167
L33:
.dbline 167
subi R20,1
sbci R21,0
L35:
.dbline 167
; for(;cnt>0;cnt--);
clr R2
clr R3
cp R2,R20
cpc R3,R21
brlt L32
.dbline -2
L31:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r cnt 20 I
.dbsym r t 20 c
.dbend
.dbfunc e lcdInit _lcdInit fV
.even
_lcdInit::
.dbline -1
.dbline 171
; }
;
; void lcdInit()
; {
.dbline 172
; DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E; //LCD控制线在PD口,故将其设为输出
in R24,0x1a
ori R24,28
out 0x1a,R24
.dbline 173
; DDR_LCDDATA = 0xff; //E\RS\RW等接在PA口
ldi R24,255
out 0x11,R24
.dbline 175
;
; _delay_ms(15);
ldi R16,15
xcall __delay_ms
.dbline 176
; lcdWriteInstruction(0x38,0); //设为8位接口模式,显示2行字符,busy=0不检测忙信号
clr R18
ldi R16,56
xcall _lcdWriteInstruction
.dbline 177
; _delay_ms(5);
ldi R16,5
xcall __delay_ms
.dbline 178
; lcdWriteInstruction(0x38,0);
clr R18
ldi R16,56
xcall _lcdWriteInstruction
.dbline 179
; _delay_ms(5);
ldi R16,5
xcall __delay_ms
.dbline 180
; lcdWriteInstruction(0x38,0);
clr R18
ldi R16,56
xcall _lcdWriteInstruction
.dbline 182
;
; lcdWriteInstruction(0x38,1);
ldi R18,1
ldi R16,56
xcall _lcdWriteInstruction
.dbline 183
; lcdWriteInstruction(0x08,1); //显示关闭
ldi R18,1
ldi R16,8
xcall _lcdWriteInstruction
.dbline 184
; lcdWriteInstruction(0x01,1); //显示清屏
ldi R18,1
ldi R16,1
xcall _lcdWriteInstruction
.dbline 185
; lcdWriteInstruction(0x06,1); //写入新数据后光标右移
ldi R18,1
ldi R16,6
xcall _lcdWriteInstruction
.dbline 186
; lcdWriteInstruction(0x0c,1); //显示功能开,显示光标
ldi R18,1
ldi R16,12
xcall _lcdWriteInstruction
.dbline -2
L36:
.dbline 0 ; func end
ret
.dbend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -