⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.s

📁 本文提出了基于MCU和RS485总线的嵌入式系统的常用设计方法
💻 S
📖 第 1 页 / 共 2 页
字号:
	.module main.c
	.area text(rom, con, rel)
	.dbfile F:\ChengXuCunFang\ChengXU\AVR单片机程序存放\max485多机通讯1\新建文件夹\485_slave2/delay.h
	.dbfunc e delay_1us _delay_1us fV
	.even
_delay_1us::
	.dbline -1
	.dbline 15
; /*******************************************************************************
;                            485多机通讯
; --------------------------------------------------------------------------------
; 通讯规则: 
; 1:时钟7.3728 MHz/波特率9600/9个数据位/奇校验/1个停止位/硬件多机通讯功能/ 
; 2:通讯连接采用硬件MAX485,双向单工 
; 3:每个上行/下行的数据包的字节个数都是一样的(通讯数据量) 
; 4:每个上行/下行的数据包都采用CRC8校验 
; 5:数据接收采用中断+查询的方式 
; 6:总是由主机向从机发送一个数据包,从机收到数据包后向主机回复一个数据包 
; 7:不管是主机还是从机,如果收到的数据包有任何错误,都将丢弃该数据包,等效于没有接收 
; 8:从机之间不能相互通讯,必须通过主机才能交换数据 
; 9:无效地址是0,主机地址是1,从机地址是2.3.4......广播地址是255 
; ********************************************************************************/ 
; 
	.dbline 16
; #include <iom16v.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 <macros.h> 
; #include "1602.h" 
; #include "delay.h" 
; #include "usart.h" 
	.dbline 21
; #include "crc8.h" //CRC校验函数就在这个文件里面 
	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
; #define amount 10 //设定通讯数据量(包括1个地址帧,n个数据帧,1个校验帧) 
; #define address 2 //请在这里设定本机地址 
; 
; unsigned char send[amount]; //发件箱 
; unsigned char inbox[amount]; //收件箱 
	.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
; unsigned char n=0; //记忆中断次数 
; unsigned char flag_me=0;     //个人数据标志
	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
; unsigned char flag_all=0;    //广播数据标志
; 
; //-------------------从机接收子程序-------------------------------- 
; #pragma interrupt_handler RXC_Int: 12
	.dbline 34
; void RXC_Int(void) 		  		   	     //接收中断 
	clr R20
	clr R21
	.dbline 35
; { 
	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\新建文件夹\485_slave2/1602.h
	.dbfunc e LCD_init _LCD_init fV
	.even
_LCD_init::
	.dbline -1
	.dbline 44
;  unsigned char ERROR=0; 
;  
;  if( UCSRA&0x04 || UCSRA&0x10 ) ERROR=1; //记录奇偶效验错误或者帧错误 
;  inbox[n]=UDR; 	   			  			 //把接收到的数据保存到收件箱 
;  n++;
;   									 //记忆接收的次数 
; 
;  if(ERROR) 								 //如果通讯有错.... 
;   { 
	.dbline 45
;    //n=0;  								 //接收计数清0 
	in R24,0x14
	ori R24,15
	out 0x14,R24
	.dbline 46
;    inbox[0]=0; 							 //把地址改为无效地址0 
	sbi 0x14,5
	.dbline 47
;    UCSRA|=0x01; 						 //重新打开接收器的地址帧筛选功能 
	sbi 0x14,4
	.dbline 48
;   }
	ldi R16,40
	xcall _LCD_write_command
	.dbline 49
;  //如果地址匹配本机或者是广播地址就关闭地址筛选(多机通讯)功能 (关闭是为了保证后面的数据全部接收到)
	xcall _LCD_en_write
	.dbline 50
;  if(inbox[0]==address ||inbox[0]==0xff) {UCSRA&=~0x01; }
	ldi R16,40
	ldi R17,0
	xcall _delay_nus
	.dbline 51
;  if(n==amount) 		  					 //接收到amount个数据以后... 
	ldi R16,40
	xcall _LCD_write_command
	.dbline 52
;   {  
	ldi R16,12
	xcall _LCD_write_command
	.dbline 53
;    n=0; 								 //接收计数清0 
	ldi R16,1
	xcall _LCD_write_command
	.dbline 54
;    UCSRA|=0x01; 						 //重新打开接收器的地址帧筛选功能 
	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
;    
;    
;    
;   
;    if(inbox[amount-1]==crc8(inbox,amount-1)) //如果crc8校验正确就... 
;     {
;      if(inbox[0]==address)flag_me=1; 		 //本机地址
	.dbline 62
; 	 //接收完数据后会置标志位,在读取数据后要把标志位置零	 
	sbi 0x15,5
	.dbline 63
; 	 if(inbox[0]==255) flag_me=1;    //广播地址,接收后不要回复
	ldi R16,1
	ldi R17,0
	xcall _delay_nus
	.dbline 64
; 	 //接收完数据后会置标志位,在读取数据后要把标志位置零	
	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
; 	} 
;   } 
;   
; } 
	.dbline 82
