It works for me at least…
/*
nRF2401 test code to run on the 24G demo board, V02,
This code will exchange user entered text between 2 modules.
Compiles with the free version of CC5X.
Pete Dokter, 2/22/06
This version ported to BoostC.
Orin Eman, 11/22/07
config_setup word 16 bits found on pages 13-15
23: 0 Payloads have an 8 bit address
22: 0
21: 1
20: 0
19: 0
18: 0
17: 1 16-Bit CRC
16: 1 CRC Enabled
15: 0 One channel receive
14: 1 ShockBurst Mode
13: 0 250K Transmission Rate
12: 0
11: 1
10: 1
9: 1 RF Output Power
8: 0 RF Output Power
7: 0 Channel select (channel 2)
6: 0
5: 0
4: 0
3: 0
2: 1
1: 0
0: 0 Transmit mode
*/
#include <system.h>
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG1, 0x3F30 //Internal Oscillator, No WDT, MCLR Enabled
#define CS porta.0 //out
#define CLK1 porta.1 //out
#define DATA1 porta.2 //I/O
#define DR1 porta.3 //in
#define DATA2 porta.4 //in
#define CE porta.6 //out
#define CLK2 porta.7 //out
#define DR2 portb.0 //in
#define stat1 portb.1 //out
#define stat2 portb.3 //out
#define stat3 portb.4 //out
unsigned char data_array[4];
void boot_up(void);
void configure_receiver(void);
void configure_transmitter(void);
void transmit_data(void);
void receive_data(void);
void output_byte(unsigned char);
unsigned char input_byte(void);
void main()
{
unsigned char x;
boot_up();
x = 3;
do {
stat1 = 1;
delay_ms(50);
stat1 = 0;
stat2 = 1;
delay_ms(50);
stat2 = 0;
stat3 = 1;
delay_ms(50);
stat3 = 0;
} while ( --x != 0 );
stat1 = 1;
configure_receiver();
for ( ; ; )
{
if ( pir1.RCIF )
{
data_array[0] = rcreg;
configure_transmitter();
transmit_data();
configure_receiver();
}
if( DR1 == 1 ) //We have data!
{
receive_data();
txreg = data_array[0];
if (stat1 == 1)
{
stat1 = 0;
stat2 = 1;
}
else if (stat2 == 1)
{
stat2 = 0;
stat3 = 1;
}
else
{
stat3 = 0;
stat1 = 1;
}
}
}
}
void boot_up(void)
{
osccon = 0b01110000; //Setup internal oscillator for 8MHz
while(osccon.2 == 0); //Wait for frequency to stabilize
ansel = 0b00000000; //Turn pins to Digital instead of Analog
cmcon = 0b00000111; //Turn off comparator on RA port
porta = 0b00000000; // TX on powerup, so DATA as output is OK
trisa = 0b00111100; //0 = Output, 1 = Input
portb = 0b00000000;
trisb = 0b11000101; //0 = Output, 1 = Input
txsta.BRGH = 1; //High speed UART
spbrg = 51; //9600 baud
txsta.SYNC = 0;
rcsta.SPEN = 1;
rcsta.CREN = 1;
txsta.TXEN = 1; //Enable transmission
pir1.RCIF = 0;
pie1.RCIE = 0; // We're not using interrupts
status.GIE = 0;
data_array[0] = 0x00;
data_array[1] = 0x00;
data_array[2] = 0x00;
data_array[3] = 0x00;
}
//2.4G Configuration - Receiver
//This setups up a RF-24G for receiving at 1mbps
void configure_receiver(void)
{
//Config Mode
porta = 0b00000000; // Everything low
// Need Td here (50nS so no worry)
porta = 0b00000001; // CS = 1
// Now safe to set DATA1 as output
trisa = 0b00111000; //0 = Output, 1 = Input (DR1 is on RA3) (DATA1 is on RA2)
delay_us(5); // Tcs2data
//Setup configuration word, set up for 250k
//config_setup = 0b.0010.0011.0100.1110.0000.0101; //Look at pages 13-15 for more bit info
output_byte(0b00100011);
output_byte(0b01001110);
output_byte(0b00000101);
porta = 0b00000001; // Everything but CS low
// After configuration of the receiver, we need DATA1 as an input
trisa = 0b00111100; // 0 = Output, 1 = Input (DR1 is on RA3) (DATA1 is on RA2)
porta = 0b00000000; // CS low
// Configuration is active on falling edge of CS (page 10)
// Td (50 nS) required here
CE = 1; //Start monitoring the air
}
//This will clock out the current payload into the data_array
void receive_data(void)
{
unsigned char i, temp;
CE = 0; //Power down RF Front end
//Clock in data, we are setup for 32-bit payloads
for(i = 0; i < 4 ; i++) //4 bytes
{
temp = input_byte();
data_array[i] = temp; //Store this byte
}
CE = 1; //Power up RF Front end
}
//2.4G Configuration - Transmitter
//This sets up one RF-24G for shockburst transmission
void configure_transmitter(void)
{
//Config Mode
porta = 0b00000000; // Everything low
// Need Td here (50nS so no worry)
porta = 0b00000001; // CS = 1
// Now safe to set DATA1 as output
trisa = 0b00111000; //0 = Output, 1 = Input (DR1 is on RA3) (DATA1 is on RA2)
delay_us(5); // Tcs2data
//Setup configuration word
//config_setup = 0b.0010.0011.0100.1110.0000.0100; //Look at pages 13-15 for more bit info
output_byte(0b00100011);
output_byte(0b01001110);
output_byte(0b00000100);
// Configuration is active on falling edge of CS (page 10)
// DATA1 (RA2) is left as an output.
porta = 0b00000000; // CS = 0
// FWIW, Td is required here
}
//This sends out the data stored in the data_array
//data_array must be setup before calling this function
void transmit_data(void)
{
unsigned char i;
CE = 1;
delay_us(5); // Tce2data
//Clock in address
// rf_address = 0b.1110.0111; // Power-on Default for all units (on page 11)
output_byte(0b11100111);
//Clock in the data_array
for(i = 0 ; i < 4 ; ++i) // 4 bytes
{
output_byte(data_array[i]);
}
CE = 0; // Start transmission
delay_us(195); // Tsby2txSB
i = 4; // Now delay Toa, 5uS per bit
do {
delay_us(8+8+32+16); // Preamble + address + payload + CRC
} while ( --i != 0 );
}
#pragma OPTIMIZE "1"
void output_byte(unsigned char byte)
{
unsigned char i = 8;
do {
DATA1 = byte.7;
// Ts(500nS) might be marginal if the clock is a bit fast
CLK1 = 1;
byte <<= 1; // Th/Thmin are both 500nS
CLK1 = 0;
} while ( --i != 0 );
}
#pragma OPTIMIZE "1"
unsigned char input_byte(void)
{
unsigned char byte;
unsigned char j = 8;
do
{
byte <<= 1;
CLK1 = 1;
byte.0 = DATA1; // Ensure Thmin of 500nS
CLK1 = 0;
} while ( --j != 0 );
return byte;
}
Edit: fixed a comment