📄 nrf905.s
字号:
.module nrf905.C
.area text(rom, con, rel)
.dbfile C:\DOCUME~1\Administrator\桌面\nrftest\nrf905.C
.dbfunc e DelayMs _DelayMs fV
; i -> R10
; ms -> R12
.even
_DelayMs::
st -y,R10
st -y,R12
mov R12,R16
.dbline -1
.dbline 7
; #define _nrf905_c_
; #include "nrf905.h"
; #include "LCD.h"
;
; //延时--------------------------------------------------
; void DelayMs( uchar ms )
; {
.dbline 10
; char i;
;
; for( i = 0; i < ms; i++ )
clr R10
xjmp L5
L2:
.dbline 11
; {
.dbline 12
; DelayUs( 1000 );
ldi R16,1000
ldi R17,3
xcall _DelayUs
.dbline 13
; }
L3:
.dbline 10
inc R10
L5:
.dbline 10
cp R10,R12
brlo L2
X0:
.dbline 15
;
; return;
.dbline -2
L1:
.dbline 0 ; func end
ld R12,y+
ld R10,y+
ret
.dbsym r i 10 c
.dbsym r ms 12 c
.dbend
.dbfunc e DelayUs _DelayUs fV
; i -> R20,R21
; us -> R16,R17
.even
_DelayUs::
st -y,R20
st -y,R21
.dbline -1
.dbline 19
; }
;
; void DelayUs(uint us)
; {
.dbline 21
; uint i;
; for( i = 0; i < us; i++ )
clr R20
clr R21
xjmp L10
L7:
.dbline 22
; { NOP(); NOP(); NOP(); NOP(); NOP(); NOP(); }
.dbline 22
nop
.dbline 22
nop
.dbline 22
nop
.dbline 22
nop
.dbline 22
nop
.dbline 22
nop
.dbline 22
L8:
.dbline 21
subi R20,255 ; offset = 1
sbci R21,255
L10:
.dbline 21
cp R20,R16
cpc R21,R17
brlo L7
X1:
.dbline -2
L6:
.dbline 0 ; func end
ld R21,y+
ld R20,y+
ret
.dbsym r i 20 i
.dbsym r us 16 i
.dbend
.dbfunc e nrf905_Config _nrf905_Config fV
.even
_nrf905_Config::
.dbline -1
.dbline 28
; }
; //-----------------------------------------------------------------
;
; //配置nRF905
; void nrf905_Config(void)
; {
.dbline 29
; CLR( PORT_SPI, CSN ); // Spi 片选开
cbi 0x18,4
.dbline 30
; nrf905_SpiRW(WC); // Write config command
clr R16
xcall _nrf905_SpiRW
.dbline 31
; nrf905_SpiRW(CH_NO_BYTE); //中心频率低8位
ldi R16,76
xcall _nrf905_SpiRW
.dbline 32
; nrf905_SpiRW(PA_PWR_10dBm | HFREQ_PLL_433MHz); //发射+10dBm,发射频率433MHz,中心频率第9位=0
ldi R16,12
xcall _nrf905_SpiRW
.dbline 33
; nrf905_SpiRW(TX_AFW_4BYTE | RX_AFW_4BYTE); //接收地址宽度4字节,发送地址宽度4字节
ldi R16,68
xcall _nrf905_SpiRW
.dbline 34
; nrf905_SpiRW(RX_PW_1BYTE); //接收数据宽度32字节
ldi R16,1
xcall _nrf905_SpiRW
.dbline 35
; nrf905_SpiRW(TX_PW_1BYTE); //发送数据宽度32字节
ldi R16,1
xcall _nrf905_SpiRW
.dbline 36
; nrf905_SpiRW(RX_ADDRESS_0); //接收有效地址第1字节
ldi R16,18
xcall _nrf905_SpiRW
.dbline 37
; nrf905_SpiRW(RX_ADDRESS_1); //接收有效地址第2字节
ldi R16,52
xcall _nrf905_SpiRW
.dbline 38
; nrf905_SpiRW(RX_ADDRESS_2); //接收有效地址第3字节
ldi R16,86
xcall _nrf905_SpiRW
.dbline 39
; nrf905_SpiRW(RX_ADDRESS_3); //接收有效地址第4字节
ldi R16,120
xcall _nrf905_SpiRW
.dbline 40
; nrf905_SpiRW(CRC16_EN | XOF_16MHz); //CRC16模式使能,晶体振荡器频率16MHz
ldi R16,216
xcall _nrf905_SpiRW
.dbline 41
; SET( PORT_SPI, CSN ); // Disable Spi
sbi 0x18,4
.dbline -2
L11:
.dbline 0 ; func end
ret
.dbend
.dbfunc e SPI_Init _SPI_Init fV
.even
_SPI_Init::
.dbline -1
.dbline 46
; }
;
; //spi寄存器以及端口初始化
; void SPI_Init(void)
; {
.dbline 47
; DDR_SPI |= ( 1 << MOSI ) | ( 1 << MISO ) | ( 1 << SCK ) | ( 1 << CSN ); // 设置MOSI和SCK、SS为输出,其他为输入
in R24,0x17
ori R24,240
out 0x17,R24
.dbline 48
; PORT_SPI |= ( 1 << MOSI ) | ( 1 << SCK ) | ( 1 << CSN );
in R24,0x18
ori R24,176
out 0x18,R24
.dbline 49
; SPCR |= ( 1 << SPE ) | ( 1 << MSTR ) | ( 0 << CPOL ) | ( 0 << SPR0); // 主机模式,fck/16, SPI方式0
in R24,0xd
ori R24,80
out 0xd,R24
.dbline -2
L12:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrfport_init _nrfport_init fV
.even
_nrfport_init::
.dbline -1
.dbline 54
; }
;
; //nRF端口初始化
; void nrfport_init( void )
; {
.dbline 55
; DDR_NRF |= ( 1 << TX_EN ) | ( 1 << TRXCE ) | ( 0 << DR ) | ( 1 << PWR );
in R24,0x17
ori R24,7
out 0x17,R24
.dbline 56
; PORT_NRF |= ( 1 << TX_EN ) | ( 1 << TRXCE ) | ( 1 << DR ) | ( 1 << PWR );
in R24,0x18
ori R24,15
out 0x18,R24
.dbline -2
L13:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_SpiRW _nrf905_SpiRW fc
; val -> R16
.even
_nrf905_SpiRW::
.dbline -1
.dbline 61
; }
;
; //spi读写函数
; uchar nrf905_SpiRW( uchar val )
; {
.dbline 62
; SPDR = val;
out 0xf,R16
L15:
.dbline 63
; while ( ( SPSR & ( 1 << SPIF ) ) == 0 );
L16:
.dbline 63
sbis 0xe,7
rjmp L15
X2:
.dbline 64
; return SPDR;
in R16,0xf
.dbline -2
L14:
.dbline 0 ; func end
ret
.dbsym r val 16 c
.dbend
.dbfunc e nrf905_StandBy _nrf905_StandBy fV
.even
_nrf905_StandBy::
.dbline -1
.dbline 69
; }
;
; //Standby 和SPI 编程模式, 一般在初始化写配置信息时用到。
; void nrf905_StandBy( void )
; {
.dbline 70
; SET( PORT_NRF, PWR ); //PWR_UP = 1 上电
sbi 0x18,2
.dbline 71
; CLR( PORT_NRF, TRXCE ); //TRX_CE = 0 关闭发送或接收
cbi 0x18,1
.dbline 72
; CLR( PORT_NRF, TX_EN ); //TX_EN = X 任意值, 当TRX_CE = 1时起作用
cbi 0x18,0
.dbline -2
L18:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_Off _nrf905_Off fV
.even
_nrf905_Off::
.dbline -1
.dbline 77
; }
;
; //掉电和SPI 编程
; void nrf905_Off( void )
; {
.dbline 78
; CLR( PORT_NRF, PWR );
cbi 0x18,2
.dbline -2
L19:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_TxOn _nrf905_TxOn fV
.even
_nrf905_TxOn::
.dbline -1
.dbline 83
; }
;
; //发送模式
; void nrf905_TxOn( void )
; {
.dbline 84
; SET( PORT_NRF, PWR ); //注意顺序
sbi 0x18,2
.dbline 85
; SET( PORT_NRF, TX_EN ); //使能发送
sbi 0x18,0
.dbline 86
; CLR( PORT_NRF, TRXCE ); //在这里还没有启动发送
cbi 0x18,1
.dbline -2
L20:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_TxSend _nrf905_TxSend fV
.even
_nrf905_TxSend::
.dbline -1
.dbline 91
; }
;
; //启动发送
; void nrf905_TxSend( void )
; {
.dbline 92
; SET( PORT_NRF, TRXCE );
sbi 0x18,1
.dbline 93
; DelayUs(20); //>10us
ldi R16,20
ldi R17,0
xcall _DelayUs
.dbline 94
; CLR( PORT_NRF, TRXCE ); //只发送一次
cbi 0x18,1
.dbline -2
L21:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_RxOn _nrf905_RxOn fV
.even
_nrf905_RxOn::
.dbline -1
.dbline 99
; }
;
; //接收模式
; void nrf905_RxOn( void )
; {
.dbline 100
; SET( PORT_NRF, PWR ); //注意顺序
sbi 0x18,2
.dbline 101
; CLR( PORT_NRF, TX_EN ); //maybe first
cbi 0x18,0
.dbline 102
; SET( PORT_NRF, TRXCE ); //
sbi 0x18,1
.dbline -2
L22:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_Init _nrf905_Init fV
.even
_nrf905_Init::
.dbline -1
.dbline 107
; }
;
; //初始化nrf905
; void nrf905_Init( void )
; {
.dbline 108
; nrfport_init(); //配置nrf端口
xcall _nrfport_init
.dbline 110
;
; nrf905_StandBy(); //掉电与spi编程模式
xcall _nrf905_StandBy
.dbline 112
;
; DelayMs(10); //!!!must >3ms
ldi R16,10
xcall _DelayMs
.dbline 114
;
; nrf905_Config(); //配置nrf
xcall _nrf905_Config
.dbline 116
;
; nrf905_RxOn(); //初始化为接收模式
xcall _nrf905_RxOn
.dbline -2
L23:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_SetTxAddr _nrf905_SetTxAddr fV
.even
_nrf905_SetTxAddr::
.dbline -1
.dbline 122
; }
;
;
; //写Tx地址
; void nrf905_SetTxAddr( void )
; {
.dbline 123
; CLR( PORT_SPI, CSN ); //启动spi
cbi 0x18,4
.dbline 124
; nrf905_SpiRW( WTA ); //写Tx地址 写TX 地址1-4 字节写操作全部从字节0 开始
ldi R16,34
xcall _nrf905_SpiRW
.dbline 125
; nrf905_SpiRW( TX_ADDRESS_0 );
ldi R16,18
xcall _nrf905_SpiRW
.dbline 126
; nrf905_SpiRW( TX_ADDRESS_1 );
ldi R16,52
xcall _nrf905_SpiRW
.dbline 127
; nrf905_SpiRW( TX_ADDRESS_2 );
ldi R16,86
xcall _nrf905_SpiRW
.dbline 128
; nrf905_SpiRW( TX_ADDRESS_3 );
ldi R16,120
xcall _nrf905_SpiRW
.dbline 129
; SET( PORT_SPI, CSN );
sbi 0x18,4
.dbline -2
L24:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_SetData _nrf905_SetData fV
; set_data -> R10
.even
_nrf905_SetData::
st -y,R10
mov R10,R16
.dbline -1
.dbline 134
; }
;
; //写数据
; void nrf905_SetData( uchar set_data )
; {
.dbline 135
; CLR( PORT_SPI, CSN ); //启动spi
cbi 0x18,4
.dbline 136
; nrf905_SpiRW( WTP ); //写TxPayload 向发送数据寄存器写特征字
ldi R16,32
xcall _nrf905_SpiRW
.dbline 137
; nrf905_SpiRW( set_data ); //写数据
mov R16,R10
xcall _nrf905_SpiRW
.dbline 138
; SET( PORT_SPI, CSN) ; //关闭spi
sbi 0x18,4
.dbline -2
L25:
.dbline 0 ; func end
ld R10,y+
ret
.dbsym r set_data 10 c
.dbend
.dbfunc e nrf905_WaitSended _nrf905_WaitSended fV
.even
_nrf905_WaitSended::
.dbline -1
.dbline 143
; }
;
; //等待发送结束
; void nrf905_WaitSended( void )
; {
L27:
.dbline 144
; while ( ( PIN_NRF &( 1 << DR ) ) == 0 );
L28:
.dbline 144
sbis 0x16,3
rjmp L27
X3:
.dbline -2
L26:
.dbline 0 ; func end
ret
.dbend
.dbfunc e nrf905_SendData _nrf905_SendData fV
; tx_data -> R10
.even
_nrf905_SendData::
st -y,R10
mov R10,R16
.dbline -1
.dbline 149
; }
;
; //发送数据
; void nrf905_SendData( uchar tx_data )
; {
.dbline 150
; nrf905_TxOn(); //切换到发送模式
xcall _nrf905_TxOn
.dbline 151
; nrf905_SetTxAddr(); //写发送地址
xcall _nrf905_SetTxAddr
.dbline 152
; nrf905_SetData( tx_data );//写数据
mov R16,R10
xcall _nrf905_SetData
.dbline 153
; nrf905_TxSend(); //启动发送
xcall _nrf905_TxSend
.dbline 154
; nrf905_WaitSended(); //等待发送结束
xcall _nrf905_WaitSended
.dbline -2
L30:
.dbline 0 ; func end
ld R10,y+
ret
.dbsym r tx_data 10 c
.dbend
.dbfunc e nrf905_ReadData _nrf905_ReadData fc
; i -> <dead>
; rx_tmp -> R10
.even
_nrf905_ReadData::
st -y,R10
.dbline -1
.dbline 160
; //nrf905_Off();
; }
;
; //读出接收到的数据
; uchar nrf905_ReadData( void )
; {
.dbline 164
; uchar i;
; uchar rx_tmp; //定义读出的数据,暂存值
;
; CLR( PORT_SPI, CSN );
cbi 0x18,4
.dbline 165
; nrf905_SpiRW( RRP ); //读RxPayload,RRP, 读数据寄存器特征字, 说明的是从字节0开始读32个字节的数据
ldi R16,36
xcall _nrf905_SpiRW
.dbline 166
; rx_tmp = nrf905_SpiRW( 0x00 );
clr R16
xcall _nrf905_SpiRW
mov R10,R16
.dbline 167
; SET( PORT_SPI, CSN );
sbi 0x18,4
.dbline 169
; //while( PIN_NRF & ( 1 << DR ) ); //等待接收完成
; return rx_tmp; //返回接收到的数据
.dbline -2
L31:
.dbline 0 ; func end
ld R10,y+
ret
.dbsym l i 1 c
.dbsym r rx_tmp 10 c
.dbend
; }
;
; //接收过程
; //
; //
; //
; //
; //
; //
; //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -