📄 main.s
字号:
.module main.c
.area text(rom, con, rel)
.dbfile F:\ChengXuCunFang\ChengXU\AVR单片机程序存放\max485多机通讯1\主机/delay.h
.dbfunc e delay_1us _delay_1us fV
.even
_delay_1us::
.dbline -1
.dbline 15
; /* 通讯规则:
; 01:时钟7.3728 MHz/波特率9600/9个数据位/奇校验/1个停止位/硬件多机通讯功能/
; 02:通讯连接采用硬件MAX485,双向单工
; 03:MAX485的RE/DE并联接到单片机的PD2脚(高电平发送/低电平接收)
; 04:所有MAX485的A脚并联/B脚并联/D脚接TXD/R脚接RXD
; 05:每个上行/下行的数据包的字节个数都是一样的(通讯数据量)
; 06:数据包格式: 地址_数据1_数据2_数据3_数据n_CRC8校验码
; 07:所有单元的数据接收都是采用中断+查询的方式
; 08:总是由主机向从机下发一个数据包,从机收到数据包并校验正确后向主机回复一个数据包
; 09:不管是主机还是从机,如果收到的数据包有任何错误,都将丢弃该数据包,等效于没有接收
; 10:通讯采用主机轮询方式,从机之间不能相互通讯,必须通过主机才能交换数据
; 11:无效地址是0,主机地址是1,从机地址是11.12.13...广播地址是255
; */
;
; #include<iom16v.h>
.dbline 16
; #include <macros.h>
nop
.dbline -2
L1:
.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 20
; #include "1602.h"
; #include "delay.h"
;
; #define amount 10
.dbline 21
; #define address 1
clr R20
clr R21
.dbline 22
;
xjmp L6
L3:
.dbline 23
xcall _delay_1us
L4:
.dbline 22
subi R20,255 ; offset = 1
sbci R21,255
L6:
.dbline 22
cp R20,R22
cpc R21,R23
brlo L3
.dbline -2
L2:
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 27
; unsigned char send[amount];
; unsigned char inbox[amount];
; unsigned char n=0;//记忆叫断接收的次数
;
;
.dbline 29
clr R16
clr R17
xjmp L11
L8:
.dbline 29
L9:
.dbline 29
subi R16,255 ; offset = 1
sbci R17,255
L11:
.dbline 29
;
; //end
cpi R16,116
ldi R30,4
cpc R17,R30
brlo L8
.dbline -2
L7:
.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 33
;
; //波特率9600/9个数据位/1个停止位/奇校验/收发开启/接收中断/地址过滤
; void usart_init(void)
; {
.dbline 34
; UCSRA=0x01;
clr R20
clr R21
.dbline 35
; UCSRB=0x9C;
xjmp L16
L13:
.dbline 36
xcall _delay_1ms
L14:
.dbline 35
subi R20,255 ; offset = 1
sbci R21,255
L16:
.dbline 35
cp R20,R22
cpc R21,R23
brlo L13
.dbline -2
L12:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbsym r i 20 i
.dbsym r n 22 i
.dbend
.dbfile F:\ChengXuCunFang\ChengXU\AVR单片机程序存放\max485多机通讯1\主机/1602.h
.dbfunc e LCD_init _LCD_init fV
.even
_LCD_init::
.dbline -1
.dbline 44
; UCSRC=0xB6;
; UBRRH=0x00;
; UBRRL=47;
; }
;
; unsigned char crc8(unsigned char *ptr, unsigned char len)
; {
; unsigned char i;
; unsigned char crc=0;
.dbline 45
; while(len--!=0)
in R24,0x14
ori R24,15
out 0x14,R24
.dbline 46
; {
sbi 0x14,5
.dbline 47
; for(i=1; i!=0; i*=2)
sbi 0x14,4
.dbline 48
; {
ldi R16,40
xcall _LCD_write_command
.dbline 49
; if((crc&1)!=0) {crc/=2; crc^=0x8C;}
xcall _LCD_en_write
.dbline 50
; else crc/=2;
ldi R16,40
ldi R17,0
xcall _delay_nus
.dbline 51
; if((*ptr&i)!=0) crc^=0x8C;
ldi R16,40
xcall _LCD_write_command
.dbline 52
; }
ldi R16,12
xcall _LCD_write_command
.dbline 53
; ptr++;
ldi R16,1
xcall _LCD_write_command
.dbline 54
; }
ldi R16,2
ldi R17,0
xcall _delay_nms
.dbline -2
L17:
.dbline 0 ; func end
ret
.dbend
.dbfunc e LCD_en_write _LCD_en_write fV
.even
_LCD_en_write::
.dbline -1
.dbline 61
; return(crc);
; }
;
; #pragma interrupt_handler RXC_Int: 12
; void RXC_Int(void)
; {
; if( UCSRA&28 ){ n=UDR; n=0; UCSRA|=0x01; } else //接收出错就重新打开地址帧筛选功能
.dbline 62
; {
sbi 0x15,5
.dbline 63
; if( UCSRB&2 ) n=0; //检测到地址信息时计数清零
ldi R16,1
ldi R17,0
xcall _delay_nus
.dbline 64
; inbox[n]=UDR; n++; //把接收到的数据保存到收件箱
cbi 0x15,5
.dbline -2
L18:
.dbline 0 ; func end
ret
.dbend
.dbfunc e LCD_write_command _LCD_write_command fV
; command -> R20
.even
_LCD_write_command::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 68
; if( inbox[0]==address ) UCSRA&=254; else UCSRA|=0x01; //地址筛选
; }
; }
;
.dbline 82
; void usart_out(unsigned char *datas,unsigned char n)
; {
; unsigned char i=0;
; PORTD |=(1<<PD4); //使MAX485处于发送状态
; while(i<n) //一共发送n个数据
; {
; if(i==0) UCSRB|=1; else UCSRB&=254;
; UDR=*(datas+i); //装载数据开始发送
; while((UCSRA&64)==0); //等待发送结束
; UCSRA|=64; //清除发送结束标志
; i++; //发送次数统计
; }
; PORTD&=~(1<<PD4); //使MAX485处于接收状态
; }
ldi R16,16
ldi R17,0
xcall _delay_nus
.dbline 83
; void main()
cbi 0x15,4
.dbline 84
; {
in R24,0x15
andi R24,240
out 0x15,R24
.dbline 85
; usart_init();
mov R24,R20
swap R24
andi R24,#0x0F
andi R24,15
in R2,0x15
or R2,R24
out 0x15,R2
.dbline 86
; LCD_init(); //液晶初始化
xcall _LCD_en_write
.dbline 87
; DDRD |=(1<<PD4);
in R24,0x15
andi R24,240
out 0x15,R24
.dbline 88
; DDRA |=0x07;
mov R24,R20
andi R24,15
in R2,0x15
or R2,R24
out 0x15,R2
.dbline 89
; SEI(); //打开全局中断,需要macros.h支持
xcall _LCD_en_write
.dbline -2
L19:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r command 20 c
.dbend
.dbfunc e LCD_write_data _LCD_write_data fV
; data -> R20
.even
_LCD_write_data::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 95
; LCD_write_string(0,0,"Send:");
; LCD_write_string(0,1,"Rev:");
; while(1){
; send[3]=9; //更新发件箱的数据(测试代码)
; send[0]=11; //指向从机地址
; send[amount-1]=crc8(send,amount-1); //计算发件箱数据的crc8校验码
.dbline 109
; usart_out(send,amount); //将发件箱的数据send[]发送出去;
; LCD_write_string(5,0,send);
; n=0; //计数复位,准备接收新数据
; delay_nms(15); //等待从机回复数据,这个时间要计算好
;
; if(n==amount && inbox[amount-1]==crc8(inbox,amount-1)) //接收正确处理与测试
; {
; PORTA|=(1<<PA1); delay_nms(10); PORTA&=~(1<<PA1);
; }
; else //接收错误处理与测试
; {
; PORTA|=(1<<PA2); delay_nms(10); PORTA&=~(1<<PA2);
; }
; }
ldi R16,16
ldi R17,0
xcall _delay_nus
.dbline 110
; }
sbi 0x15,4
.dbline 111
; }
in R24,0x15
andi R24,240
out 0x15,R24
.dbline 112
; }
mov R24,R20
swap R24
andi R24,#0x0F
andi R24,15
in R2,0x15
or R2,R24
out 0x15,R2
.dbline 113
; }
xcall _LCD_en_write
.dbline 115
; }
; }
in R24,0x15
andi R24,240
out 0x15,R24
.dbline 116
; }
mov R24,R20
andi R24,15
in R2,0x15
or R2,R24
out 0x15,R2
.dbline 117
; }
xcall _LCD_en_write
.dbline -2
L20:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r data 20 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 124
; }
; }
; }
; }
; }
; }
; }
.dbline 126
; }
; }
tst R20
brne L22
.dbline 126
mov R20,R22
subi R20,128 ; addi 128
xjmp L23
L22:
.dbline 127
; }
mov R20,R22
subi R20,64 ; addi 192
L23:
.dbline 128
; }
mov R16,R20
xcall _LCD_write_command
.dbline -2
L21:
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_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 132
; }
; }
; }
; }
.dbline 133
; }
mov R18,R10
mov R16,R22
xcall _LCD_set_xy
xjmp L26
L25:
.dbline 135
.dbline 136
movw R30,R20
ldd R16,z+0
xcall _LCD_write_data
.dbline 137
subi R20,255 ; offset = 1
sbci R21,255
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -