Functions | |
void | pic_rf_init (rf_config *my_config) |
Initialise nrf2401a chip with config. | |
void | pic_rf_quick_init (char *my_config, uns8 my_channel, bit my_receive_on) |
Initialise nrf2401a chip with quick config. | |
void | pic_rf_receive (uns8 *data, uns8 bytes_to_receive) |
Receive data from nrf2401a. | |
void | pic_rf_send_byte (uns8 b) |
Internal routine to send a byte to nrf2401a. | |
void | pic_rf_send_bytes (char *bytes, uns8 num_bytes) |
Internal routine to send bytes to nrf2401a. | |
void | pic_rf_set_channel (uns8 channel) |
Change channel on the nrf2401a. | |
void | pic_rf_set_mode (uns8 mode) |
Set rf mode to transmit or receive. | |
void | pic_rf_setup () |
Setup ports and pins for communication with nrf2401a. | |
void | pic_rf_transmit (char *data, uns8 bytes_to_transmit) |
Transmit data from nrf2401a. |
void pic_rf_init | ( | rf_config * | my_config | ) |
Initialise nRF24L01 chip with config.
Sends the configuration to the Nordic nrf2401a chip ready to begin communication. This routine assumes you have already set my_config to the correct values.
00084 { 00085 uns8 temp; 00086 uns8 options; 00087 00088 make_output(rf_data_port, rf_data_pin); // make data pin output 00089 clear_pin(rf_clk1_port, rf_clk1_pin); // make sure it's 0 00090 00091 pic_rf_chip_enable(0); 00092 pic_rf_chip_select(1); // config mode 00093 00094 pic_rf_send_byte(my_config->payload_width_ch2); 00095 pic_rf_send_byte(my_config->payload_width_ch1); 00096 pic_rf_send_bytes(my_config->address_ch2, 5); 00097 pic_rf_send_bytes(my_config->address_ch1, 5); 00098 00099 options = my_config->options; 00100 00101 temp = my_config->address_width << 2; 00102 temp.1 = options.OP_LONG_CRC; // |= my_config->long_crc << 1; 00103 temp.0 = options.OP_ENABLE_CRC; // |= my_config->enable_crc; 00104 00105 pic_rf_send_byte(temp); 00106 00107 temp = options & 0b11100000; //pull off top three bits 00108 //temp.7 = options.ENABLE_CH2; // |= my_config->enable_ch2 << 7; 00109 //temp.6 = options.ENABLE_SHOCKBURST; // |= my_config->enable_shockburst << 6; 00110 //temp.5 = options.ENABLE_1_MBPS; //my_config->enable_1_mbps << 5; 00111 temp |= (my_config->crystal & 0b00000111) << 2; // bits 4,3,2 - mask 3 bit range 00112 temp |= (my_config->output_power & 0b00000011); // bits 1,0 - mask 2 bit range 00113 00114 pic_rf_send_byte(temp); 00115 00116 temp = my_config->channel << 1; 00117 rf_current_channel = my_config->channel; 00118 00119 temp |= options.OP_ENABLE_RECEIVE; // my_config->enable_receive; 00120 rf_current_mode_receive = options.OP_ENABLE_RECEIVE; 00121 00122 pic_rf_send_byte(temp); 00123 00124 pic_rf_chip_select(0); // config mode ended 00125 pic_rf_chip_enable(1); // on the air! 00126 00127 }
void pic_rf_quick_init | ( | char * | my_config, | |
uns8 | my_channel, | |||
bit | my_receive_on | |||
) |
While the usual pic_rf_init() routine is excellent when you want to programatically change the 2401a config, if you're only doing this once (at the start) then it's likely you're burning a lot of instructions (154 words on a PIC16 device) just to send some bytes of config out to the 2401a. If you know your config in advance, then you can just send the byte-stream config using this routine. Use the nrf2401a_config.pl script in the tools directory to generate this string.
00062 { 00063 00064 uns8 byte_counter; 00065 make_output(rf_data_port, rf_data_pin); // make data pin output 00066 clear_pin(rf_clk1_port, rf_clk1_pin); // make sure it's 0 00067 00068 pic_rf_chip_enable(0); 00069 pic_rf_chip_select(1); // config mode 00070 00071 for(byte_counter = 0 ; byte_counter < 15 ; byte_counter++) { 00072 pic_rf_send_byte(my_config[byte_counter]); 00073 } 00074 rf_current_channel = my_channel; 00075 rf_current_mode_receive = my_receive_on; 00076 00077 pic_rf_chip_select(0); // config mode ended 00078 pic_rf_chip_enable(1); // on the air! 00079 00080 }
void pic_rf_receive | ( | uns8 * | data, | |
uns8 | bytes_to_receive | |||
) |
Receive data from nRF24L01.
Having been notified that there is data available, call this routine to clock the data in from the nrf2401a.
!pic_rf_chip_enable(0); // save power
pic_rf_chip_enable(1); // turn chip back on
00130 { 00131 uns8 byte_count, bit_count, temp; 00132 00134 bit my_store_gie = intcon.GIE; 00135 kill_interrupts(); 00136 00137 make_input(rf_data_port, rf_data_pin); // make data pin input 00138 00139 for (byte_count = 0; byte_count < bytes_to_receive; byte_count++) { 00140 00141 for (bit_count = 0; bit_count < 8; bit_count++) { 00142 temp <<= 1; 00143 temp.0 = test_pin(rf_data_port, rf_data_pin); 00144 set_pin(rf_clk1_port, rf_clk1_pin); // clock it out 00145 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00146 } 00147 data[byte_count] = temp; 00148 } 00149 00151 intcon.GIE = my_store_gie; 00152 }
void pic_rf_send_byte | ( | uns8 | b | ) |
Clock a byte into the nRF24L01.
Internal routine to send a byte to the nrf2401a. Generally you shouldn't need to use this, see pic_rf_transmit instead
00042 { 00043 uns8 bit_counter; 00044 for(bit_counter = 0 ; bit_counter < 8 ; bit_counter++) { 00045 change_pin(rf_data_port, rf_data_pin, b.7); // Put data on data pin 00046 set_pin(rf_clk1_port, rf_clk1_pin); // clock it in (positive edge) 00047 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00048 00049 b <<= 1; // Move all the bits left 00050 } // repeat until finished 00051 00052 } // pic_rf_send_byte
void pic_rf_send_bytes | ( | char * | bytes, | |
uns8 | num_bytes | |||
) |
Internal routine to send bytes to the nrf2401a. Generally you shouldn't need to use this, see pic_rf_transmit instead
00054 { 00055 00056 uns8 byte_counter; 00057 for(byte_counter = 0 ; byte_counter < num_bytes ; byte_counter++) { 00058 pic_rf_send_byte(bytes[byte_counter]); 00059 } 00060 }
void pic_rf_set_channel | ( | uns8 | channel | ) |
Receive data from nRF24L01 (inline).
Reclocks the essential config to change the current channel used by the nrf2401a.
00200 { 00201 bit my_store_gie = intcon.GIE; 00202 kill_interrupts(); 00203 00204 clear_bit(tris_array[rf_data_port - PORTA], rf_data_pin); // make data pin output 00205 00206 pic_rf_chip_enable(0); 00207 pic_rf_chip_select(1); // config mode 00208 00209 rf_current_channel = channel; 00210 channel <<= 1; 00211 channel |= rf_current_mode_receive; 00212 00213 pic_rf_send_byte(channel); 00214 00215 pic_rf_chip_select(0); // config mode ended 00216 pic_rf_chip_enable(1); 00217 00218 intcon.GIE = my_store_gie; 00219 }
void pic_rf_set_mode | ( | uns8 | mode | ) |
Pass RECEIVE_MODE or TRANSMIT_MODE to change current mode. Generally, you shouldn't need to call this routine. The library assumes you want to receive until you transmit, in which case it switches automatically to transmit mode and back to receive afterwards.
00179 { 00180 bit my_store_gie = intcon.GIE; 00181 kill_interrupts(); 00182 00183 make_output(rf_data_port, rf_data_pin); // make data pin output 00184 pic_rf_chip_enable(0); 00185 pic_rf_chip_select(1); // config mode 00186 00187 change_pin(rf_data_port, rf_data_pin, mode); // send a zero 00188 set_pin(rf_clk1_port, rf_clk1_pin); // clock it in (positive edge) 00189 clear_pin(rf_clk1_port, rf_clk1_pin); // ready for next bit 00190 00191 pic_rf_chip_select(0); // config mode ended 00192 pic_rf_chip_enable(1); 00193 00194 rf_current_mode_receive = mode; 00195 00196 intcon.GIE = my_store_gie; 00197 }
void pic_rf_setup | ( | ) |
Setup ports and pins for communication with nRF24L01.
Set up ports and pins to correct input/output for communication with Nordif nrf2401a
00221 { 00222 00223 make_output(rf_data_port, rf_data_pin); // make data pin output 00224 make_output(rf_cs_port, rf_cs_pin); // make cs pin output 00225 make_output(rf_ce_port, rf_ce_pin); // make ce pin output 00226 make_input (rf_dr1_port, rf_dr1_pin); // make dr1 pin input 00227 make_output(rf_clk1_port, rf_clk1_pin); // make clk1 pin output 00228 00229 }
void pic_rf_transmit | ( | char * | data, | |
uns8 | bytes_to_transmit | |||
) |
Changes to transmit mode, clocks data into the nRF2401A and hits the shockburst button. Returns to receive mode when finished.
00154 { 00155 00156 uns8 byte_count, bit_count, temp; 00157 00158 bit my_store_gie = intcon.GIE; 00159 kill_interrupts(); 00160 00161 if (rf_current_mode_receive) { 00162 pic_rf_set_mode(TRANSMIT_MODE); 00163 } 00164 00165 make_output(rf_data_port, rf_data_pin); // make data pin output 00166 00167 pic_rf_send_bytes_inline(data, bytes_to_transmit); 00168 00169 pic_rf_chip_enable(0); // On low, enable shockburst 00170 00171 delay_ms(1); 00172 00173 pic_rf_set_mode(RECEIVE_MODE); // Go back to receive mode 00174 00175 intcon.GIE = my_store_gie; 00176 }