Advertisement
Guest User

Untitled

a guest
Oct 15th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. /*
  2.  * Project: nRF905 AVR/Arduino Library/Driver
  3.  * Author: Zak Kemble, contact@zakkemble.co.uk
  4.  * Copyright: (C) 2013 by Zak Kemble
  5.  * License: GNU GPL v3 (see License.txt)
  6.  * Web: http://blog.zakkemble.co.uk/nrf905-avrarduino-librarydriver/
  7.  */
  8.  
  9. /*
  10.  * Wait for data and reply.
  11.  *
  12.  * 7 -> CE
  13.  * 8 -> PWR
  14.  * 9 -> TXE
  15.  * 2 -> CD
  16.  * 3 -> DR
  17.  * 10 -> CSN
  18.  * 12 -> SO
  19.  * 11 -> SI
  20.  * 13 -> SCK
  21.  */
  22.  
  23. #include <nRF905.h>
  24. #include <SPI.h>
  25.  
  26. #define RXADDR {0x58, 0x6F, 0x2E, 0x10} // Address of this device (4 bytes)
  27. #define TXADDR {0xFE, 0x4C, 0xA6, 0xE5} // Address of device to send to (4 bytes)
  28.  
  29. void setup()
  30. {
  31.     // Start up
  32.     nRF905_init();
  33.    
  34.     // Set address of this device
  35.     byte addr[] = RXADDR;
  36.     nRF905_setRXAddress(addr);
  37.  
  38.     // Put into receive mode
  39.     nRF905_receive();
  40.  
  41.     Serial.begin(9600);
  42.  
  43.     Serial.println(F("Server started"));
  44. }
  45.  
  46. void loop()
  47. {
  48.     Serial.println(F("Waiting for ping..."));
  49.  
  50.     // Make buffer for data
  51.     byte buffer[NRF905_MAX_PAYLOAD];
  52.  
  53.     // Wait for data
  54.     while(!nRF905_getData(buffer, sizeof(buffer)));
  55.  
  56.     Serial.println(F("Got ping"));
  57.  
  58.     // Set address of device to send to
  59.     byte addr[] = TXADDR;
  60.     nRF905_setTXAddress(addr);
  61.  
  62.     // Set payload data (reply with data received)
  63.     nRF905_setData(buffer, sizeof(buffer));
  64.    
  65.     Serial.println(F("Sending reply..."));
  66.  
  67.     // Send payload (send fails if other transmissions are going on, keep trying until success)
  68.     while(!nRF905_send());
  69.  
  70.     // Put back into receive mode
  71.     nRF905_receive();
  72.  
  73.     Serial.println(F("Reply sent"));
  74.  
  75.     // Print out ping contents
  76.     Serial.print(F("Data: "));
  77.     Serial.write(buffer, sizeof(buffer));
  78.     Serial.println();
  79.  
  80.    
  81.     // Read config
  82.     byte config[10];
  83.     memset(config, 0, sizeof(config));
  84.    
  85.     digitalWrite(CSN, LOW);
  86.     SPI.transfer(NRF905_CMD_R_CONFIG);
  87.  
  88.     for(uint8_t i=0;i<sizeof(config);i++)
  89.         config[i] = SPI.transfer(NRF905_CMD_NOP);
  90.  
  91.     digitalWrite(CSN, HIGH);
  92.    
  93.     // Print out config
  94.     Serial.println("CONFIG: ");
  95.     for(uint8_t i=0;i<sizeof(config);i++)
  96.     {
  97.         Serial.print(config[i], HEX);
  98.         Serial.print(' ');
  99.     }
  100.     Serial.println();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement