Aries
New Member
I use Nordic NRF24L01 transceivers, which use SPI to communicate with the Picaxe. Originally, I used 20M2s, and so had to use bit-banging. Recently, I started to use 20X2s as well, and so "upgraded" to using SPIIN and SPIOUT. No problems. Now, for the first time, I want to use a Picaxe 40X2. When I try to use the SPIIN code which works correctly on the 20X2, the values returned are inverted (i.e. instead of getting "b0", I get "NOT b0"). Am I missing something very obvious, or is there a difference between the 40X2 and the 20X2?
This is the code I have run for both chips - the code is identical, and I plugged the 20X2 port B pins into the corresponding pins in the 40X2 holder on the PCB, so the connections are identical. The code writes to two registers (0000 and 1010), with the write bit (00100000) set, and then reads them back (without the write bit set).
These are the results:
Cases 1 and 3 (bit-banging on receive) are the same with both chips
In cases 0 and 2 (SPIIN on receive), the 40X2 receives the inverse of the 20X2
This is the code I have run for both chips - the code is identical, and I plugged the 20X2 port B pins into the corresponding pins in the 40X2 holder on the PCB, so the connections are identical. The code writes to two registers (0000 and 1010), with the write bit (00100000) set, and then reads them back (without the write bit set).
Code:
#terminal 9600
symbol pinMISO = pinB.2 ' Master In, Slave Out (from NRF)
symbol MISO = B.2 ' Master In, Slave Out (from NRF)
symbol SCK = B.3 ' SPI clock
symbol CE = B.4 ' Chip Enable
symbol pinCE = pinB.4
symbol pinMOSI = pinB.6 ' Master Out, Slave In (to NRF)
symbol MOSI = B.6 ' Master Out, Slave In (to NRF)
symbol IRQ = pinB.5 ' Interrupt (from NRF)
symbol CSN = B.7 ' Chip Select Not (keep high)
'symbol PowerPin = A.1
symbol PowerPin = B.0
low PowerPin
pause 5000
for b1 = 0 to 3
sertxd(CR,LF,"Case ",#b1,CR,LF)
gosub InitNRF2401
sertxd(CR,LF)
gosub ReportNRF2401
next b1
end
InitNRF2401:
HIGH CSN 'Chip select
LOW SCK
LOW MOSI
LOW CE 'disable transceiver
pause 1000
high CSN 'Chip select (not)
low SCK
low MOSI
' Send 00100000
' Send 00111111
b0 = %00100000 : gosub SendToNRF
b0 = %00111111 : gosub SendToNRF
high CSN
' Send 00101010
' Send 11100111
b0 = %00101010 : gosub SendToNRF
b0 = %11100111 : gosub SendToNRF
high CSN
return
ReportNRF2401:
pause 1000
b0 = %00000000 : gosub SendToNRF
gosub GetFromNRF
high CSN
b0 = %00001010 : gosub SendToNRF
gosub GetFromNRF
high CSN
return
SendToNRF:
sertxd ("Send ",#bit7,#bit6,#bit5,#bit4,#bit3,#bit2,#bit1,#bit0,CR,LF)
low CSN
select case b1
case 0,1
spiout SCK,MOSI,MSBFirst_L,(b0)
case 2,3
for b2 = 0 to 7
pinMOSI = bit7
pulsout SCK,1
b0 = b0 * 2 ' shift variable left for MSB
next b2
end select
return
'*********************************************
' get one byte (MSB first) from NRF
GetFromNRF:
select case b1
case 0,2
spiin SCK,MISO,MSBPre_L,(b0)
case 1,3
b0 = 0
for b12 = 0 to 7
b0 = b0 * 2 + pinMISO ' set LSB if MISO = 1
pulsout SCK,1
next b12
end select
sertxd ("Recv ",#bit7,#bit6,#bit5,#bit4,#bit3,#bit2,#bit1,#bit0,CR,LF)
return
Code:
Picaxe 40X2 Picaxe 20X2
Case 0
Send 00100000 Send 00100000
Send 00111111 Send 00111111
Send 00101010 Send 00101010
Send 11100111 Send 11100111
Send 00000000 Send 00000000
Recv 11000000 Recv 00111111
Send 00001010 Send 00001010
Recv 00011000 Recv 11100111
Case 1
Send 00100000 Send 00100000
Send 00111111 Send 00111111
Send 00101010 Send 00101010
Send 11100111 Send 11100111
Send 00000000 Send 00000000
Recv 00111111 Recv 00111111
Send 00001010 Send 00001010
Recv 11100111 Recv 11100111
Case 2
Send 00100000 Send 00100000
Send 00111111 Send 00111111
Send 00101010 Send 00101010
Send 11100111 Send 11100111
Send 00000000 Send 00000000
Recv 11000000 Recv 00111111
Send 00001010 Send 00001010
Recv 00011000 Recv 11100111
Case 3
Send 00100000 Send 00100000
Send 00111111 Send 00111111
Send 00101010 Send 00101010
Send 11100111 Send 11100111
Send 00000000 Send 00000000
Recv 00111111 Recv 00111111
Send 00001010 Send 00001010
Recv 11100111 Recv 11100111
In cases 0 and 2 (SPIIN on receive), the 40X2 receives the inverse of the 20X2