; 
; 
; void out_485(unsigned char *ptr)
; {unsigned char i;
; 
;    send[0]=1; 						 //发件箱地址指向主机 
;    for(i=1;i<amount-1;i++)send[i]=*ptr++;//把发送的数据方到发信箱 	 
;    send[amount-1]=crc8(send,amount-1); //计算发件箱的crc8校验码 
;    usart_out(send,amount); //将发件箱的数据send[]发送出去
; 
; }
; 
; //------------------------主函数---------------------------------
; void main(void) 
	ldi R16,16
	ldi R17,0
	xcall _delay_nus
	.dbline 83
; { 
	cbi 0x15,4
	.dbline 84
;  usart_init(); 
	in R24,0x15
	andi R24,240
	out 0x15,R24
	.dbline 85
; SEI(); 
	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
;  LCD_write_string(0,0,"out:");
	in R24,0x15
	andi R24,240
	out 0x15,R24
	.dbline 88
;  LCD_write_string(0,1,"int:");
	mov R24,R20
	andi R24,15
	in R2,0x15
	or R2,R24
	out 0x15,R2
	.dbline 89
; while (1) 
	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
; { 
;  while(flag_me)
;  { LCD_write_string(4,1,inbox);
;    n=0; //中断次数清0 
;    inbox[0]=0; //收件箱地址清0 
;    flag_me=0;
	.dbline 109
;    out_485("2,ok!!!!");
;    LCD_write_string(4,0,send);
;    LCD_write_string(13,0,"     ");
;    LCD_write_string(13,1,"     ");
;    
;    
;  }
; }; 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
	ldi R16,16
	ldi R17,0
	xcall _delay_nus
	.dbline 110
; } //end 
	sbi 0x15,4
	.dbline 111
; } //end 
	in R24,0x15
	andi R24,240
	out 0x15,R24
	.dbline 112
; } //end 
	mov R24,R20
	swap R24
	andi R24,#0x0F
	andi R24,15
	in R2,0x15
	or R2,R24
	out 0x15,R2
	.dbline 113
; } //end 
	xcall _LCD_en_write
	.dbline 115
; } //end 
; } //end 
	in R24,0x15
	andi R24,240
	out 0x15,R24
	.dbline 116
; } //end 
	mov R24,R20
	andi R24,15
	in R2,0x15
	or R2,R24
	out 0x15,R2
	.dbline 117
; } //end 
	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
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
	.dbline 126
; } //end 
; } //end 
	tst R20
	brne L22
	.dbline 126
	mov R20,R22
	subi R20,128    ; addi 128
	xjmp L23
L22:
	.dbline 127
; } //end 
	mov R20,R22
	subi R20,64    ; addi 192
L23:
	.dbline 128
; } //end 
	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
; } //end 
; } //end 
; } //end 
; } //end 
	.dbline 133
; } //end 
	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
	.dbline 138
L26:
	.dbline 134
; } //end 
	movw R30,R20
	ldd R2,z+0
	tst R2
	brne L25
	.dbline -2
L24:
	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_write_char _LCD_write_char fV
;           data -> y+4
;              Y -> R22
;              X -> R20
	.even
_LCD_write_char::
	xcall push_gset2
	mov R22,R18
	mov R20,R16
	.dbline -1
	.dbline 143
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
; } //end 
	.dbline 144
; } //end 
	mov R18,R22
	mov R16,R20
	xcall _LCD_set_xy
	.dbline 145
; } //end 
	ldd R16,y+4

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -