📄 mock_uart.s
字号:
.module Mock_Uart.C
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
.dbfunc e Timer0_init _Timer0_init fV
.even
_Timer0_init::
.dbline -1
.dbline 17
;
; //模拟串口通讯
; #include"include.h"
;
; #define TX_LO() PORTD &= ~(1<<4) //发送端口
; #define TX_HI() PORTD |= (1<<4)
;
; #define RX_Port() (PIND&(1<<2)) //接收端口
;
; unsigned char F_TM;
;
; //TIMER0 initialize - prescale:8
; // WGM: Normal
; // desired value: 104uSec
; // actual value: 104.000uSec (0.0%)
; void Timer0_init(void)
; {
.dbline 18
; TCCR0 = 0x00; //stop
clr R2
out 0x33,R2
.dbline 19
; TCNT0 = 0x30; //set count
ldi R24,48
out 0x32,R24
.dbline 20
; OCR0 = 0xD0; //set compare
ldi R24,208
out 0x3c,R24
.dbline -2
L2:
.dbline 0 ; func end
ret
.dbend
.area vector(rom, abs)
.org 36
jmp _Timer0_ovf_isr
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
.dbfunc e Timer0_ovf_isr _Timer0_ovf_isr fV
.even
_Timer0_ovf_isr::
st -y,R24
in R24,0x3f
st -y,R24
.dbline -1
.dbline 25
; }
;
; #pragma interrupt_handler Timer0_ovf_isr:10
; void Timer0_ovf_isr(void)
; {
.dbline 26
; TCNT0 = 0x30; //reload counter value
ldi R24,48
out 0x32,R24
.dbline 27
; F_TM = 1;
ldi R24,1
sts _F_TM,R24
.dbline -2
L3:
.dbline 0 ; func end
ld R24,y+
out 0x3f,R24
ld R24,y+
reti
.dbend
.dbfunc e PSendChar _PSendChar fV
; ii -> R20
; inch -> R16
.even
_PSendChar::
st -y,R20
.dbline -1
.dbline 33
; }
;
; //------------------------------------------------------------------------------
; //发送一个字符
; void PSendChar(unsigned char inch)
; {
.dbline 35
; unsigned char ii;
; ii=0;
clr R20
.dbline 36
; F_TM=0;
clr R2
sts _F_TM,R2
.dbline 37
; TX_LO(); //start bit
cbi 0x12,4
.dbline 38
; TCCR0 = 0x02; //start timer
ldi R24,2
out 0x33,R24
L5:
.dbline 39
; while(!F_TM);
L6:
.dbline 39
lds R2,_F_TM
tst R2
breq L5
X0:
xjmp L9
L8:
.dbline 41
; while(ii<8)
; {
.dbline 42
; if(inch&1)
sbrs R16,0
rjmp L11
X1:
.dbline 43
; {
.dbline 44
; TX_HI();
sbi 0x12,4
.dbline 45
; }
xjmp L12
L11:
.dbline 47
; else
; {
.dbline 48
; TX_LO();
cbi 0x12,4
.dbline 49
; }
L12:
.dbline 50
; F_TM=0;
clr R2
sts _F_TM,R2
L13:
.dbline 51
; while(!F_TM);
L14:
.dbline 51
lds R2,_F_TM
tst R2
breq L13
X2:
.dbline 52
; ii++;
inc R20
.dbline 53
; inch>>=1;
lsr R16
.dbline 54
; }
L9:
.dbline 40
cpi R20,8
brlo L8
X3:
.dbline 55
; TX_HI();
sbi 0x12,4
.dbline 56
; F_TM=0;
clr R2
sts _F_TM,R2
L16:
.dbline 57
; while(!F_TM);
L17:
.dbline 57
lds R2,_F_TM
tst R2
breq L16
X4:
.dbline 58
; TCCR0 = 0x00; //stop//停止timer
clr R2
out 0x33,R2
.dbline -2
L4:
.dbline 0 ; func end
ld R20,y+
ret
.dbsym r ii 20 c
.dbsym r inch 16 c
.dbend
.dbfunc e PGetChar _PGetChar fc
; rch -> R16
; ii -> R18
.even
_PGetChar::
.dbline -1
.dbline 63
; }
; //------------------------------------------------------------------------------
; //接收一个字符
; unsigned char PGetChar()
; {
.dbline 65
; unsigned char ii,rch;
; TCCR0 = 0x02; //start timer
ldi R24,2
out 0x33,R24
.dbline 66
; F_TM=0;
clr R2
sts _F_TM,R2
.dbline 67
; ii=0;
clr R18
.dbline 68
; rch=0;
clr R16
L20:
.dbline 69
; while(!F_TM); //等过起始位
L21:
.dbline 69
lds R2,_F_TM
tst R2
breq L20
X5:
xjmp L24
L23:
.dbline 71
; while(ii<8)
; {
.dbline 72
; rch>>=1;
lsr R16
.dbline 73
; if(RX_Port())
sbis 0x10,2
rjmp L26
X6:
.dbline 74
; {
.dbline 75
; rch|=0x80;
ori R16,128
.dbline 76
; }
L26:
.dbline 77
; ii++;
inc R18
.dbline 78
; F_TM=0;
clr R2
sts _F_TM,R2
L28:
.dbline 79
; while(!F_TM);
L29:
.dbline 79
lds R2,_F_TM
tst R2
breq L28
X7:
.dbline 80
; }
L24:
.dbline 70
cpi R18,8
brlo L23
X8:
.dbline 81
; F_TM=0;
clr R2
sts _F_TM,R2
xjmp L32
L31:
.dbline 83
; while(!F_TM)
; {
.dbline 84
; if(RX_Port())
sbis 0x10,2
rjmp L34
X9:
.dbline 85
; {
.dbline 86
; break;
xjmp L33
L34:
.dbline 88
; }
; }
L32:
.dbline 82
lds R2,_F_TM
tst R2
breq L31
X10:
L33:
.dbline 89
; TCCR0 = 0x00; //stop//停止timer
clr R2
out 0x33,R2
.dbline 90
; return rch;
.dbline -2
L19:
.dbline 0 ; func end
ret
.dbsym r rch 16 c
.dbsym r ii 18 c
.dbend
.area bss(ram, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
L37:
.blkb 2
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
.dbfunc e BUS_SendPacket_51Device _BUS_SendPacket_51Device fV
.dbstruct 0 260 Device51_StructOfProtocol
.dbfield 0 Address c
.dbfield 1 Length i
.dbfield 3 Command c
.dbfield 4 Data A[256:256]c
.dbend
.dbsym s C51_uart L37 pc
; C51_buf -> R20,R21
; CRC -> y+0
; Count -> R20,R21
; i -> R10
; tmp -> R12
.even
_BUS_SendPacket_51Device::
xcall push_xgset303C
sbiw R28,2
.dbline -1
.dbline 96
; }
; //------------------------------------------------------------------------------
; //------------------------------------------------------------------------------
; //发送数据
; void BUS_SendPacket_51Device(void)
; {
.dbline 101
; unsigned char i,tmp;
; unsigned int CRC,Count;
; static unsigned char *C51_uart;
; struct Device51_StructOfProtocol *C51_buf;
; C51max485_on();
sbi 0x12,6
.dbline 102
; PSendChar(BOOT_CODE);
ldi R16,254
xcall _PSendChar
.dbline 103
; PSendChar(BOOT_CODE);//发送引导码
ldi R16,254
xcall _PSendChar
.dbline 104
; PSendChar(DEVICE_NUMBER);//发送系统设备号
ldi R16,2
xcall _PSendChar
.dbline 105
; C51_buf = (struct Device51_StructOfProtocol *)C51Buffer485;
ldi R20,<_C51Buffer485
ldi R21,>_C51Buffer485
.dbline 106
; Count = C51_buf->Length;
movw R30,R20
ldd R20,z+1
ldd R21,z+2
.dbline 107
; if(Count&&(Count<= 256))
cpi R20,0
cpc R20,R21
breq L38
X11:
ldi R24,256
ldi R25,1
cp R24,R20
cpc R25,R21
brlo L38
X12:
.dbline 108
; {
.dbline 109
; C51_uart = C51Buffer485; /*初始化数据发送指针*/
ldi R24,<_C51Buffer485
ldi R25,>_C51Buffer485
sts L37+1,R25
sts L37,R24
.dbline 110
; CRC = 0xffff; /*初始化校验码*/
ldi R24,65535
ldi R25,255
std y+1,R25
std y+0,R24
.dbline 111
; Count += 3;
subi R20,253 ; offset = 3
sbci R21,255
.dbline 113
;
; for(i=0;i<Count;i++)
clr R10
xjmp L43
L40:
.dbline 114
; {
.dbline 115
; tmp = *C51_uart++; /*取发送数据*/
lds R30,L37
lds R31,L37+1
ld R12,Z+
sts L37+1,R31
sts L37,R30
.dbline 116
; PSendChar(tmp);
mov R16,R12
xcall _PSendChar
.dbline 117
; CRC16(&CRC,tmp);
mov R18,R12
movw R16,R28
xcall _CRC16
.dbline 118
; }
L41:
.dbline 113
inc R10
L43:
.dbline 113
mov R2,R10
clr R3
cp R2,R20
cpc R3,R21
brlo L40
X13:
.dbline 119
; PSendChar(CRC>>8);
ldd R16,y+0
ldd R17,y+1
mov R16,R17
clr R17
xcall _PSendChar
.dbline 120
; PSendChar(CRC);//发送校验码
ldd R16,y+0
xcall _PSendChar
.dbline 121
; }
L38:
.dbline 122
; C51max485_off();
cbi 0x12,6
.dbline 123
; Power_LED_Drown();
cbi 0x11,7
.dbline -2
L36:
.dbline 0 ; func end
adiw R28,2
xjmp pop_xgset303C
.dbsym r C51_buf 20 pS[Device51_StructOfProtocol]
.dbsym l CRC 0 i
.dbsym r Count 20 i
.dbsym r i 10 c
.dbsym r tmp 12 c
.dbend
.area bss(ram, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
L45:
.blkb 2
L46:
.blkb 2
L47:
.blkb 2
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
.dbfunc e BUS_GetPacket_51Device _BUS_GetPacket_51Device fc
.dbstruct 0 256 Device51_Address
.dbfield 0 Address A[256:256]c
.dbend
.dbsym s C51_uart L47 pc
.dbsym s Count L46 i
.dbsym s CRC L45 i
; Device51 -> <dead>
; C51_buf -> R20,R21
; tmp -> R10
.even
_BUS_GetPacket_51Device::
xcall push_xgsetF03C
.dbline -1
.dbline 128
; }
; //------------------------------------------------------------------------------
; //接收数据
; unsigned char BUS_GetPacket_51Device(void)
; {
.dbline 134
; unsigned char tmp;
; static unsigned int CRC,Count;
; static unsigned char *C51_uart;
; struct Device51_StructOfProtocol *C51_buf;
; struct Device51_Address *Device51;
; if(PGetChar()!=BOOT_CODE) return 1;
xcall _PGetChar
cpi R16,254
breq L48
X14:
.dbline 134
ldi R16,1
xjmp L44
L48:
.dbline 135
; if(PGetChar()!=BOOT_CODE) return 1;
xcall _PGetChar
cpi R16,254
breq L50
X15:
.dbline 135
ldi R16,1
xjmp L44
L50:
.dbline 136
; if(PGetChar()!=DEVICE_NUMBER) return 1;
xcall _PGetChar
mov R20,R16
cpi R16,2
breq L52
X16:
.dbline 136
ldi R16,1
xjmp L44
L52:
.dbline 137
; CRC = 0xffff; /*初始化校验码*/
ldi R24,65535
ldi R25,255
sts L45+1,R25
sts L45,R24
.dbline 138
; C51_uart = C51Buffer485; /*初始化数据接收指针*/
ldi R24,<_C51Buffer485
ldi R25,>_C51Buffer485
sts L47+1,R25
sts L47,R24
.dbline 139
; tmp = PGetChar();
xcall _PGetChar
mov R10,R16
.dbline 140
; *C51_uart++ = tmp; /*接收地址*/
lds R30,L47
lds R31,L47+1
st Z+,R10
sts L47+1,R31
sts L47,R30
.dbline 141
; CRC16(&CRC,tmp); /*数据校验*/
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 142
; tmp = PGetChar();
xcall _PGetChar
mov R12,R16
mov R10,R12
.dbline 143
; *C51_uart++ = tmp; /*接收包长度低位字节*/
lds R30,L47
lds R31,L47+1
st Z+,R10
sts L47+1,R31
sts L47,R30
.dbline 144
; CRC16(&CRC,tmp); /*数据校验*/
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 145
; tmp = PGetChar();
xcall _PGetChar
mov R12,R16
mov R10,R12
.dbline 146
; *C51_uart++ = tmp; /*接收包长度高位字节*/
lds R30,L47
lds R31,L47+1
st Z+,R10
sts L47+1,R31
sts L47,R30
.dbline 147
; CRC16(&CRC,tmp); /*数据校验*/
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 148
; C51_buf = (struct Device51_StructOfProtocol *)C51Buffer485;
ldi R20,<_C51Buffer485
ldi R21,>_C51Buffer485
.dbline 149
; Count = C51_buf->Length;
movw R30,R20
ldd R2,z+1
ldd R3,z+2
sts L46+1,R3
sts L46,R2
.dbline 150
; if (Count && Count <= (256 - 8))
movw R12,R2
tst R2
brne X17
tst R3
breq L54
X17:
ldi R24,248
ldi R25,0
cp R24,R2
cpc R25,R3
brlo L54
X18:
.dbline 151
; {
L56:
.dbline 152
; do{
.dbline 153
; *C51_uart++ = tmp; /*接收数据*/
lds R30,L47
lds R31,L47+1
st Z+,R10
sts L47+1,R31
sts L47,R30
.dbline 154
; CRC16(&CRC,tmp); /*数据校验*/
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 155
; }while(Count--);
L57:
.dbline 155
lds R22,L46
lds R23,L46+1
movw R24,R22
sbiw R24,1
sts L46+1,R25
sts L46,R24
cpi R22,0
cpc R22,R23
brne L56
X19:
.dbline 156
; }
xjmp L55
L54:
.dbline 158
; else
; return 1;
ldi R16,1
xjmp L44
L55:
.dbline 159
; CRC16(&CRC,PGetChar()); /*数据校验*/
xcall _PGetChar
mov R10,R16
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 160
; CRC16(&CRC,PGetChar()); /*数据校验*/
xcall _PGetChar
mov R10,R16
mov R18,R10
ldi R16,<L45
ldi R17,>L45
xcall _CRC16
.dbline 161
; C51_buf = (struct Device51_StructOfProtocol *)C51Buffer485;
ldi R20,<_C51Buffer485
ldi R21,>_C51Buffer485
.dbline 162
; if (!CRC)
lds R2,L45
lds R3,L45+1
tst R2
brne L59
tst R3
brne L59
X20:
.dbline 164
; //接收完数据
; return 0;
clr R16
xjmp L44
L59:
.dbline 166
; else
; return 1;
ldi R16,1
.dbline -2
L44:
.dbline 0 ; func end
xjmp pop_xgsetF03C
.dbsym l Device51 1 pS[Device51_Address]
.dbsym r C51_buf 20 pS[Device51_StructOfProtocol]
.dbsym r tmp 10 c
.dbend
.area bss(ram, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\LCU904~1\固件程序\Mock_Uart.C
_F_TM::
.blkb 1
.dbsym e F_TM _F_TM c
; }
; //------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -