1602.s
来自「AVR M16单片机AD转换测试程序」· S 代码 · 共 505 行
S
505 行
.module _1602.c
.area text(rom, con, rel)
.dbfile G:\AVRsource\adc\1602.c
.dbfunc e LCD_init _LCD_init fV
.even
_LCD_init::
.dbline -1
.dbline 24
; #include <iom16v.h>
; #include <macros.h>
; #define LCD_EN_PORT PORTC
; #define LCD_RW_PORT PORTC
; #define LCD_RS_PORT PORTC
; #define LCD_DATA_PORT PORTB
; #define LCD_DATA_DDR DDRB
; #define LCD_DATA_PIN PINB
; #define LCD_EN 0x80 //portd7 out
; #define LCD_RS 0x40 //portc6 out
; #define LCD_DATA 0xf0 //portb4/5/6/7 out
;
; /*--------------------------------------------------------------------------------------------------
; Public function prototypes
; --------------------------------------------------------------------------------------------------*/
; void LCD_init (void);
; void LCD_en_write (void);
; void LCD_write_char (unsigned command,unsigned data);
; void LCD_set_xy (unsigned char x, unsigned char y);
; void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
; void delay_nus (unsigned int n);
; void delay_nms (unsigned int n);
; void LCD_init(void) //液晶初始化
; {
.dbline 25
; DDRB|=LCD_DATA; // 数据为输出
in R24,0x17
ori R24,240
out 0x17,R24
.dbline 26
; DDRC|=LCD_RS|LCD_EN; //置位RS.EN
in R24,0x14
ori R24,192
out 0x14,R24
.dbline 27
; delay_nms(15);
ldi R16,15
ldi R17,0
xcall _delay_nms
.dbline 28
; LCD_write_char(0x28,0); //4位显示
clr R18
clr R19
ldi R16,40
ldi R17,0
xcall _LCD_write_char
.dbline 29
; LCD_write_char(0x0c,0); //显示开
clr R18
clr R19
ldi R16,12
ldi R17,0
xcall _LCD_write_char
.dbline 30
; LCD_write_char(0x01,0); //清屏
clr R18
clr R19
ldi R16,1
ldi R17,0
xcall _LCD_write_char
.dbline -2
L1:
.dbline 0 ; func end
ret
.dbend
.dbfunc e LCD_write_string _LCD_write_string fV
; s -> R20,R21
; Y -> R10
; X -> R22
.even
_LCD_write_string::
xcall push_gset3
mov R10,R18
mov R22,R16
ldd R20,y+6
ldd R21,y+7
.dbline -1
.dbline 33
; }
; void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
; {
.dbline 34
; LCD_set_xy( X, Y ); //写地址
mov R18,R10
mov R16,R22
xcall _LCD_set_xy
xjmp L4
L3:
.dbline 37
.dbline 38
movw R30,R20
ldd R18,z+0
clr R19
clr R16
clr R17
xcall _LCD_write_char
.dbline 39
subi R20,255 ; offset = 1
sbci R21,255
.dbline 40
L4:
.dbline 36
;
; while (*s) // 写显示字符
movw R30,R20
ldd R2,z+0
tst R2
brne L3
.dbline -2
L2:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r s 20 pc
.dbsym r Y 10 c
.dbsym r X 22 c
.dbend
.dbfunc e LCD_set_xy _LCD_set_xy fV
; address -> R20
; y -> R20
; x -> R22
.even
_LCD_set_xy::
xcall push_gset2
mov R20,R18
mov R22,R16
.dbline -1
.dbline 43
; {
; LCD_write_char( 0, *s );
; s ++;
; }
; }
; void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
; {
.dbline 45
; unsigned char address;
; if (y == 0) address = 0x80 + x;
tst R20
brne L7
.dbline 45
mov R20,R22
subi R20,128 ; addi 128
xjmp L8
L7:
.dbline 47
; else
; address = 0xc0 + x;
mov R20,R22
subi R20,64 ; addi 192
L8:
.dbline 48
; LCD_write_char( address, 0 );
clr R18
clr R19
mov R16,R20
clr R17
xcall _LCD_write_char
.dbline -2
L6:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r address 20 c
.dbsym r y 20 c
.dbsym r x 22 c
.dbend
.dbfunc e LCD_en_write _LCD_en_write fV
.even
_LCD_en_write::
.dbline -1
.dbline 51
; }
; void LCD_en_write(void) //液晶使能
; {
.dbline 52
; LCD_EN_PORT|=LCD_EN;
sbi 0x15,7
.dbline 53
; delay_nus(1);
ldi R16,1
ldi R17,0
xcall _delay_nus
.dbline 54
; LCD_EN_PORT&=~LCD_EN;
cbi 0x15,7
.dbline -2
L9:
.dbline 0 ; func end
ret
.dbend
.dbfunc e LCD_write_char _LCD_write_char fV
; data_temp -> R22,R23
; command_temp -> R20,R21
; data -> R12,R13
; command -> R10,R11
.even
_LCD_write_char::
xcall push_gset4
movw R12,R18
movw R10,R16
.dbline -1
.dbline 57
; }
; void LCD_write_char(unsigned command,unsigned data) // 写数据
; {
.dbline 59
; unsigned command_temp,data_temp;
; command_temp=command;
movw R20,R10
.dbline 60
; data_temp=data;
movw R22,R12
.dbline 61
; delay_nus(16);
ldi R16,16
ldi R17,0
xcall _delay_nus
.dbline 62
; if(command==0)
cpi R20,0
cpc R20,R21
brne L11
X0:
.dbline 63
; {
.dbline 64
; LCD_RS_PORT|=LCD_RS; //RS=1
sbi 0x15,6
.dbline 65
; LCD_DATA_PORT&=0X0f;
in R24,0x18
andi R24,15
out 0x18,R24
.dbline 66
; LCD_DATA_PORT|=data_temp&0xf0; //写高四位
movw R24,R22
andi R24,240
andi R25,0
in R2,0x18
clr R3
or R2,R24
or R3,R25
out 0x18,R2
.dbline 67
; LCD_en_write();
xcall _LCD_en_write
.dbline 68
; data_temp=data_temp<<4;
lsl R22
rol R23
lsl R22
rol R23
lsl R22
rol R23
lsl R22
rol R23
.dbline 69
; LCD_DATA_PORT&=0X0f;
in R24,0x18
andi R24,15
out 0x18,R24
.dbline 70
; LCD_DATA_PORT|=data_temp&0xf0; //写低四位
movw R24,R22
andi R24,240
andi R25,0
in R2,0x18
clr R3
or R2,R24
or R3,R25
out 0x18,R2
.dbline 71
; LCD_en_write();
xcall _LCD_en_write
.dbline 72
; }
xjmp L12
L11:
.dbline 74
.dbline 75
cbi 0x15,6
.dbline 76
in R24,0x18
andi R24,15
out 0x18,R24
.dbline 77
movw R24,R20
andi R24,240
andi R25,0
in R2,0x18
clr R3
or R2,R24
or R3,R25
out 0x18,R2
.dbline 78
xcall _LCD_en_write
.dbline 79
lsl R20
rol R21
lsl R20
rol R21
lsl R20
rol R21
lsl R20
rol R21
.dbline 80
in R24,0x18
andi R24,15
out 0x18,R24
.dbline 81
movw R24,R20
andi R24,240
andi R25,0
in R2,0x18
clr R3
or R2,R24
or R3,R25
out 0x18,R2
.dbline 82
xcall _LCD_en_write
.dbline 83
L12:
.dbline -2
L10:
xcall pop_gset4
.dbline 0 ; func end
ret
.dbsym r data_temp 22 i
.dbsym r command_temp 20 i
.dbsym r data 12 i
.dbsym r command 10 i
.dbend
.dbfunc e delay_1us _delay_1us fV
.even
_delay_1us::
.dbline -1
.dbline 107
; else
; {
; LCD_RS_PORT&=~LCD_RS; //RS=0
; LCD_DATA_PORT&=0X0f;
; LCD_DATA_PORT|=command_temp&0xf0; //写高四位
; LCD_en_write();
; command_temp=command_temp<<4;
; LCD_DATA_PORT&=0x0f;
; LCD_DATA_PORT|=command_temp&0xf0; //写低四位
; LCD_en_write();
; }
; }
; /*void main(void)
; {
; DDRA|=LCD_DATA; // 数据为输出
; DDRC|=LCD_RS|LCD_EN; //置位RS.EN
; LCD_init();
; for(;;) //for循环
; {
; LCD_write_char(0x01,0);
; delay_nms(2);
; LCD_write_string(0,0,"Wellcome");
; LCD_write_string(0,1,"www.OURAVR.com!");
; delay_nms(5000);
; LCD_write_string(0,0,"happy new year!");
; LCD_write_string(0,1,"amork:everfriend");
; delay_nms(5000);
; }
; }*/
; /*-----------------------------------------------------------------------
; 延时函数
; 系统时钟:8M
; -----------------------------------------------------------------------*/
; void delay_1us(void) //1us延时函数
; {
.dbline 108
; asm("nop");
nop
.dbline -2
L13:
.dbline 0 ; func end
ret
.dbend
.dbfunc e delay_nus _delay_nus fV
; i -> R20,R21
; n -> R22,R23
.even
_delay_nus::
xcall push_gset2
movw R22,R16
.dbline -1
.dbline 112
; }
;
; void delay_nus(unsigned int n) //N us延时函数
; {
.dbline 113
; unsigned int i=0;
clr R20
clr R21
.dbline 114
; for (i=0;i<n;i++)
xjmp L18
L15:
.dbline 115
xcall _delay_1us
L16:
.dbline 114
subi R20,255 ; offset = 1
sbci R21,255
L18:
.dbline 114
cp R20,R22
cpc R21,R23
brlo L15
.dbline -2
L14:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r i 20 i
.dbsym r n 22 i
.dbend
.dbfunc e delay_1ms _delay_1ms fV
; i -> R16,R17
.even
_delay_1ms::
.dbline -1
.dbline 119
; delay_1us();
; }
;
; void delay_1ms(void) //1ms延时函数
; {
.dbline 121
clr R16
clr R17
xjmp L23
L20:
.dbline 121
L21:
.dbline 121
subi R16,255 ; offset = 1
sbci R17,255
L23:
.dbline 121
; unsigned int i;
; for (i=0;i<1140;i++);
cpi R16,116
ldi R30,4
cpc R17,R30
brlo L20
.dbline -2
L19:
.dbline 0 ; func end
ret
.dbsym r i 16 i
.dbend
.dbfunc e delay_nms _delay_nms fV
; i -> R20,R21
; n -> R22,R23
.even
_delay_nms::
xcall push_gset2
movw R22,R16
.dbline -1
.dbline 125
; }
;
; void delay_nms(unsigned int n) //N ms延时函数
; {
.dbline 126
; unsigned int i=0;
clr R20
clr R21
.dbline 127
; for (i=0;i<n;i++)
xjmp L28
L25:
.dbline 128
xcall _delay_1ms
L26:
.dbline 127
subi R20,255 ; offset = 1
sbci R21,255
L28:
.dbline 127
cp R20,R22
cpc R21,R23
brlo L25
.dbline -2
L24:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r i 20 i
.dbsym r n 22 i
.dbend
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?