📄 spi_test_slave.s
字号:
.module Spi_test_slave.c
.area text(rom, con, rel)
.dbfile F:\IccAvr_Pro\Spi_test_slave.c
.area vector(rom, abs)
.org 68
jmp _spi_stc_isr
.area text(rom, con, rel)
.dbfunc e spi_stc_isr _spi_stc_isr fV
.even
_spi_stc_isr::
st -y,R2
st -y,R24
st -y,R25
st -y,R30
st -y,R31
in R2,0x3f
st -y,R2
.dbline -1
.dbline 16
; #include <iom16v.h>
; #include <macros.h>
; #define SIZE 100
; #define MOSI PORTB5
; #define MISO PINB6
; #define SCK PORTB7
; #define SS PORTB4
;
; unsigned char SPI_rx_buff[SIZE];
; unsigned char SPI_tx_buff[SIZE];
; unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;
; unsigned char tx_wr_index,tx_rd_index,tx_counter,SPI_ok,SPI_datain;
;
; #pragma interrupt_handler spi_stc_isr:18
; void spi_stc_isr(void)
; {
.dbline 17
; SPI_rx_buff[rx_wr_index] = SPDR; //从ISP口读出收到的字节
ldi R24,<_SPI_rx_buff
ldi R25,>_SPI_rx_buff
lds R30,_rx_wr_index
clr R31
add R30,R24
adc R31,R25
in R2,0xf
std z+0,R2
.dbline 18
; SPI_ok=1;
ldi R24,1
sts _SPI_ok,R24
.dbline 19
; SPI_datain=1;
sts _SPI_datain,R24
.dbline 20
; if (++rx_wr_index == SIZE) rx_wr_index = 0; //放入接收缓冲区,并调整队列指针
lds R24,_rx_wr_index
subi R24,255 ; addi 1
mov R2,R24
sts _rx_wr_index,R2
cpi R24,100
brne L2
.dbline 20
clr R2
sts _rx_wr_index,R2
L2:
.dbline 21
; if (++rx_counter == SIZE)
lds R24,_rx_counter
subi R24,255 ; addi 1
mov R2,R24
sts _rx_counter,R2
cpi R24,100
brne L4
.dbline 22
; {
.dbline 23
; rx_counter = 0;
clr R2
sts _rx_counter,R2
.dbline 24
; rx_buffer_overflow = 1;
ldi R24,1
sts _rx_buffer_overflow,R24
.dbline 25
; }
L4:
.dbline 26
; if (tx_counter) //如果发送缓冲区中有待发的数据
lds R2,_tx_counter
tst R2
breq L6
.dbline 27
; {
.dbline 28
; --tx_counter;
mov R24,R2
subi R24,1
sts _tx_counter,R24
.dbline 29
; SPDR = SPI_tx_buff[tx_rd_index]; //发送一个字节数据,并调整指针
ldi R24,<_SPI_tx_buff
ldi R25,>_SPI_tx_buff
lds R30,_tx_rd_index
clr R31
add R30,R24
adc R31,R25
ldd R2,z+0
out 0xf,R2
.dbline 30
; SPI_ok=0;
clr R2
sts _SPI_ok,R2
.dbline 31
; if (++tx_rd_index == SIZE) tx_rd_index = 0;
lds R24,_tx_rd_index
subi R24,255 ; addi 1
mov R2,R24
sts _tx_rd_index,R2
cpi R24,100
brne L8
.dbline 31
clr R2
sts _tx_rd_index,R2
L8:
.dbline 32
L6:
.dbline -2
L1:
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 getSPIchar _getSPIchar fc
; data -> R16
.even
_getSPIchar::
.dbline -1
.dbline 36
; }
; }
;
; unsigned char getSPIchar(void)
; {
L11:
.dbline 38
L12:
.dbline 38
; unsigned char data;
; while (rx_counter == 0); //无接收数据,等待
lds R2,_rx_counter
tst R2
breq L11
.dbline 39
; data = SPI_rx_buff[rx_rd_index]; //从接收缓冲区取出一个SPI收到的数据
ldi R24,<_SPI_rx_buff
ldi R25,>_SPI_rx_buff
lds R30,_rx_rd_index
clr R31
add R30,R24
adc R31,R25
ldd R16,z+0
.dbline 40
; if (++rx_rd_index == SIZE) rx_rd_index = 0; //调整指针
lds R24,_rx_rd_index
subi R24,255 ; addi 1
mov R2,R24
sts _rx_rd_index,R2
cpi R24,100
brne L14
.dbline 40
clr R2
sts _rx_rd_index,R2
L14:
.dbline 41
; CLI();
cli
.dbline 42
; --rx_counter;
lds R24,_rx_counter
subi R24,1
sts _rx_counter,R24
.dbline 43
; SEI();
sei
.dbline 44
; return data;
.dbline -2
L10:
.dbline 0 ; func end
ret
.dbsym r data 16 c
.dbend
.dbfunc e putSPIchar _putSPIchar fV
; c -> R16
.even
_putSPIchar::
.dbline -1
.dbline 48
; }
;
; void putSPIchar(char c)
; {
L17:
.dbline 49
L18:
.dbline 49
; while (tx_counter == SIZE);//发送缓冲区满,等待
lds R24,_tx_counter
cpi R24,100
breq L17
.dbline 50
; CLI();
cli
.dbline 51
; if (tx_counter || (SPI_ok==0))//发送缓冲区已中有待发数据
tst R24
brne L22
lds R2,_SPI_ok
tst R2
brne L20
L22:
.dbline 52
; { //或SPI正在发送数据时
.dbline 53
; SPI_tx_buff[tx_wr_index] = c; //将数据放入发送缓冲区排队
ldi R24,<_SPI_tx_buff
ldi R25,>_SPI_tx_buff
lds R30,_tx_wr_index
clr R31
add R30,R24
adc R31,R25
std z+0,R16
.dbline 54
; if (++tx_wr_index == SIZE) tx_wr_index = 0; //调整指针
lds R24,_tx_wr_index
subi R24,255 ; addi 1
mov R2,R24
sts _tx_wr_index,R2
cpi R24,100
brne L23
.dbline 54
clr R2
sts _tx_wr_index,R2
L23:
.dbline 55
; ++tx_counter;
lds R24,_tx_counter
subi R24,255 ; addi 1
sts _tx_counter,R24
.dbline 56
; }
xjmp L21
L20:
.dbline 58
; else
; SPDR = c; //发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送
out 0xf,R16
L21:
.dbline 59
; SPI_ok=0;
clr R2
sts _SPI_ok,R2
.dbline 60
; SEI();
sei
.dbline -2
L16:
.dbline 0 ; func end
ret
.dbsym r c 16 c
.dbend
.dbfunc e spi_init _spi_init fV
; temp -> R16
.even
_spi_init::
.dbline -1
.dbline 65
; }
;
; //从机模式
; void spi_init(void)
; {
.dbline 68
; unsigned char temp;
; //MISO=ouput and MOSI,SCK,SS = input
; DDRB |=BIT(MISO);
sbi 0x17,6
.dbline 69
; PORTB |=BIT(MISO); //MISO上拉电阻有效
sbi 0x18,6
.dbline 70
; SPCR = 0xC4; //SPI允许,从机模式,MSB,允许SPI中断 极性方式01,1/4系统时钟速率
ldi R24,196
out 0xd,R24
.dbline 71
; SPSR = 0x00;
clr R2
out 0xe,R2
.dbline 72
; temp = SPSR;
in R16,0xe
.dbline 73
; temp = SPDR; //清空SPI,和中断标志,使SPI空闲
in R16,0xf
.dbline 74
; SPI_ok=1;
ldi R24,1
sts _SPI_ok,R24
.dbline 75
; SPI_datain=0;
sts _SPI_datain,R2
.dbline -2
L25:
.dbline 0 ; func end
ret
.dbsym r temp 16 c
.dbend
.area lit(rom, con, rel)
L27:
.byte 1,2
.byte 3,4
.byte 5,6
.byte 7
.byte 0
.area text(rom, con, rel)
.dbfunc e main _main fV
; data -> y+0
; i -> R20
.even
_main::
sbiw R28,8
.dbline -1
.dbline 79
; }
;
; void main(void)
; {
.dbline 80
; unsigned char i=0;
clr R20
.dbline 81
; unsigned char data[8]={1,2,3,4,5,6,7};
ldi R24,<L27
ldi R25,>L27
movw R30,R28
ldi R16,8
ldi R17,0
st -y,R31
st -y,R30
st -y,R25
st -y,R24
xcall asgncblk
.dbline 82
; CLI(); //关中断
cli
.dbline 83
; spi_init(); //初始化SPI接口
xcall _spi_init
.dbline 84
; SEI(); //开中断
sei
xjmp L29
L28:
.dbline 86
; while(1)
; {
.dbline 87
; if(SPI_datain)
lds R2,_SPI_datain
tst R2
breq L31
.dbline 88
; {
.dbline 89
; for (i=0;i<7;i++)
clr R20
xjmp L36
L33:
.dbline 90
; {
.dbline 91
; if(getSPIchar()==0xAA)
xcall _getSPIchar
cpi R16,170
brne L37
.dbline 92
; putSPIchar(data[i]);
movw R24,R28
mov R30,R20
clr R31
add R30,R24
adc R31,R25
ldd R16,z+0
xcall _putSPIchar
xjmp L38
L37:
.dbline 94
; else
; putSPIchar(0xee);
ldi R16,238
xcall _putSPIchar
L38:
.dbline 95
L34:
.dbline 89
inc R20
L36:
.dbline 89
cpi R20,7
brlo L33
.dbline 96
; }
; SPI_datain=0;
clr R2
sts _SPI_datain,R2
.dbline 97
; }
L31:
.dbline 99
L29:
.dbline 85
xjmp L28
X0:
.dbline -2
L26:
adiw R28,8
.dbline 0 ; func end
ret
.dbsym l data 0 A[8:8]c
.dbsym r i 20 c
.dbend
.area bss(ram, con, rel)
_SPI_datain::
.blkb 1
.dbsym e SPI_datain _SPI_datain c
_SPI_ok::
.blkb 1
.dbsym e SPI_ok _SPI_ok c
_tx_counter::
.blkb 1
.dbsym e tx_counter _tx_counter c
_tx_rd_index::
.blkb 1
.dbsym e tx_rd_index _tx_rd_index c
_tx_wr_index::
.blkb 1
.dbsym e tx_wr_index _tx_wr_index c
_rx_buffer_overflow::
.blkb 1
.dbsym e rx_buffer_overflow _rx_buffer_overflow c
_rx_counter::
.blkb 1
.dbsym e rx_counter _rx_counter c
_rx_rd_index::
.blkb 1
.dbsym e rx_rd_index _rx_rd_index c
_rx_wr_index::
.blkb 1
.dbsym e rx_wr_index _rx_wr_index c
_SPI_tx_buff::
.blkb 100
.dbsym e SPI_tx_buff _SPI_tx_buff A[100:100]c
_SPI_rx_buff::
.blkb 100
.dbsym e SPI_rx_buff _SPI_rx_buff A[100:100]c
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -