| Example: |
.ORG 0x1000 ; This is a rather lengthy example
nRF_ReadAndWriteExample:
ADIW R26, 0 ; If there is nothing on the command line past
BREQ AEarly_Ret ; the "@" command (the "Command Tail"), exit
MOVW R30, R26 ; Save the pointer to the Command Tail
MOVW R24, R26 ; Use that pointer for the first parse request
CALL AParseValue ; If a numeric value was able to be extracted
SBRS R21, BIT_PV_VALID ; (parsed) from the string, skip Early_Ret
Early_Ret: RET ; If not, there's nothing more to be done
MOVW R24, R26 ; Reset the "next string" pointer into R25:R24
PUSH R22 ; Save the Register Index BYTE specified
LDI R22, '=' ; Parse a value after the first equal sign
CALL AParseValueAfter ; The Low Order BYTE is returned in R22
POP R24 ; Retrieve the Register Index (PUSHed as R22)
SBRS R21, BIT_PV_VALID ; before checking R21; if the first data BYTE
RJMP AEarly_Ret ; could not be parsed, exit now
Have2Values:
CALL APushAll ; Ready to begin since there are 2 values
LDI R18, 4 ; Initialize the parsing loop iteration limit
MOV R4, R18 ; to 4 loops
MOV R5, R22 ; Save the first/only data BYTE to Write
MOV R9, R24 ; Save the Register Index specified in R9
MOVW R10, R30 ; Save the Command Tail pointer in R11:R10
LDI R17, 0x29B >> 8 ; SRAM address 0x29B is uVars.B[ 27 ], the
LDI R16, 0x29B & 0xFF ; last one guaranteed to always be valid
MOVW R30, R16 ; Point to the uVars.B[##] temporary buffer
ST Z+, R5 ; Enbuffer the first data BYTE parsed
ParseLoop:
ADIW R26, 0 ; See if the 'X' pointer indicates "no more
BREQ AReadBeforeWrite ; values to parse"; if so, ready to write
MOVW R24, R26 ; Point R25:R24 at the next value to parse
CALL AParseValue ; from the Command Tail and parse it into
SBRS R21, BIT_PV_VALID ; R25:R24:R23:R22; see if a value was parsed
RJMP AReadBeforeWrite ; If not, exit the loop without saving it
ST Z+, R22 ; Save the validated, data BYTE just parsed
DEC R4 ; Decrement the loop count (5 data BYTE limit)
BRNE AParseLoop ; Continue until all BYTEs have been parsed
ReadBeforeWrite:
RCALL AReadRegValue ; Read the selected Register's present value
LDI R25, ACSZ_Reg >> 8 ; Point to the "nRF Register" string
LDI R24, ACSZ_Reg & 0xFF
MOV R22, R9 ; Transfer the Index to R22 (the Hex BYTE to
LDI R19, ' ' ; be printed after the string) and set a
CALL APrintDescAndHexBYTE ; single space as the separator
LDI R25, ACSZ_Val >> 8 ; Point to the string " value before"
LDI R24, ACSZ_Val & 0xFF
RCALL APrintRegValue ; Call the local Print Register helper
SendWrite:
MOV R24, R9 ; Register Index (command line before '=')
MOVW R22, R16 ; Specify where to write the results
CALL AnRF_Write1Register ; Modify the nRF24L01+ Register's contents
RCALL AReadRegValue ; Read the selected Register's new value
LDI R25, ACSZ_Now >> 8 ; Point to the string "; now "
LDI R24, ACSZ_Now & 0xFF
RCALL APrintRegValue ; Call the local Print Register helper
CALL APrintNewLine ; Advance the output to the next line
JMP APopAll ; Restore Registers and Stack, then exit
;------------------------ Local subroutines begin: -----------------------
ReadRegValue:
MOV R24, R9 ; The Register index specified
MOVW R22, R10 ; Destination buffer = Command Tail
CALL AnRF_Read1Register ; Return values = R25: Register's first BYTE,
MOV R12, R22 ; R24: REG_7_STATUS, R22: count (1 or 5) and
Local_Ret: RET ; either 1 or 5 BYTEs in the target buffer
PrintRegValue:
MOVW R30, R10 ; Point to the same Command Tail location
LD R22, Z+ ; Load the first Register contents data BYTE
LDI R19, '=' ; returned and set the separator
CALL APrintDescAndHexBYTE ; Display the string and value
PrintLoop:
DEC R12 ; Decrement the reply BYTE count returned in
BREQ ALocal_Ret ; R22 and exit upon reaching zero
LDI R24, ',' ; Print the intermediate ", " between values
CALL APrint1CHARAnd1Space
LD R24, Z+ ; Read the next BYTE returned from the target
CALL APrint0xHexBYTE ; buffer and print its value
RJMP APrintLoop ; Always loop back; the check is at the top
CSZ_Reg: .STRING "nRF Register"
CSZ_Val: .STRING " value before"
CSZ_Now: .STRING "; now" |
| Test it: |
>N ; Display all nRF24L01 Register contents
0x00 (0E) -> 0x7F = B01111111
0x01 (0E) -> 0x00 = B00000000
0x02 (0E) -> 0x3F = B00111111
0x03 (0E) -> 0x03 = B00000011
0x04 (0E) -> 0x34 = B00110100
0x05 (0E) -> 0x4E = B01001110
0x06 (0E) -> 0x06 = B00000110
0x07 (0E) -> 0x0E = B00001110
0x08 (0E) -> 0x00 = B00000000
0x09 (0E) -> 0x00 = B00000000
0x0A (0E) -> 0x32, 0x44, 0x65, 0x6D, 0x6F
0x0B (0E) -> 0x31, 0x44, 0x65, 0x6D, 0x6F
0x0C (0E) -> 0x33 = B00110011
0x0D (0E) -> 0x34 = B00110100
0x0E (0E) -> 0x35 = B00110101
0x0F (0E) -> 0x36 = B00110110
0x10 (0E) -> 0x32, 0x44, 0x65, 0x6D, 0x6F
0x11 (0E) -> 0x00 = B00000000
0x12 (0E) -> 0x00 = B00000000
0x13 (0E) -> 0x00 = B00000000
0x14 (0E) -> 0x00 = B00000000
0x15 (0E) -> 0x00 = B00000000
0x16 (0E) -> 0x00 = B00000000
0x17 (0E) -> 0x11 = B00010001
0x1C (0E) -> 0x3F = B00111111
0x1D (0E) -> 0x07 = B00000111
> ; Enter into a privileged mode to issue '@' commands (using '$')
m>@ 4096 2 = 0x1E
nRF Register 0x02 value before = 0x3F; now = 0x1E
m>N 2 ?
0x02 (0E) -> 0x1E = B00011110
m>@ 0x1000 2 = 7
nRF Register 0x02 value before = 0x1E; now = 0x07
m>n 2 ?
0x02 (0E) -> 0x07 = B00000111
m>@ 4096 2 = 0x3F
nRF Register 0x02 value before = 0x07; now = 0x3F
m>@ 0x1000 10 = 65 66 67 68 69
nRF Register 0x0A value before = 0x32, 0x44, 0x65, 0x6D, 0x6F; now = 0x41, 0x42, 0x43, 0x44, 0x45
m>n 10 ?
0x0A (0E) -> 0x41, 0x42, 0x43, 0x44, 0x45
m>@ 4096 = 0x32 0x44 0x65 0x6d 0x6F ; Missing Register to change; skipped
m>@ 4096 10 = ; No new data values; skipped
m>@ 4096 10 = 0x32 0x44 0x65 0x6d 0x6F
nRF Register 0x0A value before = 0x41, 0x42, 0x43, 0x44, 0x45; now = 0x32, 0x44, 0x65, 0x6D, 0x6F
m>N 10 ?
0x0A (0E) -> 0x32, 0x44, 0x65, 0x6D, 0x6F
m>N 5 = 2
m>@ 4096 5 80 ; Rejected because there is no "=" separator
m>@ 4096 5 = 80
nRF Register 0x05 value before = 0x02; now = 0x50
m>@ 4096 5 = 90 ; The Operating System limits this value to 82
nRF Register 0x05 value before = 0x50; now = 0x52
m>n 5 ?
0x05 (0E) -> 0x52 = B01010010
m>n 5 = 0
m>N 5 ?
0x05 (0E) -> 0x00 = B00000000
m>n 5 = 100
m>n 5 ?
0x05 (0E) -> 0x52 = B01010010
m>@ 0x1000 5 = 0x4E
nRF Register 0x05 value before = 0x52; now = 0x4E
m>
|