📄 main.s
字号:
sts _RESID+1,R24
.dbline 157
mov R2,R24
add R2,R16
sts _RESID+1,R2
.dbline 158
lds R16,_RESID
.dbline 159
lsl R16
lsl R16
lsl R16
lsl R16
lsl R16
.dbline 160
add R2,R16
sts _RESID+1,R2
.dbline 161
lds R2,_RESID
lsr R2
lsr R2
lsr R2
sts _RESID,R2
.dbline 162
L53:
.dbline -2
L50:
.dbline 0 ; func end
ret
.dbsym r k 16 c
.dbend
.dbfunc e usart_init _usart_init fV
.even
_usart_init::
xcall push_gset2
.dbline -1
.dbline 167
; else
; {//扩展帧处理方法,RESID[3]、RESID[4]不变
; k=k>>5;
; k=k<<2;
; RESID[1]=RESID[1]&0x03;
; RESID[1]=RESID[1]+k;
; k=RESID[0];
; k=k<<5;
; RESID[1]=RESID[1]+k;
; RESID[0]=RESID[0]>>3;//将RXB0SIDH,RXB0SIDL转化
; }
; }
; //##############################################################################
; //##############################USART初始化程序###########################//
; void usart_init(void)
; {
.dbline 168
; UCSRB=(1<<RXEN)|(1<<TXEN);//允许发送和接收
ldi R24,24
out 0xa,R24
.dbline 169
; UBRRL=(fosc/16/(bps+1))%256;
lds R24,_bps
lds R25,_bps+1
adiw R24,1
movw R2,R24
clr R4
clr R5
ldi R20,0
ldi R21,8
ldi R22,7
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall div32s
ldi R20,0
ldi R21,1
ldi R22,0
ldi R23,0
st -y,R23
st -y,R22
st -y,R21
st -y,R20
xcall mod32s
out 0x9,R16
.dbline 170
; UBRRH=(fosc/16/(bps+1))/256;
lds R24,_bps
lds R25,_bps+1
adiw R24,1
movw R2,R24
clr R4
clr R5
ldi R20,0
ldi R21,8
ldi R22,7
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall div32s
ldi R20,0
ldi R21,1
ldi R22,0
ldi R23,0
st -y,R23
st -y,R22
st -y,R21
st -y,R20
xcall div32s
out 0x20,R16
.dbline 171
; UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP位
ldi R24,134
out 0x20,R24
.dbline 172
; bytetime=215983/bps;//215983=1000000*20/92.6,92.6=1024/11.0592,定时器分频为1024
lds R2,_bps
lds R3,_bps+1
clr R4
clr R5
ldi R20,175
ldi R21,75
ldi R22,3
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall div32s
sts _bytetime,R16
.dbline 173
; bytetime=255-bytetime;//计算不同的波特率所要延时的初值,要附值给TCNT0,实现透明转换中用到
mov R2,R16
clr R3
ldi R24,255
ldi R25,0
sub R24,R2
sbc R25,R3
sts _bytetime,R24
.dbline -2
L64:
xcall pop_gset2
.dbline 0 ; func end
ret
.dbend
.dbfunc e putchar _putchar fV
; c -> R16
.even
_putchar::
.dbline -1
.dbline 178
L66:
.dbline 178
L67:
.dbline 178
; }
;
; //##########################串行通信##########################//
; void putchar(uchar c)
; {while(!(UCSRA&(1<<UDRE)));//判断上次发送有没有完成
sbis 0xb,5
rjmp L66
.dbline 179
; UDR=c;}
out 0xc,R16
.dbline -2
L65:
.dbline 0 ; func end
ret
.dbsym r c 16 c
.dbend
.dbfunc e INT1_17 _INT1_17 fV
; j -> <dead>
; k -> R20
; R0 -> R22
; i -> R20
.even
_INT1_17::
xcall push_lset
xcall push_gset2
.dbline -1
.dbline 182
; //##############################中断服务程序##################################//
; void INT1_17(void)
; {
.dbline 183
; uchar i,j,k,R0=0x66;
ldi R22,102
.dbline 184
; GICR &=0x7f;
in R24,0x3b
andi R24,127
out 0x3b,R24
.dbline 185
; state=Read_state(0xa0);
ldi R16,160
xcall _Read_state
sts _state,R16
.dbline 187
; //WriteSPI(0xa0);//读取2515状态指令,状态值附值给state
; if((state&0x01)!=0)
sbrs R16,0
rjmp L70
.dbline 188
; {//判断是不是接收缓冲器0满产生的中断
.dbline 189
; k=Read_Byte(RXB0SIDL);//为了判断是标准帧还是扩展帧
ldi R16,98
xcall _Read_Byte
mov R20,R16
.dbline 190
; if((k&0x08)==0)eflag=0;//置标准帧标志
sbrc R16,3
rjmp L72
.dbline 190
clr R2
sts _eflag,R2
xjmp L73
L72:
.dbline 191
; else eflag=1;//置扩展帧标志
ldi R24,1
sts _eflag,R24
L73:
.dbline 192
; DLC=Read_Byte(RXB0DLC);
ldi R16,101
xcall _Read_Byte
sts _DLC,R16
.dbline 193
; DLC &=0x0f;
mov R24,R16
andi R24,15
sts _DLC,R24
.dbline 194
; RESID[0]=Read_Byte(RXB0SIDH);
ldi R16,97
xcall _Read_Byte
sts _RESID,R16
.dbline 195
; RESID[1]=Read_Byte(RXB0SIDL);
ldi R16,98
xcall _Read_Byte
sts _RESID+1,R16
.dbline 196
; RESID[2]=Read_Byte(RXB0EID8);
ldi R16,99
xcall _Read_Byte
sts _RESID+2,R16
.dbline 197
; RESID[3]=Read_Byte(RXB0EID0);//先读取所有ID
ldi R16,100
xcall _Read_Byte
sts _RESID+3,R16
.dbline 198
; Receive_all_ID_process();//处理RESID
xcall _Receive_all_ID_process
.dbline 199
clr R20
xjmp L80
L77:
.dbline 199
mov R16,R22
xcall _Read_Byte
ldi R24,<_Rdate
ldi R25,>_Rdate
mov R30,R20
clr R31
add R30,R24
adc R31,R25
std z+0,R16
L78:
.dbline 199
inc R20
inc R22
L80:
.dbline 199
; for(i=0;i<DLC;i++,R0++)Rdate[i]=Read_Byte(R0);//从2515缓冲区内读取收到的数据部分
lds R2,_DLC
cp R20,R2
brlo L77
.dbline 200
; if(eflag==0)
lds R2,_eflag
tst R2
brne L81
.dbline 201
; {//如果是标准帧
.dbline 202
; putchar(RESID[0]);
lds R16,_RESID
xcall _putchar
.dbline 203
; putchar(RESID[1]);//先送标准帧ID号
lds R16,_RESID+1
xcall _putchar
.dbline 204
; }
xjmp L82
L81:
.dbline 206
; else
; {//如果是扩展帧
.dbline 207
; putchar(RESID[0]);
lds R16,_RESID
xcall _putchar
.dbline 208
; putchar(RESID[1]);
lds R16,_RESID+1
xcall _putchar
.dbline 209
; putchar(RESID[2]);
lds R16,_RESID+2
xcall _putchar
.dbline 210
; putchar(RESID[3]);//先送扩展帧ID号
lds R16,_RESID+3
xcall _putchar
.dbline 211
; }
L82:
.dbline 212
; putchar(DLC);
lds R16,_DLC
xcall _putchar
.dbline 213
clr R20
xjmp L90
L87:
.dbline 213
ldi R24,<_Rdate
ldi R25,>_Rdate
mov R30,R20
clr R31
add R30,R24
adc R31,R25
ldd R16,z+0
xcall _putchar
L88:
.dbline 213
inc R20
L90:
.dbline 213
; for(i=0;i<DLC;i++)putchar(Rdate[i]);//再送接收到的DLC位数
lds R2,_DLC
cp R20,R2
brlo L87
.dbline 214
; }
L70:
.dbline 215
; Write_Byte(CANINTF,0x00);//接收完一次必须对中断标志位清0
clr R18
ldi R16,44
xcall _Write_Byte
.dbline 216
; GICR |=0x80;
in R24,0x3b
ori R24,128
out 0x3b,R24
.dbline -2
L69:
xcall pop_gset2
xcall pop_lset
.dbline 0 ; func end
reti
.dbsym l j 1 c
.dbsym r k 20 c
.dbsym r R0 22 c
.dbsym r i 20 c
.dbend
.dbfunc e Usart_receive _Usart_receive fV
.even
_Usart_receive::
st -y,R2
st -y,R24
st -y,R25
st -y,R30
st -y,R31
in R2,0x3f
st -y,R2
.dbline -1
.dbline 220
; }
; //####################################
; void Usart_receive(void)//定义接收中断服务程序
; {Tdate[usart_number]=UDR;
.dbline 220
ldi R24,<_Tdate
ldi R25,>_Tdate
lds R30,_usart_number
lds R31,_usart_number+1
add R30,R24
adc R31,R25
in R2,0xc
std z+0,R2
.dbline 221
; usart_number++;
lds R24,_usart_number
lds R25,_usart_number+1
adiw R24,1
sts _usart_number+1,R25
sts _usart_number,R24
.dbline 222
; TIMSK=0x05;//打开T1,T0中断屏蔽
ldi R24,5
out 0x39,R24
.dbline 223
; TCCR0=0x05;//设T0分频数为1024
out 0x33,R24
.dbline 224
; TCNT0=bytetime;//设T0时间常数,从此值开始计数
lds R2,_bytetime
out 0x32,R2
.dbline -2
L91:
ld R2,y+
out 0x3f,R2
ld R31,y+
ld R30,y+
ld R25,y+
ld R24,y+
ld R2,y+
.dbline 0 ; func end
reti
.dbend
.dbfunc e Timer0 _Timer0 fV
.even
_Timer0::
xcall push_lset
.dbline -1
.dbline 228
; }
; //##############定时器中断0,串行数据流控制
; void Timer0(void)
; {TIMSK=0x00;//关T0中断屏蔽
.dbline 228
clr R2
out 0x39,R2
.dbline 229
; TCNT0=0x00;//
out 0x32,R2
.dbline 230
; TCCR0=0x00;//T0停止计数
out 0x33,R2
.dbline 231
; if((usart_number>2)&&(usart_number<11))
ldi R24,2
ldi R25,0
lds R2,_usart_number
lds R3,_usart_number+1
cp R24,R2
cpc R25,R3
brsh L93
movw R24,R2
cpi R24,11
ldi R30,0
cpc R25,R30
brsh L93
.dbline 232
; {
.dbline 233
; TID[0]=Tdate[0];TID[1]=Tdate[1];
lds R2,_Tdate
sts _TID,R2
.dbline 233
lds R2,_Tdate+1
sts _TID+1,R2
.dbline 234
; load_Standard_ID_dates(usart_number-2);//对2515发送缓冲器0和ID寄存器进行数据装载
mov R16,R24
subi R16,2
sbci R17,0
xcall _load_Standard_ID_dates
.dbline 235
; usart_number=0;
clr R2
clr R3
sts _usart_number+1,R3
sts _usart_number,R2
.dbline 236
; Write_Byte(CANCTRL,0x00);//选定工作模式
clr R18
ldi R16,15
xcall _Write_Byte
.dbline 237
; WriteSPI(CAN_RTS_TXB0);//发送缓冲器0请求发送
ldi R16,129
xcall _WriteSPI
.dbline 238
; }
xjmp L94
L93:
.dbline 239
clr R2
clr R3
sts _usart_number+1,R3
sts _usart_number,R2
L94:
.dbline -2
L92:
xcall pop_lset
.dbline 0 ; func end
reti
.dbend
.dbfunc e CAN_Initialize _CAN_Initialize fV
.even
_CAN_Initialize::
.dbline -1
.dbline 243
; else usart_number=0;
; }
; //##############################CAN(2515)初始化程序###########################//
; void CAN_Initialize(void)
; {
.dbline 244
; WriteSPI(CAN_RESET);
ldi R16,192
xcall _WriteSPI
.dbline 245
; delay(20);
ldi R16,20
xcall _delay
.dbline 246
; Write_Byte(CANCTRL,0x80);//CAN工作在配置模式
ldi R18,128
ldi R16,15
xcall _Write_Byte
.dbline 247
; Write_Byte(CNF1,can_boud);
lds R18,_can_boud
ldi R16,42
xcall _Write_Byte
.dbline 248
; Write_Byte(CNF2,0x80 | PHSEG1_3TQ | PRSEG_1TQ);//Set CNF2
ldi R18,144
ldi R16,41
xcall _Write_Byte
.dbline 249
; Write_Byte(CNF3,PHSEG2_3TQ);
ldi R18,2
ldi R16,40
xcall _Write_Byte
.dbline 251
; //0x80+0x10+0x00,相位缓冲段2由CNF3确定,相位缓冲段1为3TQ,传播段为1TQ
; Write_Byte(RXB0CTRL,0xf0);//接收类型选择,接收所有报文
ldi R18,240
ldi R16,96
xcall _Write_Byte
.dbline 252
; Write_Byte(CANINTF,0x00);//接收完一次必须对中断标志位清0
clr R18
ldi R16,44
xcall _Write_Byte
.dbline 253
; Write_Byte(CANINTE,0x01);//接收缓冲器0满中断使能
ldi R18,1
ldi R16,43
xcall _Write_Byte
.dbline 254
; Write_Byte(CANCTRL,0x00);//选定正常工作模式
clr R18
ldi R16,15
xcall _Write_Byte
.dbline -2
L97:
.dbline 0 ; func end
ret
.dbend
.dbfunc e AVR_Initialize _AVR_Initialize fV
.even
_AVR_Initialize::
.dbline -1
.dbline 258
; }
; //##############################系统初始化程序################################//
; void AVR_Initialize(void) //初始化
; {
.dbline 259
; DDRB=0xff; //SPI口
ldi R24,255
out 0x17,R24
.dbline 260
; PORTB=0xff;
out 0x18,R24
.dbline 261
; DDRD=0xf0;//将外部中断引脚设定为输入
ldi R24,240
out 0x11,R24
.dbline 262
; PORTD=0x7f;
ldi R24,127
out 0x12,R24
.dbline 263
; SPCR=0b01011100;//关中断(SPIE=0),使能SPI(SPE=1),MSB首先发送(DORD=0)
ldi R24,92
out 0xd,R24
.dbline 268
; //选择微机为主机模式(MSTR=1)
; //空闲时SCK 为高电平(CPOL=1)
; //在SCK 的结束沿采样(CPHA=1)以保证数据稳定
; //SPR1=0和SPR0=0,SCK=fosc/4
; SPSR |=0x01;//SPI的速度加倍
sbi 0xe,0
.dbline 269
; MCUCR=0x0a;//设置外部中断的中断触发方式为下降沿触发
ldi R24,10
out 0x35,R24
.dbline 270
; GICR=0xc0;//通用中断控制寄存器设置,打开中断INT0和INT1
ldi R24,192
out 0x3b,R24
.dbline 271
; SREG=0x80;//开全局中断
ldi R24,128
out 0x3f,R24
.dbline -2
L98:
.dbline 0 ; func end
ret
.dbend
.dbfunc e main _main fV
.even
_main::
.dbline -1
.dbline 275
; }
; //##################################主程序####################################//
; void main()
; {
.dbline 276
; AVR_Initialize(); //I/O口及中断初始化
xcall _AVR_Initialize
.dbline 277
; CAN_Initialize();
xcall _CAN_Initialize
.dbline 278
; usart_init();
xcall _usart_init
.dbline 279
; UCSRB |=0x80;//接收中断使能
sbi 0xa,7
.dbline -2
L99:
.dbline 0 ; func end
ret
.dbend
.area bss(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
_state::
.blkb 1
.dbsym e state _state c
_bytetime::
.blkb 1
.dbsym e bytetime _bytetime c
_RESID::
.blkb 4
.dbsym e RESID _RESID A[4:4]c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -