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

📄 spi_test.s

📁 用ICC编写的程序包括SPI
💻 S
字号:
	.module Spi_test.c
	.area text(rom, con, rel)
	.dbfile F:\IccAvr_Pro\Spi_test.c
	.area vector(rom, abs)
	.org 40
	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 18
; #include <iom16v.h>
; #include <macros.h>
; #include "Display.h"
; 
; #define SIZE 10  
; #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:11  
; void spi_stc_isr(void)  
; {  
	.dbline 20
;   //PORTB|=BIT(SS);
;   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 21
;   SPI_datain=1;
	ldi R24,1
	sts _SPI_datain,R24
	.dbline 22
;   SPI_ok=1;
	sts _SPI_ok,R24
	.dbline 23
;   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,10
	brne L2
	.dbline 23
	clr R2
	sts _rx_wr_index,R2
L2:
	.dbline 24
;   if (++rx_counter == SIZE)  
	lds R24,_rx_counter
	subi R24,255    ; addi 1
	mov R2,R24
	sts _rx_counter,R2
	cpi R24,10
	brne L4
	.dbline 25
;   {  
	.dbline 26
;     rx_counter = 0;  
	clr R2
	sts _rx_counter,R2
	.dbline 27
;     rx_buffer_overflow = 1;  
	ldi R24,1
	sts _rx_buffer_overflow,R24
	.dbline 28
;   }  
L4:
	.dbline 29
;   if (tx_counter)        //如果发送缓冲区中有待发的数据  
	lds R2,_tx_counter
	tst R2
	breq L6
	.dbline 30
;   {  
	.dbline 31
;      --tx_counter;  
	mov R24,R2
	subi R24,1
	sts _tx_counter,R24
	.dbline 33
;      //PORTB&=~BIT(SS);
; 	 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 34
;      SPI_ok=0;
	clr R2
	sts _SPI_ok,R2
	.dbline 35
; 	 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,10
	brne L8
	.dbline 35
	clr R2
	sts _tx_rd_index,R2
L8:
	.dbline 36
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 40
;   }  
; }  
; 
; unsigned char getSPIchar(void)  
; {  
L11:
	.dbline 42
L12:
	.dbline 42
;   unsigned char data;  
;   while (rx_counter == 0);     //无接收数据,等待  
	lds R2,_rx_counter
	tst R2
	breq L11
	.dbline 43
;   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 44
;   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,10
	brne L14
	.dbline 44
	clr R2
	sts _rx_rd_index,R2
L14:
	.dbline 45
;   CLI();  
	cli
	.dbline 46
;   --rx_counter;  
	lds R24,_rx_counter
	subi R24,1
	sts _rx_counter,R24
	.dbline 47
;   SEI();  
	sei
	.dbline 48
;   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 52
; }  
; 
; void putSPIchar(char c)  
; {  
L17:
	.dbline 53
L18:
	.dbline 53
;   while (tx_counter == SIZE);//发送缓冲区满,等待  
	lds R24,_tx_counter
	cpi R24,10
	breq L17
	.dbline 54
;   CLI();  
	cli
	.dbline 55
;   if (tx_counter || (SPI_ok==0))//发送缓冲区已中有待发数据  
	tst R24
	brne L22
	lds R2,_SPI_ok
	tst R2
	brne L20
L22:
	.dbline 56
;   {                //或SPI正在发送数据时  
	.dbline 57
;     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 58
;     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,10
	brne L23
	.dbline 58
	clr R2
	sts _tx_wr_index,R2
L23:
	.dbline 59
;     ++tx_counter;  
	lds R24,_tx_counter
	subi R24,255    ; addi 1
	sts _tx_counter,R24
	.dbline 60
;   }  
	xjmp L21
L20:
	.dbline 62
;   else  
;     {
	.dbline 64
; 	//PORTB&=~BIT(SS);
; 	SPDR = c;        //发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送  
	out 0xf,R16
	.dbline 65
;     SPI_ok=0;
	clr R2
	sts _SPI_ok,R2
	.dbline 66
; 	}
L21:
	.dbline 67
;   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 72
; }  
; 
; 	   	   			 //主机模式
; void spi_init(void)  
; {  
	.dbline 74
;   unsigned char temp;  
;   DDRB |= BIT(MOSI)|BIT(SCK)|BIT(SS);    //MISO=input and MOSI,SCK,SS = output 
	in R24,0x17
	ori R24,176
	out 0x17,R24
	.dbline 75
;   DDRB &=~BIT(MISO); 
	cbi 0x17,6
	.dbline 76
;   PORTB|= BIT(SCK)|BIT(MOSI)|BIT(SS);    //MISO上拉电阻有效   
	in R24,0x18
	ori R24,176
	out 0x18,R24
	.dbline 77
;   SPCR = 0xD6;    //SPI允许,主机模式,MSB,允许SPI中断,极性方式01,1/4系统时钟速率  
	ldi R24,214
	out 0xd,R24
	.dbline 78
;   SPSR = 0x00;  
	clr R2
	out 0xe,R2
	.dbline 79
;   temp = SPSR;  
	in R16,0xe
	.dbline 80
;   temp = SPDR;    //清空SPI,和中断标志,使SPI空闲
	in R16,0xf
	.dbline 81
;   SPI_ok=1;  
	ldi R24,1
	sts _SPI_ok,R24
	.dbline -2
L25:
	.dbline 0 ; func end
	ret
	.dbsym r temp 16 c
	.dbend
	.dbfunc e main _main fV
;              j -> R20
;           data -> y+0
;              i -> R22
	.even
_main::
	sbiw R28,5
	.dbline -1
	.dbline 85
; }  
; 
; void main(void)  
; {  
	.dbline 86
;   unsigned char i=0,j=0;  
	clr R22
	.dbline 86
	clr R20
	.dbline 89
;   unsigned char data[5];
; 
;   CLI();        //关中断  
	cli
	.dbline 90
;   spi_init();    //初始化SPI接口  
	xcall _spi_init
	.dbline 91
;   SEI();        //开中断  
	sei
	.dbline 92
;   PORTB&=~BIT(SS);
	cbi 0x18,4
	xjmp L28
L27:
	.dbline 94
;   while(j<1)  
;   {  
	.dbline 95
; 	for (i=0;i<5;i++)
	clr R22
	xjmp L33
L30:
	.dbline 96
; 	{
	.dbline 97
; 	putSPIchar(0xaa);
	ldi R16,170
	xcall _putSPIchar
L34:
	.dbline 98
L35:
	.dbline 98
	lds R2,_SPI_datain
	tst R2
	breq L34
	.dbline 99
	xcall _getSPIchar
	movw R24,R28
	mov R30,R22
	clr R31
	add R30,R24
	adc R31,R25
	std z+0,R16
	.dbline 100
	clr R2
	sts _SPI_datain,R2
	.dbline 101
L31:
	.dbline 95
	inc R22
L33:
	.dbline 95
	cpi R22,5
	brlo L30
	.dbline 102
	inc R20
	.dbline 103
L28:
	.dbline 93
	cpi R20,1
	brlo L27
	.dbline 104
; 	while(!SPI_datain);
; 	  data[i]=getSPIchar();
; 	  SPI_datain=0;
; 	}
; 	j++;
;   }  
;   PORTB|=BIT(SS);
	sbi 0x18,4
	.dbline 105
;   Disp_Init();
	xcall _Disp_Init
	.dbline 106
;   Display(*(unsigned int*)(data+1),0);
	clr R18
	clr R19
	movw R30,R28
	ldd R16,z+1
	ldd R17,z+2
	xcall _Display
L38:
	.dbline 109
L39:
	.dbline 109
	xjmp L38
X0:
	.dbline -2
L26:
	adiw R28,5
	.dbline 0 ; func end
	ret
	.dbsym r j 20 c
	.dbsym l data 0 A[5:5]c
	.dbsym r i 22 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 10
	.dbsym e SPI_tx_buff _SPI_tx_buff A[10:10]c
_SPI_rx_buff::
	.blkb 10
	.dbsym e SPI_rx_buff _SPI_rx_buff A[10:10]c

⌨️ 快捷键说明

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