Exemplo n.º 1
0
int main()
{
	uint8_t addr[5];
	uint8_t buf[32];

	WDTCTL = WDTHOLD | WDTPW;
	DCOCTL = CALDCO_16MHZ;
	BCSCTL1 = CALBC1_16MHZ;
	BCSCTL2 = DIVS_2;  // SMCLK = DCOCLK/4
	// SPI (USCI) uses SMCLK, prefer SMCLK < 10MHz (SPI speed limit for nRF24 = 10MHz)
	user = 0xFE;

	// Red LED will be our output
	P1DIR |= BIT0;
	P1OUT &= ~BIT0;

	/* Initial values for nRF24L01+ library config variables */
	rf_crc = RF24_EN_CRC; // CRC enabled, 8-bit
	rf_addr_width      = 5;
	rf_speed_power     = RF24_SPEED_MIN | RF24_POWER_MAX;
	rf_channel         = 120;
	msprf24_init();
	msprf24_set_pipe_packetsize(0, 0);
	msprf24_open_pipe(0, 1);  // Open pipe#0 with Enhanced ShockBurst

	// Set our RX address
	memcpy(addr, "\xDE\xAD\xBE\xEF\x01", 5);
	w_rx_addr(0, addr);

	// Receive mode
	if (!(RF24_QUEUE_RXEMPTY & msprf24_queue_state())) {
		flush_rx();
	}
	msprf24_activate_rx();
	LPM4;

	while (1) {
		if (rf_irq & RF24_IRQ_FLAGGED) {
			msprf24_get_irq_reason();
		}
		if (rf_irq & RF24_IRQ_RX || msprf24_rx_pending()) {
			r_rx_payload(r_rx_peek_payload_size(), buf);
			msprf24_irq_clear(RF24_IRQ_RX);
			user = buf[0];

			if (buf[0] == '0')
				P1OUT &= ~BIT0;
			if (buf[0] == '1')
				P1OUT |= BIT0;

		} else {
			user = 0xFF;
		}
		LPM4;
	}
	return 0;
}
Exemplo n.º 2
0
int main() {
  uint8_t buf[32];

  WDTCTL = WDTPW | WDTHOLD;
  DCOCTL = 0;
  BCSCTL1 = CALBC1_16MHZ;
  DCOCTL = CALDCO_16MHZ;
  BCSCTL3 |= LFXT1S_2;

  P1DIR |= BIT0;
  P1OUT &= ~BIT0;

  _BIS_SR(GIE);
#ifdef UART_DEBUG
  uart_setup();
#endif
  radio_setup();

  // flush RX just in case
  if (!(RF24_RX_EMPTY & msprf24_queue_state())) {
    flush_rx();
  }
  msprf24_activate_rx();
  LPM4;

  while (1) {
    if (rf_irq & RF24_IRQ_FLAGGED) {
      rf_irq &= ~RF24_IRQ_FLAGGED;
      msprf24_get_irq_reason();
    }
    if (rf_irq & RF24_IRQ_RX || msprf24_rx_pending()) {
      uint8_t nextPayloadSize = r_rx_peek_payload_size();
      r_rx_payload(nextPayloadSize, buf);
      msprf24_irq_clear(RF24_IRQ_MASK);
      memcpy(dht_data.bytes, buf, nextPayloadSize);
#ifdef UART_DEBUG
      int t = (((dht_data.val.th&0x7f)<<8) + dht_data.val.tl)*((dht_data.val.th&0x80)?-1:1);
      int h = (dht_data.val.hh<<8) + (dht_data.val.hl);
      uart_send(sprintf(txbuf, "%d.%1d;%d.%1d\r\n", t/10, t%10, h/10, h%10));
#endif
    }
    LPM4;
  }

  return 0;
}
Exemplo n.º 3
0
int main(){
    uint8_t txretry_count=0, txretry_latch=0;

    WDTCTL = (WDTPW + WDTHOLD);
    DCOCTL = CALDCO_16MHZ;
    BCSCTL1 = CALBC1_16MHZ;
    BCSCTL2 = DIVS_1;  // SMCLK = 8MHz

    // Inicializacao do transceiver
    rf_crc = RF24_EN_CRC;
    rf_addr_width = 5;
    rf_speed_power = RF24_SPEED_MIN | RF24_POWER_MAX;
    rf_channel = 8;
    msprf24_init();
    msprf24_open_pipe(0, 1);  // This is only for receiving auto-ACK packets.
    msprf24_set_pipe_packetsize(0, 0);  // Dynamic payload support
        // Note: Pipe#0 is hardcoded in the transceiver hardware as the designated "pipe" for a TX node to receive
        // auto-ACKs.  This does not have to match the pipe# used on the RX side.

    Serial_config();

    // Main loop
    while (1) {
        // Handle any nRF24 IRQs first
        if (rf_irq & RF24_IRQ_FLAGGED) {
            msprf24_get_irq_reason();

            if (rf_irq & RF24_IRQ_TX) {
                msprf24_irq_clear(RF24_IRQ_TX);
                flush_tx();
                w_rx_addr(0, (uint8_t*)masterAddr);
                msprf24_powerdown();
            }

            if (rf_irq & RF24_IRQ_TXFAILED) {
                msprf24_irq_clear(RF24_IRQ_TXFAILED);
                if (txretry_count) {
                    txretry_count--;
                    if (!txretry_latch) {
                        txretry_latch = 1;
                        tx_reuse_lastpayload();
                    }
                    pulse_ce();
                } else {
                    // Ran out of retries, give up
                    flush_tx();
                    w_rx_addr(0, (uint8_t*)masterAddr);
                    txretry_latch = 0;
                    msprf24_powerdown();
                }
            }
            // No need to handle RX packets since we never enter RX mode
        }

        if (handle_serial){
            if(serial_data_rcv == START_CHAR){
                pl_index == 0;
            }
            else if(serial_data_rcv == END_CHAR){
                send_pkg = true;
                pl_index == 0;
            }
            else if(pl_index < PL_SIZE){
                rfpayload[pl_index++] = serial_data_rcv;
            }
            else{
                pl_index = 0;
            }

            handle_serial = false;
        }

        if (msprf24_queue_state() & RF24_QUEUE_TXEMPTY && send_pkg) {

            Serial_escreve_texto("\nEnviando pacote: ");
            Serial_escreve_texto(rfpayload);

            msprf24_standby();
            w_tx_addr((uint8_t*)nodeAddr);
            w_rx_addr(0, (uint8_t*)nodeAddr);  // To catch the auto-ACK reply
            w_tx_payload(2, (uint8_t*)rfpayload);
            msprf24_activate_tx();
            txretry_count = 20;  // Try damned hard to send this, retrying up to 20 * ARC times (ARC=15 by default)

            send_pkg = false;
        }

    }
}