Hi,
I am trying to make 2 x PIC16F689 communicate with each other through 2 x Nordic 2.4GHz Transceiver modules (http://www.sparkfun.com/commerce/produc … cts_id=151).
Everything runs at 3.3V as I use the same 3.3v voltage regulator to supply both breadboards located a few inches apart.
I used PORTA.2 as an input to set which one is the transmitter and which one is the receiver. The transmitter keeps transmitting every seconds (PORTA.0 connected to a LED is toggled shows transmit activity). The receiver polls the RF_DR1 pin and toggles PORTA.5 (connected to a LED) to show receive activity, when RF_DR1 is active.
My problem is the RF_DR1 pin on the receiver is never active (logic 1), so the LED on PORTA.5 is never toggled. On the transmitter side, the LED on PORTA.0 toggled On/Off, so the it looks like the transmitter code is executed. I have verified the code and connections and everything seems OK, according the the datasheet.
-
Is there a way to make sure the RF transceiver modules are functional ?
-
Have I missed anything in the code below, since I could NOT make these RF transceivers to work… ?
I’d really appreciate any help and suggestions…
Thanks very for your help.
Best regards
pleq
#define RF_24G_DR1 PORTC.F0
#define RF_24G_CLK1 PORTC.F1
#define RF_24G_DATA PORTC.F2
#define RF_24G_CS PORTC.F3
#define RF_24G_CE PORTC.F4
#define RF_24G_DATA_DIR TRISC.F2
////////////////////////////////////////////////////////////////////////////////
//Configuration Bytes
//
//Bytes 14-02: Shockburst Configuration
//Bytes 01-00: General Device Configuration
#define DATA2_W 8
//Byte 13: Length of data payload section RX channel 1 in bits
#define DATA1_W 8
//Byte 12-08: Channel 2 Address
#define ADDR2_4 0x00
#define ADDR2_3 0x00
#define ADDR2_2 0x00
#define ADDR2_1 0x00
#define ADDR2_0 0x00
//Byte 07-03: Channel 1 Address
#define ADDR1_4 0xE7
#define ADDR1_3 0xE7
#define ADDR1_2 0xE7
#define ADDR1_1 0xE7
#define ADDR1_0 0xE7
//Byte 02
// Bit 07-02: ADDR_W - Number of address bits (both RX channels)
// Maximum number of address bits is 40 (5 bytes)
// Bit 01: CRC_L - 8 or 16 bits CRC
// Bit 00: CRC_EN - Enable on-chip CRC generation/checking
// Combine (via |) together constants from each group
// 0b76543210
#define ADDR_W_5_BYTE 0b10100000
#define ADDR_W_4_BYTE 0b10000000
#define ADDR_W_3_BYTE 0b01100000
#define ADDR_W_2_BYTE 0b01000000
#define ADDR_W_1_BYTE 0b00100000
#define CRC_L_8_BIT 0b00000000
#define CRC_L_16_BIT 0b00000010
#define CRC_EN_DISABLE 0b00000000
#define CRC_EN_ENABLE 0b00000001
//Byte 01
// Bit 07: RX2_EN - Enable two channel receive mode
// Bit 06: CM - Communication mode ( Direct or ShockBurst)
// Bit 05: RFDR_SB - RF data rate (1Mbps requires 16MHz crystal)
// Bit 04-02: XO_F - Crystal frequency (Factory default 16MHz crystal mounted)
// Bit 01-00: RF_PWR - RF output power
// Combine (via |) together constants from each group
// 0b76543210
#define RX2_EN_DISABLE 0b00000000
#define RX2_EN_ENABLE 0b10000000
#define CM_DIRECT 0b00000000
#define CM_SHOCKBURST 0b01000000
#define RFDR_SB_250_KBPS 0b00000000
#define RFDR_SB_1_MBPS 0b00100000
#define XO_F_4MHZ 0b00000000
#define XO_F_8MHZ 0b00000100
#define XO_F_12MHZ 0b00001100
#define XO_F_16MHZ 0b00001100
#define XO_F_20MHZ 0b00010000
#define RF_PWR_N20DB 0b00000000 // -20db
#define RF_PWR_N10DB 0b00000001 // -10db
#define RF_PWR_N5DB 0b00000010 // -5db
#define RF_PWR_0DB 0b00000011 // 0db (Full Power)
//Byte 01
// Bit 07-01: RF_CH# - Frequency channel (2400MHz + RF_CH# * 1.0MHz)
// Bit 00: RXEN - RX or TX operation
// Combine (via |) together constants from each group
// 0b76543210
#define RF_CH 0b10000000 // 64 - 2464GHz
#define RXEN_TX 0b00000000
#define RXEN_RX 0b00000001
////////////////////////////////////////////////////////////////////////////////
#define BUF_MAX 1
#define CSDELAY() Delay_us(100)
#define CEDELAY() Delay_us(100)
#define TTADELAY() Delay_us(300)
#define PWUPDELAY() Delay_ms(3)
void RF_24G_initPorts(void)
{
OSCCON = 0x71; // Internal 8Mhz, OSTS = 0, SCS = 1
OPTION_REG = 0xC0; // Disable PORTA/PORTB pull-ups, INTEDG = 1
ANSEL = 0; //Turn pins to Digital instead of Analog
ANSELH = 0; //Turn pins to Digital instead of Analog
PORTA = 0;
TRISA = 0x08; //0 = Output, 1 = Input; RA3 is ALWAYS input on 16F687/689/690
WPUA = 0; // Disable Pull-ups
IOCA = 0; // Disable Interrupt-On-Change on PORTA
PORTB = 0;
TRISB = 0x20; //0 = Output, 1 = Input (RX is an input)
WPUB = 0; // Disable Pull-ups
IOCB = 0; // Disable Interrupt-On-Change on PORTB
PORTC = 0;
TRISC = 0x01; //0 = Output, 1 = Input (DR1 is input)
}
void putByte( char b )
{
//MSB bit first
int i;
RF_24G_DATA_DIR = 0; // Set DATA pin output
RF_24G_CLK1 = 0;
for (i = 0; i < 8; i++)
{
RF_24G_DATA = b.F7;
RF_24G_CLK1 = 1; // Clock out on rising edge
RF_24G_CLK1 = 0;
b <<= 1;
}
}
char getByte(void)
{
//MSB bit first
int i;
char b = 0;
RF_24G_DATA_DIR = 1; // Set DATA pin input
RF_24G_CLK1 = 0;
for (i = 0; i < 8; i++)
{
b <<= 1;
RF_24G_CLK1 = 1;
b.F0 = RF_24G_DATA;
RF_24G_CLK1 = 0; // Read before falling edge
}
return b;
}
void RF_24G_Config(void)
{
RF_24G_DATA_DIR = 0; // Set DATA pin output
RF_24G_CE = 0;
RF_24G_CS = 0;
PWUPDELAY();
RF_24G_CS = 1;
CSDELAY();
//MSB byte first
putByte(DATA2_W);
putByte(DATA1_W);
putByte(ADDR2_4);
putByte(ADDR2_3);
putByte(ADDR2_2);
putByte(ADDR2_1);
putByte(ADDR2_0);
putByte(ADDR1_4);
putByte(ADDR1_3);
putByte(ADDR1_2);
putByte(ADDR1_1);
putByte(ADDR1_0);
putByte(ADDR_W_5_BYTE |
CRC_L_16_BIT |
CRC_EN_ENABLE);
putByte(RX2_EN_DISABLE |
CM_SHOCKBURST |
RFDR_SB_1_MBPS |
XO_F_16MHZ |
RF_PWR_0DB);
putByte(RF_CH | RXEN_RX);
RF_24G_CS = 0;
RF_24G_CE = 1;
}
void RF_24G_SetTxByte(void)
{
RF_24G_DATA_DIR = 0; // Set DATA pin output
RF_24G_CE = 0;
RF_24G_CS = 1;
CSDELAY();
putByte(RF_CH | RXEN_TX);
RF_24G_CS = 0;
RF_24G_CE = 0;
}
void RF_24G_SetTx(void)
{
RF_24G_DATA_DIR = 0; // Set DATA pin output
// Once the wanted protocol, modus and RF channel are set,
// only one bit (RXEN) is shifted in to switch between RX and TX.
RF_24G_CE = 0;
RF_24G_CS = 1;
CSDELAY();
RF_24G_DATA = 0; // 0 for TX; 1 for RX;
RF_24G_CLK1 = 1; // Clock out on rising edge
RF_24G_CLK1 = 0;
RF_24G_CS = 0;
RF_24G_CE = 0;
}
void RF_24G_SetRxByte(void)
{
RF_24G_DATA_DIR = 0; // Set DATA pin output
RF_24G_CE = 0;
RF_24G_CS = 1;
CSDELAY();
putByte(RF_CH | RXEN_RX);
RF_24G_CS = 0;
RF_24G_CE = 1;
RF_24G_DATA_DIR = 1; // Set DATA pin input
}
void RF_24G_SetRx(void)
{
RF_24G_DATA_DIR = 0; // Set DATA pin output
// Once the wanted protocol, modus and RF channel are set,
// only one bit (RXEN) is shifted in to switch between RX and TX.
RF_24G_CE = 0;
RF_24G_CS = 1;
CSDELAY();
RF_24G_DATA = 1; // 0 for TX; 1 for RX;
RF_24G_CLK1 = 1; // Clock out on rising edge
RF_24G_CLK1 = 0;
RF_24G_CS = 0;
RF_24G_CE = 1;
RF_24G_DATA_DIR = 1; // Set DATA pin input
}
void putBuffer(char *pbuf)
{
int i;
RF_24G_DATA_DIR = 0; // Set DATA pin output
RF_24G_CE = 1;
CEDELAY();
putByte(ADDR1_4);
putByte(ADDR1_3);
putByte(ADDR1_2);
putByte(ADDR1_1);
putByte(ADDR1_0);
for (i = 0; i < BUF_MAX; i++)
{
putByte(pbuf*);*
}
RF_24G_CE = 0;
TTADELAY(); // Time To Air data
}
void getBuffer(char *pbuf)
{
int i;
RF_24G_DATA_DIR = 1; // Set DATA pin input
for (i = 0; i < BUF_MAX; i++)
{
pbuf = getByte();
}
RF_24G_CE = 1;
}
/////////////////////////////////////////////////////////////////////
char TxBuf[BUF_MAX];
char RxBuf[BUF_MAX];
long counter = 0;
void main()
{
unsigned char i;
TxBuf[0] = ‘A’;
RxBuf[0] = ‘?’;
RF_24G_initPorts();
TRISA.F2 = 1; // Set RA<2> as input for debug
TRISB.F5 = 1; // RB<5> UART RX as input
RF_24G_Config();
if (PORTA.F2)
RF_24G_SetTx(); // Switch to transmit
else
RF_24G_SetRx(); // Switch to receive
PORTA.F0 = 1; // Set LED on PORTA.F0 for debug purpose
PORTA.F5 = 1; // Set LED on PORTA.F5 for debug purpose
Delay_ms(1000);
PORTA.F0 = 0;
PORTA.F5 = 0;
while (1)
{
if (PORTA.F2) // Transmitter
{
if (RF_24G_DATA_DIR)
{
RF_24G_SetTx(); // switch to transmit
Delay_ms(1);
}
if (counter >= 100000)
{
counter = 0;
// Transmit RF
putBuffer(TxBuf); // send packet (buf)
PORTA.F0 = !PORTA.F0;
Delay_ms(1);
}
counter++;
}
else // Receiver
{
if (!RF_24G_DATA_DIR)
{
RF_24G_SetRx(); // switch to receive
Delay_ms(1);
}
if (RF_24G_DR1)
{
getBuffer(RxBuf); // Get packet
// if (RxBuf[0] == ‘A’)
PORTA.F5 = !PORTA.F5;
}
}
}
}