📄 ex2a.asm
字号:
;= ex2a.asm ====================================================================
;
; Copyright (C) 2003 Nordic VLSI ASA
;
; Author(s) : Ole Saether
; Support mail : nrfprog@nvlsi.no
;
; DESCRIPTION:
;
; This program gives an example of using the ADC and PWM together with the
; radio. You need two nrf24E1 evaluation boards to test this program. One
; board will act as the transmitter and the other as the receiver determined
; by the logical level on pin DIO8 (P0.6).
;
; After initializing the ADC and the radio, the transmitter enters an infinite
; loop continuously reading the ADC and transmitting the read data.
;
; After initializing the PWM and radio, the receiver enters an infinite loop
; continuously waiting for a radio packet and updating the PWM with the data
; in the received packet.
;
; The functionality is the same as in ex2c.c.
;
; ASSEMBLER:
;
; You need as31.exe to assemble this program. it can be downloaded from this
; web page: http://www.pjrc.com/tech/8051/
;
; $Revision: 2 $
;
;===============================================================================
.equ CKCON, 0x8e
.equ EXIF, 0x91
.equ P0_DIR, 0x94
.equ P0_ALT, 0x95
.equ RADIO, 0xA0
.equ ADCCON, 0xa1
.equ ADCDATAH, 0xa2
.equ ADCDATAL, 0xa3
.equ ADCSTATIC, 0xa4
.equ PWMCON, 0xa9
.equ PWMDUTY, 0xaa
.equ SPI_DATA, 0xb2
.equ SPI_CTRL, 0xb3
.equ SPICLK, 0xb4
ljmp start
start: mov P0_ALT, #0x00
mov P0_DIR, #0x40 ; P0.6 is input, the rest output
mov P0, #0x10 ; P0.4 = 1 for the rec/tran strap
setb RADIO.7 ; Turn on Radio (PWR_UP=1)
acall rf_init ; Initialize radio
jb P0.6, receiver
trasmitter: acall adc_init
acall settxmode
transmitter1: acall read_adc
acall tx_packet
sjmp transmitter1
receiver: acall pwm_init
acall setrxmode
receiver1: acall rx_packet
acall write_pwm
sjmp receiver1
read_adc: mov a, EXIF
jnb acc.4, read_adc
anl EXIF, #0xef
mov a, ADCDATAH ; Read ADC result
anl ADCCON, #0x7f ; Start new...
orl ADCCON, #0x80 ; ...conversion
ret
write_pwm: mov PWMDUTY, a
ret
rf_init: mov r1, #255
acall wait
mov SPICLK, #0x00 ; SPICLK = CLK/8
mov SPI_CTRL, #0x02 ; Connect SPI to RADIO CH1
mov dptr, #confw
mov a, #0
movc a, @a+dptr ; Get number of config bytes...
mov r3, a ; ...and store it in r3
setb RADIO.3 ; RF SPI CS = 1
mov r1, #10
acall wait
rf_init1: inc dptr ; Point to next data byte
mov a, #0
movc a, @a+dptr ; Read next config byte
acall spi_wr ; Write byte to nrf2401
djnz r3, rf_init1 ; Loop until all bytes are written
clr RADIO.3 ; RF SPI CS = 0
ret
tx_packet: setb RADIO.6 ; RF CE = 1
mov r7, a
mov r1, #1
acall wait
mov dptr, #addr
mov r1, #4
tx_packet1: mov a, #0
movc a, @a+dptr ; Read next config byte
acall spi_wr
inc dptr ; Point to next address byte
djnz r1, tx_packet1
mov a, r7
acall spi_wr
clr RADIO.6 ; RF CE = 0
mov r1, #100
acall wait
ret
rx_packet: setb RADIO.6 ; RF CE = 1
rx_packet1: jnb RADIO.2, rx_packet1 ; Wait for RF data ready
acall spi_rd
clr RADIO.6 ; RF CE = 0
ret
setrxmode: mov r0, #0x01 ; Set RXEN bit
ajmp setrxtxmode
settxmode: mov r0, #0x00
setrxtxmode: mov dptr, #confw
mov a, #0
movc a, @a+dptr ; a = #of bytes in 2401 conf word
movc a, @a+dptr ; Get last byte of conf word
setb RADIO.3 ; RF SPI CS = 1
orl a, r0 ; Tx if r0=0, Rx if r0=1
mov r1, #1
acall wait
acall spi_wr ; Write byte to radio
clr RADIO.3 ; RF SPI CS = 0
ret
spi_rd:
spi_wr: anl EXIF, #0xdf ; Clear SPI interrupt
mov SPI_DATA, a ; Move byte to send into SPI_DATA
spi_rdwr1: mov a, EXIF ; Wait until...
jnb acc.5, spi_rdwr1 ; ...SPI_RX bit is set
mov a, SPI_DATA ; Move byte received into acc...
ret
; adc_init: initialize ADC =====================================================
;
adc_init: anl ADCSTATIC, #0xfc ; 8bit
orl ADCSTATIC, #0x01
mov ADCCON, #0x20 ; Ch 0, NPD=1, ADCRUN=0, EXTREF=0
anl ADCCON, #0x7f ; Start new...
orl ADCCON, #0x80 ; ...conversion
ret
; pwm_init: initialize PWM =====================================================
;
pwm_init: orl P0_ALT, #0x80 ; Select P0.7 as PWM output
mov PWMCON, #0xc0 ; Enable 8 bit PWM, pre=min
ret
; wait: simple wait function ===================================================
;
wait: mov r2, #2
wait1: djnz r2, wait1
djnz r1, wait
ret
confw: .db 15 ; Number of bytes that follow
.db 0x08, 0x08, 0x00, 0x00, 0x00, 0x00
.db 0x00, 0x00
addr: .db 0xaa, 0xbb, 0x12, 0x34
.db 0x83, 0x6c, 0x04
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -