| karlgauss 
 
 
 Joined: 23 Jul 2006
 Posts: 1
 
 
 
			    
 
 | 
			
				| TRF2.4 (Nordic NRF2401) Driver |  
				|  Posted: Sun Jul 30, 2006 9:30 pm |   |  
				| 
 |  
				| Hi there, some useful code for the TRF2.4, cheers 
 Carlos Romero
 Electronic Eng.
 
 
  	  | Code: |  	  | ////////////////////////////////////////////////////////////////////////////////
 //                        Laipac RF-24G / TXRX24G
 //                   2.4GHz Wireless Transceiver Driver
 //
 // Original by carlos_rom
 //
 // Filename     : RF-24G.c
 // Programmer   : Carlos Rom
 // Version      : Version 1 - 21/07/2006
 //
 ////////////////////////////////////////////////////////////////////////////////
 
 ////////////////////////////////////////////////////////////////////////////////
 //Pin Configuration
 //
 //    Pin   Name  Pin(uC)     Description
 //    1     GND   Power       Gound (0V)
 //    2     CE    Output      Chip Enable activates RX or TX mode
 //    4     CS    Output      Chip Select activates Configuration mode
 //    5     CLK1  I/O         Clock Input(TX)&I/O(RX) for data channel 1 3-wire interface
 //    6     DATA  I/O         RX data channel 1/TX data input /3-wire interface
 //    7     DR1   Input       RX data ready at data channel 1 (ShockBurst only)
 //    10    VCC   Power       Power Supply (+3V DC)
 
 #define RF_24G_CE       PIN_B2
 #define RF_24G_CS       PIN_B1
 #define RF_24G_DATA    PIN_C0
 #define RF_24G_CLK1     PIN_C1
 #define RF_24G_DR1       PIN_B0  //interrupt driven
 
 #define num_bytes       2
 #define num_byte_conf   15
 #define address_width    2   //numero de BYTES en la direcci�n
 #define CLKDELAY       delay_us(1);
 #define CSDELAY        delay_us(20);
 #define PWUPDELAY       delay_ms(4);;
 
 //extern int8 buffer_rx[num_bytes];
 //extern int8 buffer_tx[num_bytes];
 //extern int8 configuracion_rx[num_byte_conf];
 //extern int8 configuracion_tx[num_byte_conf];
 //extern int8 address_rx[address_width];
 int8 buffer_rx[num_bytes];
 int8 buffer_tx[num_bytes]={0x0F,0xF3};
 int8 address_rx[address_width]={0x01,0xE7};
 int8 configuracion_rx[num_byte_conf]={0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x01, 0xE7, 0x43, 0x4F, 0x17}; //RX con 1 al final
 int8 configuracion_tx[num_byte_conf]={0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x01, 0xE7, 0x43, 0x4F, 0x16};  //6F = 250k 4F=1MBPS
 //palabra de control con 2 bytes de direcci�n 0x01,0xE7, con CRC 16, 16 MHz Xtal, RF CH 80, 0dBm, 1Mbps
 void OUT_BYTE(int8 b)
 {
 int8 j;
 int8 p = 7;
 for(j = 0 ; j < 8 ; j++) //One bit at a time
 {
 if( BIT_TEST(b,p--) )
 {
 OUTPUT_HIGH(RF_24G_DATA);
 }
 else
 {
 OUTPUT_LOW(RF_24G_DATA);
 }
 
 CLKDELAY;
 OUTPUT_HIGH(RF_24G_CLK1);
 CLKDELAY;
 OUTPUT_LOW(RF_24G_CLK1);
 OUTPUT_FLOAT(RF_24G_DATA);
 OUTPUT_FLOAT(RF_24G_CLK1);
 }
 }
 
 void receive_data(void)
 {
 int8 i=0, j=0, temp=0, temp2=0;
 set_tris_c(0b00000101);
 //TRF_control_port=TRF_control_port&TRF_standby;  //ponerlo inactivo
 //CSDELAY;
 //Erase the current data array so that we know we are looking at actual received data
 for(i = 0 ; i < num_bytes ; i++) //4 int8s
 { buffer_rx[i] = 0x00; }
 
 //Clock in data, el tama�o del paquete sera de 1int8*num_bytes
 for(i = 0 ; i < num_bytes ; i++)
 {
 for(j = 0 ; j < 8 ; j++) //8 bits each comenzando por el MSB
 {
 OUTPUT_HIGH(RF_24G_CLK1);
 //CLKDELAY;
 
 if (INPUT(RF_24G_DATA))
 {temp2=0x01;}
 else
 {temp2=0x00;}
 
 OUTPUT_LOW(RF_24G_CLK1);
 //CLKDELAY;
 temp <<= 1;
 temp = temp|temp2;
 
 }
 
 buffer_rx[i] = temp; //Store this int8
 }
 
 if(INPUT(RF_24G_DR1)==0) //Once the data is clocked completely, the receiver should make DR go low
 {        //hay un bit corrido hacia la izquierda
 if (buffer_rx[1]==0xF3)  //se esta recibiendo el primer int8 2 veces
 {
 output_toggle(PIN_A0);
 }
 else { output_low(PIN_A0); }
 
 }
 else
 { output_low(PIN_A0); }
 
 //    {ERROR_FLAG=1;}
 set_tris_c(0b00000111);
 OUTPUT_HIGH(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 OUTPUT_FLOAT(RF_24G_CLK1);
 OUTPUT_FLOAT(RF_24G_DATA);
 CSDELAY;
 }
 
 
 
 void transmit_data(void)
 {
 int8 i, j, temp=0,temp2=0, rf_address;
 set_tris_c(0b00000111);
 OUTPUT_HIGH(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 CSDELAY;
 set_tris_c(0b00000100);
 for(j = 0 ; j < address_width ; j++)  //comenzando por el MSB
 {
 rf_address=address_rx[j];
 OUT_BYTE(rf_address);
 }
 
 //Clock in the buffer_tx
 for(i = 0 ; i < num_bytes ; i++)
 {
 temp = buffer_tx[i];
 OUT_BYTE(temp);
 }
 OUTPUT_LOW(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 set_tris_c(0b00000111);
 output_toggle(PIN_A0);
 }
 
 void configure_receiver(void)
 {
 int8 i=0, j=0, temp=0, temp2=0;
 enable_interrupts(INT_EXT);
 enable_interrupts(GLOBAL);
 OUTPUT_LOW(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 CSDELAY;
 OUTPUT_LOW(RF_24G_DATA); OUTPUT_LOW(RF_24G_CLK1);
 PWUPDELAY;
 OUTPUT_LOW(RF_24G_CE);
 OUTPUT_HIGH(RF_24G_CS);
 CSDELAY;
 set_tris_c(0b00000100);
 
 //Clock in the buffer_tx
 for(i = 0 ; i < num_byte_conf ; i++)
 {
 temp = configuracion_rx[i];
 OUT_BYTE(temp);
 }
 
 OUTPUT_HIGH(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 set_tris_c(0b00000111);
 OUTPUT_LOW(RF_24G_CLK1);
 OUTPUT_FLOAT(RF_24G_CLK1);
 
 }
 
 void configure_transmitter(void)
 {
 int8 i, j, temp,temp2;
 OUTPUT_LOW(RF_24G_CE);
 OUTPUT_HIGH(RF_24G_CS);
 set_tris_c(0b00000100);
 PWUPDELAY;
 //Clock in the buffer_tx
 for(i = 0 ; i < num_byte_conf ; i++)
 {
 temp = configuracion_tx[i];
 OUT_BYTE(temp);
 }
 
 OUTPUT_LOW(RF_24G_CE);
 OUTPUT_LOW(RF_24G_CS);
 set_tris_c(0b00000111);
 OUTPUT_LOW(RF_24G_CLK1);
 OUTPUT_FLOAT(RF_24G_CLK1);
 }
 
 | 
 
 you can have more speed with fixed_io ports, but its straightforward
 for usage just for example (remember thar this code is using rb0 interrupt, that, you can change also easily
 
 
  	  | Code: |  	  | if(INPUT(PIN_A1)) { configure_transmitter();}
 else {configure_receiver();}
 
 
 while(1) {
 
 if(INPUT(PIN_A1)) {
 delay_ms(100);
 transmit_data();
 }
 
 
 }
 }
 
 #INT_EXT
 ext2_handler() //llega a la interrupci�n con un flanco de subida en RB0/INT0 es decir en SDI
 {
 receive_data();
 }
 
 | 
 
 
  
 Last edited by karlgauss on Fri Jun 19, 2009 4:46 am; edited 1 time in total
 |  |