📄 main.s
字号:
.module main.c
.area vector(rom, abs)
.org 8
jmp _INT1_17
.org 36
jmp _Timer0
.org 44
jmp _Usart_receive
.area data(ram, con, rel)
_can_boud::
.blkb 1
.area idata
.byte 7
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e can_boud _can_boud c
_bps::
.blkb 2
.area idata
.word 38400
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e bps _bps i
_eflag::
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e eflag _eflag c
_Tdate::
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.blkb 9
.area idata
.byte 0,0,0,0,0,0,0,0,0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e Tdate _Tdate A[10:10]c
_TID::
.blkb 2
.area idata
.byte 0,0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e TID _TID A[2:2]c
_Rdate::
.blkb 1
.area idata
.byte 0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.blkb 7
.area idata
.byte 0,0,0,0,0,0,0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e Rdate _Rdate A[8:8]c
_usart_number::
.blkb 2
.area idata
.word 0
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e usart_number _usart_number i
_DLC::
.blkb 1
.area idata
.byte 8
.area data(ram, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbsym e DLC _DLC c
.area text(rom, con, rel)
.dbfile F:\小制作\can,usb,以太网学习板\232转CAN学习程序\main.c
.dbfunc e delay _delay fV
; i -> R20,R21
; k -> R16
.even
_delay::
xcall push_gset1
.dbline -1
.dbline 39
; /*##############################################################################
; 功能:CAN与232相互转换
; 作者:LHF
; 时间:2007-01
; 版本:V1.0
; ################################################################################
; 资源分配: PORTC.2~5:JTAG
; ##############################################################################*/
; //##############################################################################
; #include <iom16v.h> //寄存器定义;
; #include <macros.h> //宏定义;
; #include "MCP2515.h" //2515寄存器定义
; #define uchar unsigned char //数据类型定义
; #define uint unsigned int //数据类型定义
;
; #pragma interrupt_handler INT1_17:3 //定义INT1的中断服务程序
; #pragma interrupt_handler Timer0:10 //定义定时计数器0的中断服务程序
; #pragma interrupt_handler Usart_receive:12 //定义接收中断服务程序
; #define fosc 7372800 //晶振7.3728MHZ
; uchar can_boud=0x07;//MCP2515在16M晶振情况,can_boud=0x00总线波特率为1M,0x01=500K,0x03=250K,0x07=125K;公式:16M/(16*(1+X))
; uint bps=38400;//定义串口波特率
;
; uchar eflag=0;//是不是扩展帧,为1则表示接收到的是扩展帧,0表示标准帧
; uchar Tdate[10]={0};//存放要发送标准帧的数据,最大10位,前2位是ID号,后8位是数据位
; uchar TID[2]={0,0};//存放要发送标准帧的ID号
;
; uchar Rdate[8]={0};//存放接收到的数据
; uchar RESID[4];//存放接收到的数据帧的ID号,标准帧只用到RESID[0],RESID[1],扩展帧全部用到
;
; uchar bytetime;//232与CAN透明转换时,根据不同的波特率确定所要延时的时间,
; uint usart_number=0;//计数当前所接收的串行数据的那一组数据流的数据个数,串口传来的数据存放在Tdate中等待用CAN标准帧发送
;
; uchar state;//2515状态(包括发送接收中断标志位和各请求发送位),具体见数据手册
; uchar DLC=8;//接收到数据的长度
;
; //****************************************************************************//
; //********************************延时程序************************************//
; void delay(uchar k)//
; {uint i=0;
.dbline 39
clr R20
clr R21
xjmp L3
L2:
.dbline 40
.dbline 40
clr R20
clr R21
xjmp L8
L5:
.dbline 40
L6:
.dbline 40
subi R20,255 ; offset = 1
sbci R21,255
L8:
.dbline 40
cpi R20,64
ldi R30,31
cpc R21,R30
brlo L5
.dbline 40
L3:
.dbline 40
; while(k--){for(i=0;i<8000;i++);}
mov R2,R16
clr R3
subi R16,1
tst R2
brne L2
.dbline -2
L1:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r i 20 i
.dbsym r k 16 c
.dbend
.dbfunc e Set_CS _Set_CS fV
; level -> R16
.even
_Set_CS::
.dbline -1
.dbline 44
; }
; //##############################################################################
; void Set_CS(uchar level) //
; {if(level) PORTB|=0x10; //
.dbline 44
tst R16
breq L10
.dbline 44
sbi 0x18,4
xjmp L11
L10:
.dbline 45
in R24,0x18
andi R24,239
out 0x18,R24
L11:
.dbline -2
L9:
.dbline 0 ; func end
ret
.dbsym r level 16 c
.dbend
.dbfunc e WriteSPI _WriteSPI fV
; clear -> R20
; order -> R20
.even
_WriteSPI::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 51
; else PORTB&=0xef; //
; }
; //##############################################################################
; //********************************SPI对2515访问*******************************//
; //****************WriteSPI()*******************//
; void WriteSPI(uchar order)
; { uchar clear;
.dbline 52
; Set_CS(0); //
clr R16
xcall _Set_CS
.dbline 53
; SPDR=order; //2515读指令为0x03
out 0xf,R20
L13:
.dbline 54
L14:
.dbline 54
; while(!(SPSR&0x80)); //等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L13
.dbline 55
; clear=SPSR;
in R20,0xe
.dbline 56
; clear=SPDR;//
in R20,0xf
.dbline 57
; Set_CS(1); //
ldi R16,1
xcall _Set_CS
.dbline -2
L12:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r clear 20 c
.dbsym r order 20 c
.dbend
.dbfunc e Read_state _Read_state fc
; clear -> R20
; order -> R20
.even
_Read_state::
xcall push_gset1
mov R20,R16
.dbline -1
.dbline 62
; }
;
; //****************Read_state()*******************//
; uchar Read_state(uchar order)//读状态命令,order=0xa0,0xa1,
; { uchar clear;
.dbline 63
; Set_CS(0); //
clr R16
xcall _Set_CS
.dbline 64
; SPDR=order; //
out 0xf,R20
L17:
.dbline 65
L18:
.dbline 65
; while(!(SPSR&0x80)); //等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L17
.dbline 66
; clear=SPSR;
in R20,0xe
.dbline 67
; clear=SPDR;//
in R20,0xf
.dbline 69
;
; SPDR=0; //空数据
clr R2
out 0xf,R2
L20:
.dbline 70
L21:
.dbline 70
; while(!(SPSR&0x80));//等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L20
.dbline 71
; clear=SPSR;
in R20,0xe
.dbline 72
; clear=SPDR; //通过先读SPSR,紧接着访问SPDR来对SPIF清零
in R20,0xf
.dbline 74
;
; Set_CS(1); //
ldi R16,1
xcall _Set_CS
.dbline 75
; return clear;
mov R16,R20
.dbline -2
L16:
xcall pop_gset1
.dbline 0 ; func end
ret
.dbsym r clear 20 c
.dbsym r order 20 c
.dbend
.dbfunc e Read_Byte _Read_Byte fc
; date -> R20
; clear -> R22
; Address -> R10
.even
_Read_Byte::
xcall push_gset3
mov R10,R16
.dbline -1
.dbline 79
; }
; //**从2515指定地址Address读取一个字节数据Data**//
; uchar Read_Byte(uchar Address)
; {uchar clear;
.dbline 81
; uchar date;
; Set_CS(0); //使能SPI器件
clr R16
xcall _Set_CS
.dbline 83
;
; SPDR=0x03; //送2515读指令为0x03
ldi R24,3
out 0xf,R24
L24:
.dbline 84
L25:
.dbline 84
; while(!(SPSR&0x80)); //等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L24
.dbline 85
; clear=SPSR;
in R22,0xe
.dbline 86
; clear=SPDR; //通过先读SPSR,紧接着访问SPDR来对SPIF清零
in R22,0xf
.dbline 88
;
; SPDR=Address; //送地址
out 0xf,R10
L27:
.dbline 89
L28:
.dbline 89
; while(!(SPSR&0x80));
sbis 0xe,7
rjmp L27
.dbline 90
; clear=SPSR;
in R22,0xe
.dbline 91
; clear=SPDR;
in R22,0xf
.dbline 93
;
; SPDR=0x00; //发空数据,启动数据发送以接收数据
clr R2
out 0xf,R2
L30:
.dbline 94
L31:
.dbline 94
; while(!(SPSR&0x80));
sbis 0xe,7
rjmp L30
.dbline 95
; clear=SPSR;
in R22,0xe
.dbline 96
; clear=SPDR;
in R22,0xf
.dbline 98
;
; date=SPDR; //接收数据
in R20,0xf
.dbline 99
; Set_CS(1); //关SPI器件DS1722
ldi R16,1
xcall _Set_CS
.dbline 100
; return date;
mov R16,R20
.dbline -2
L23:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r date 20 c
.dbsym r clear 22 c
.dbsym r Address 10 c
.dbend
.dbfunc e Write_Byte _Write_Byte fV
; clear -> R20
; Data -> R22
; Address -> R10
.even
_Write_Byte::
xcall push_gset3
mov R22,R18
mov R10,R16
.dbline -1
.dbline 104
; }
; //**向2515指定地址Address写一个字节数据Data**//
; void Write_Byte(uchar Address,uchar Data)
; {uchar clear;
.dbline 105
; Set_CS(0); //使能SPI器件2515
clr R16
xcall _Set_CS
.dbline 107
;
; SPDR=0x02; //送2515写命令为0x02
ldi R24,2
out 0xf,R24
L34:
.dbline 108
L35:
.dbline 108
; while(!(SPSR&0x80));//等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L34
.dbline 109
; clear=SPSR;
in R20,0xe
.dbline 110
; clear=SPDR;
in R20,0xf
.dbline 112
;
; SPDR=Address; //送地址,启动SPI时钟
out 0xf,R10
L37:
.dbline 113
L38:
.dbline 113
; while(!(SPSR&0x80));//等待SPIF置位,等数据发送完毕
sbis 0xe,7
rjmp L37
.dbline 114
; clear=SPSR;
in R20,0xe
.dbline 115
; clear=SPDR; //通过先读SPSR,紧接着访问SPDR来对SPIF清零
in R20,0xf
.dbline 117
;
; SPDR=Data;
out 0xf,R22
L40:
.dbline 118
L41:
.dbline 118
; while(!(SPSR&0x80));
sbis 0xe,7
rjmp L40
.dbline 119
; clear=SPSR;
in R20,0xe
.dbline 120
; clear=SPDR;
in R20,0xf
.dbline 121
; Set_CS(1); //关SPI器件
ldi R16,1
xcall _Set_CS
.dbline -2
L33:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r clear 20 c
.dbsym r Data 22 c
.dbsym r Address 10 c
.dbend
.dbfunc e load_Standard_ID_dates _load_Standard_ID_dates fV
; TIDL -> R12
; TIDH -> R14
; j -> R20
; T0 -> R22
; i -> R10
; num -> y+10
.even
_load_Standard_ID_dates::
st -y,r17
st -y,r16
xcall push_gset5
.dbline -1
.dbline 128
; }
; //****************************************************************************//
; //###########################对CAN的一些操作####################################
; //***************************要发送的数据包***********************************//
; void load_Standard_ID_dates(uchar num)//给标准帧装载ID和数据
; //选用发送缓冲器0,num:要发送的个数(最大8个)
; {
.dbline 129
; uchar i,j,T0=0x36;
ldi R22,54
.dbline 131
; uchar TIDH,TIDL;
; i=TID[1]>>3;j=TID[0]<<5;j=j+i;i=TID[1]<<5;
lds R10,_TID+1
lsr R10
lsr R10
lsr R10
.dbline 131
lds R20,_TID
lsl R20
lsl R20
lsl R20
lsl R20
lsl R20
.dbline 131
add R20,R10
.dbline 131
lds R10,_TID+1
lsl R10
lsl R10
lsl R10
lsl R10
lsl R10
.dbline 132
; TIDL=i;TIDH=j;//将数组TID中的值转化为TIDH,TIDL以便给TXB0SIDH,TXB0SIDL附值
mov R12,R10
.dbline 132
mov R14,R20
.dbline 133
; Write_Byte(CANCTRL,0x80);//CAN工作在配置模式
ldi R18,128
ldi R16,15
xcall _Write_Byte
.dbline 134
; Write_Byte(CNF1,can_boud);
lds R18,_can_boud
ldi R16,42
xcall _Write_Byte
.dbline 135
; Write_Byte(TXB0SIDH,TIDH);
mov R18,R14
ldi R16,49
xcall _Write_Byte
.dbline 136
; Write_Byte(TXB0SIDL,TIDL);
mov R18,R12
ldi R16,50
xcall _Write_Byte
.dbline 137
; Write_Byte(TXB0DLC,num);//
ldd R18,y+10
ldi R16,53
xcall _Write_Byte
.dbline 138
ldi R24,2
mov R10,R24
xjmp L49
L46:
.dbline 138
ldi R24,<_Tdate
ldi R25,>_Tdate
mov R30,R10
clr R31
add R30,R24
adc R31,R25
ldd R18,z+0
mov R16,R22
xcall _Write_Byte
L47:
.dbline 138
inc R10
inc R22
L49:
.dbline 138
; for(i=2;i<num+2;i++,T0++)Write_Byte(T0,Tdate[i]);
ldd R24,y+10
subi R24,254 ; addi 2
cp R10,R24
brlo L46
.dbline -2
L43:
xcall pop_gset5
adiw R28,2
.dbline 0 ; func end
ret
.dbsym r TIDL 12 c
.dbsym r TIDH 14 c
.dbsym r j 20 c
.dbsym r T0 22 c
.dbsym r i 10 c
.dbsym l num 10 c
.dbend
.dbfunc e Receive_all_ID_process _Receive_all_ID_process fV
; k -> R16
.even
_Receive_all_ID_process::
.dbline -1
.dbline 142
; }
; //##############################################################################
; void Receive_all_ID_process(void)//结合MCP2515数据手册来看
; {uchar k;
.dbline 143
; k=RESID[1];//先判断接收到的是不是标准帧,因为标准帧和扩展帧的ID处理不一样
lds R16,_RESID+1
.dbline 144
; if((k&0x08)==0)
sbrc R16,3
rjmp L52
.dbline 145
; {//如果是标准处理方法
.dbline 146
; RESID[1]=RESID[1]>>5;
mov R2,R16
lsr R2
lsr R2
lsr R2
lsr R2
lsr R2
sts _RESID+1,R2
.dbline 147
; k=RESID[0]&0x1f;//借用k
lds R16,_RESID
andi R16,31
.dbline 148
; k=k<<3;
lsl R16
lsl R16
lsl R16
.dbline 149
; RESID[1]=RESID[1]+k;
add R2,R16
sts _RESID+1,R2
.dbline 150
; RESID[0]=RESID[0]>>5;
lds R2,_RESID
lsr R2
lsr R2
lsr R2
lsr R2
lsr R2
sts _RESID,R2
.dbline 151
; }
xjmp L53
L52:
.dbline 153
.dbline 154
lsr R16
lsr R16
lsr R16
lsr R16
lsr R16
.dbline 155
lsl R16
lsl R16
.dbline 156
lds R24,_RESID+1
andi R24,3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -