//powers up the 24L01 with all necessary delays //this function allows the user to set the contents of the CONFIG register, but the function // sets the PWR_UP bit in the CONFIG register, so the user does not need to. //the argument rx_active_mode is only used if the user is setting up the // 24L01 as a receiver. If the argument is false, the receiver will remain in // standby mode and not monitor for packets. If the argument is true, the CE // pin will be set and the 24L01 will monitor for packets. In TX mode, the value // of this argument is insignificant. void nrf24l01_power_up_param(bool rx_active_mode, unsigned char config) { unsigned char test, test2; config |= nrf24l01_CONFIG_PWR_UP; nrf24l01_write_register(nrf24l01_CONFIG, &config, 1); delay_us(1500); if((config & nrf24l01_CONFIG_PRIM_RX) == 0) nrf24l01_clear_ce(); else { if(rx_active_mode != false) nrf24l01_set_ce(); else nrf24l01_clear_ce(); } }
//sets up the 24L01 as a receiver with all necessary delays //this function takes the existing contents of the CONFIG register and sets the PRIM_RX // bit in the CONFIG register. //if the argument rx_active_mode is false, the receiver will remain in standby mode // and not monitor for packets. If the argument is true, the CE pin will be set // and the 24L01 will monitor for packets. //note: if the read value of the CONFIG register already has the PRIM_RX bit set, this function // exits in order to not make an unecessary register write. void nrf24l01_set_as_rx(bool rx_active_mode) { unsigned char config; // unsigned char status; // status = nrf24l01_read_register(0, &config, 1); nrf24l01_read_register(0, &config, 1); if((config & nrf24l01_CONFIG_PRIM_RX) != 0) return; config |= nrf24l01_CONFIG_PRIM_RX; nrf24l01_write_register(nrf24l01_CONFIG, &config, 1); if(rx_active_mode != false) nrf24l01_set_ce(); else nrf24l01_clear_ce(); }
// Sends a data package to the default address. Be sure to send the correct // amount of bytes as configured as payload on the receiver. void nrf24l01_send_data(uint8_t* value) { // Wait until last packet is sent if in TX mode while (PTX) { uint8_t status = nrf24l01_read_register(NRF24L01_STATUS_REG); // Packet transmitted or Max retransmission if((status & (NRF24L01_TX_DS_BM | NRF24L01_MAX_RT_BM))){ PTX = 0; break; } } nrf24l01_write_register(NRF24L01_STATUS_REG, NRF24L01_TX_DS_BM | NRF24L01_MAX_RT_BM); ioport_set_pin_low(CONF_NRF24L01_CE_PIN); nrf24l01_primary_tx(); spi_select_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf); spi_write_single_packet(&CONF_NRF24L01_SPI, NRF24L01_W_TX_PAYLOAD); spi_write_packet(&CONF_NRF24L01_SPI, value, CONF_NRF24L01_PAYLOAD); spi_deselect_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf); ioport_set_pin_high(CONF_NRF24L01_CE_PIN); // Start transmission delay_us(Thce); delay_us(Tstby2a); }
void nrf24l01_set_rf_channel(uint8_t channel) { nrf24l01_write_register(NRF24L01_RF_CH_REG, channel); }
//clears only the MAX_RT interrupt void nrf24l01_irq_clear_max_rt() { unsigned char data = nrf24l01_STATUS_MAX_RT; nrf24l01_write_register(nrf24l01_STATUS, &data, 1); }
//clears only the TX_DS interrupt void nrf24l01_irq_clear_tx_ds() { unsigned char data = nrf24l01_STATUS_TX_DS; nrf24l01_write_register(nrf24l01_STATUS, &data, 1); }
//clears only the RX_DR interrupt void nrf24l01_irq_clear_rx_dr() { unsigned char data = nrf24l01_STATUS_RX_DR; nrf24l01_write_register(nrf24l01_STATUS, &data, 1); }
//clear all interrupts in the status register void nrf24l01_irq_clear_all() { unsigned char data = nrf24l01_STATUS_RX_DR | nrf24l01_STATUS_TX_DS | nrf24l01_STATUS_MAX_RT; nrf24l01_write_register(nrf24l01_STATUS, &data, 1); }
//clears the PLOS_CNT field of the OBSERVE_TX register //this function allows the user to set the RF_CH register by using // the argument in the function during the PLOS_CNT clearing process void nrf24l01_clear_plos_cnt_param(unsigned char rf_ch) { nrf24l01_write_register(nrf24l01_RF_CH, &rf_ch, 1); }
//sets the value of the CONFIG register void nrf24l01_set_config(unsigned char config) { nrf24l01_write_register(nrf24l01_CONFIG, &config, 1); }
//sets the TX address in the TX_ADDR register //unsigned char * address is the actual address to be used. It should be sized // according to the tx_addr length specified to the nrf24l01. //unsigned int len is the length of the address. Its value should be specified // according to the tx_addr length specified to the nrf24l01. void nrf24l01_set_tx_addr(unsigned char * address, unsigned int len) { nrf24l01_write_register(nrf24l01_TX_ADDR, address, len); }
//Arguments except opt_rx_standby_mode fill the actual register they are named // after. Registers that do not need to be initialized are not included here. //The argument opt_rx_active_mode is only used if the user is initializing the // 24L01 as a receiver. If the argument is false, the receiver will remain in // standby mode and not monitor for packets. If the argument is true, the CE // pin will be set and the 24L01 will monitor for packets. In TX mode, the value // of this argument is insignificant. //If the user wants to leave any 1-byte register in its default state, simply put // as that register's argument nrf24l01_<reg>_DEFAULT_VAL, where <reg> is the register // name. //If the user wants to leave any of the 5-byte registers RX_ADDR_P0, RX_ADDR_P1, or // TX_ADDR in its default state, simply put NULL in the argument for that address value. void nrf24l01_initialize(unsigned char config, unsigned char opt_rx_active_mode, unsigned char en_aa, unsigned char en_rxaddr, unsigned char setup_aw, unsigned char setup_retr, unsigned char rf_ch, unsigned char rf_setup, unsigned char * rx_addr_p0, unsigned char * rx_addr_p1, unsigned char rx_addr_p2, unsigned char rx_addr_p3, unsigned char rx_addr_p4, unsigned char rx_addr_p5, unsigned char * tx_addr, unsigned char rx_pw_p0, unsigned char rx_pw_p1, unsigned char rx_pw_p2, unsigned char rx_pw_p3, unsigned char rx_pw_p4, unsigned char rx_pw_p5) { unsigned char data[5]; data[0] = en_aa; nrf24l01_write_register(nrf24l01_EN_AA, data, 1); data[0] = en_rxaddr; nrf24l01_write_register(nrf24l01_EN_RXADDR, data, 1); data[0] = setup_aw; nrf24l01_write_register(nrf24l01_SETUP_AW, data, 1); data[0] = setup_retr; nrf24l01_write_register(nrf24l01_SETUP_RETR, data, 1); data[0] = rf_ch; nrf24l01_write_register(nrf24l01_RF_CH, data, 1); data[0] = rf_setup; nrf24l01_write_register(nrf24l01_RF_SETUP, data, 1); if(rx_addr_p0 != NULL) nrf24l01_set_rx_addr(rx_addr_p0, 5, 0); else { data[0] = nrf24l01_RX_ADDR_P0_B0_DEFAULT_VAL; data[1] = nrf24l01_RX_ADDR_P0_B1_DEFAULT_VAL; data[2] = nrf24l01_RX_ADDR_P0_B2_DEFAULT_VAL; data[3] = nrf24l01_RX_ADDR_P0_B3_DEFAULT_VAL; data[4] = nrf24l01_RX_ADDR_P0_B4_DEFAULT_VAL; nrf24l01_set_rx_addr(data, 5, 0); } if(rx_addr_p1 != NULL) nrf24l01_set_rx_addr(rx_addr_p1, 5, 1); else { data[0] = nrf24l01_RX_ADDR_P1_B0_DEFAULT_VAL; data[1] = nrf24l01_RX_ADDR_P1_B1_DEFAULT_VAL; data[2] = nrf24l01_RX_ADDR_P1_B2_DEFAULT_VAL; data[3] = nrf24l01_RX_ADDR_P1_B3_DEFAULT_VAL; data[4] = nrf24l01_RX_ADDR_P1_B4_DEFAULT_VAL; nrf24l01_set_rx_addr(data, 5, 1); } data[0] = rx_addr_p2; nrf24l01_set_rx_addr(data, 1, 2); data[0] = rx_addr_p3; nrf24l01_set_rx_addr(data, 1, 3); data[0] = rx_addr_p4; nrf24l01_set_rx_addr(data, 1, 4); data[0] = rx_addr_p5; nrf24l01_set_rx_addr(data, 1, 5); if(tx_addr != NULL) nrf24l01_set_tx_addr(tx_addr, 5); else { data[0] = nrf24l01_TX_ADDR_B0_DEFAULT_VAL; data[1] = nrf24l01_TX_ADDR_B1_DEFAULT_VAL; data[2] = nrf24l01_TX_ADDR_B2_DEFAULT_VAL; data[3] = nrf24l01_TX_ADDR_B3_DEFAULT_VAL; data[4] = nrf24l01_TX_ADDR_B4_DEFAULT_VAL; nrf24l01_set_tx_addr(data, 5); } data[0] = rx_pw_p0; nrf24l01_write_register(nrf24l01_RX_PW_P0, data, 1); data[0] = rx_pw_p1; nrf24l01_write_register(nrf24l01_RX_PW_P1, data, 1); data[0] = rx_pw_p2; nrf24l01_write_register(nrf24l01_RX_PW_P2, data, 1); data[0] = rx_pw_p3; nrf24l01_write_register(nrf24l01_RX_PW_P3, data, 1); data[0] = rx_pw_p4; nrf24l01_write_register(nrf24l01_RX_PW_P4, data, 1); data[0] = rx_pw_p5; nrf24l01_write_register(nrf24l01_RX_PW_P5, data, 1); if((config & nrf24l01_CONFIG_PWR_UP) != 0) nrf24l01_power_up_param(opt_rx_active_mode, config); else nrf24l01_power_down_param(config); }
void main(void) { unsigned int channel = CHANNEL; unsigned char data = 0x07; unsigned char t1; unsigned char t2; unsigned int tmpcount = 0; unsigned char wl_data[10]; unsigned char rs_line[20]; unsigned char rs_data[10]; unsigned char tmp_data[50]; // DBGU output configuration TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); // Configuration PIT (Periodic Interrupt Timer) ConfigurePit(); // Configuration TC (Timer Counter) ConfigureTc(); // Configuration PIO (Paralell In and Out port), Init Interrupt on PIO ConfigureButtons(); ConfigureLeds(); // Configuration Radio Module nRF24L (PIO and SPI), ConfigureButtons must be executed before ConfigureNRF24L(); ConfigureUSART0(); ConfigureUSART1(); while(Timer1Tick<2); // wait until NRF24L01 power up nrf24l01_power_up(True); while(Timer1Tick<4); // wait until NRF24L01 stand by Timer1Tick = 0; //initialize the 24L01 to the debug configuration as RX and auto-ack disabled nrf24l01_initialize_debug(True, nrf_TX_RX_SIZE, False); nrf24l01_write_register(0x06, &data, 1); nrf24l01_set_as_rx(True); Delay_US(130); nrf24l01_set_rf_ch(channel); nrf24l01_flush_rx(); Delay_US(300); reset_wl = 1; while (1) { if( nrf_Data > 0 ) { nrf_Data = 0; for( t1 = 0; t1<8; t1++ ) { wl_data[t1] = nrfRxMessage.Data[t1]; } LED_Toggle(LED_Green); writeByteSequence_8(wl_data); } if(messageUSART1){ messageUSART1 = 0; pmsgRead(tmp_data); while (tmp_data[tmpcount]!='\n'){ t1 = tmp_data[tmpcount]; tmpcount++; if( t1 >= '0' && t1 <= '9' ) { // If character is 0-9 convert it to num if( count < 20) { rs_line[count] = t1-'0'; count++; } } if( t1 >= 'A' && t1 <= 'F' ) { // If character A-F convert to 10-15 if( count < 20) { rs_line[count] = t1-'A'+10; count++; } } } // If character is a line break send packet for( count = 0; count <10; count++ ) { // Convert from 16*4 to 8*8 t1 = (rs_line[count*2])<<4; t2 = rs_line[count*2+1]; rs_data[count] = t1 | t2; } count = 0; tmpcount = 0; if( nrf_Transmission_Done == 1 ) { TX_packet_BASE(rs_data); // Send packet. LED_Toggle(LED_Yellow); } }//if msg flag has been raised }//while }//main