Hello world example for the nRF2401A Library

Dependencies:   mbed nRF2401A

Committer:
TheChrisyd
Date:
Fri Oct 04 19:56:17 2013 +0000
Revision:
2:440c95f796ac
Parent:
0:8fcb46c5fa63
Child:
3:34ae527e9d41
commit to share with Erik Olieman

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TheChrisyd 0:8fcb46c5fa63 1 #include "mbed.h"
TheChrisyd 0:8fcb46c5fa63 2 #include "nRF2401A.h"
TheChrisyd 0:8fcb46c5fa63 3
TheChrisyd 0:8fcb46c5fa63 4 // comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented.
TheChrisyd 0:8fcb46c5fa63 5 #define TX
TheChrisyd 0:8fcb46c5fa63 6 #define RX
TheChrisyd 0:8fcb46c5fa63 7
TheChrisyd 0:8fcb46c5fa63 8 DigitalOut myled(LED1);
TheChrisyd 2:440c95f796ac 9 DigitalOut debug(LED2);
TheChrisyd 0:8fcb46c5fa63 10 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 11 nRF2401A rf1(p10, p11, p12, p13, p14);
TheChrisyd 0:8fcb46c5fa63 12 #endif
TheChrisyd 0:8fcb46c5fa63 13 #ifdef RX
TheChrisyd 0:8fcb46c5fa63 14 nRF2401A rf2(p21, p22, p23, p24, p25);
TheChrisyd 0:8fcb46c5fa63 15 #endif
TheChrisyd 0:8fcb46c5fa63 16
TheChrisyd 0:8fcb46c5fa63 17 Serial pc(USBTX, USBRX);
TheChrisyd 2:440c95f796ac 18 #ifdef RX
TheChrisyd 2:440c95f796ac 19 bool rx_recieved = false;
TheChrisyd 2:440c95f796ac 20 nRF2401A_rx_handler_t nRF2401A_rx ()
TheChrisyd 2:440c95f796ac 21 {
TheChrisyd 2:440c95f796ac 22 //rf2.printDataPacket(pc);
TheChrisyd 2:440c95f796ac 23 debug = !debug;
TheChrisyd 2:440c95f796ac 24 rx_recieved = true;
TheChrisyd 2:440c95f796ac 25 return;
TheChrisyd 2:440c95f796ac 26 }
TheChrisyd 2:440c95f796ac 27 #endif
TheChrisyd 0:8fcb46c5fa63 28 int main() {
TheChrisyd 0:8fcb46c5fa63 29
TheChrisyd 0:8fcb46c5fa63 30 wait(0.005);
TheChrisyd 0:8fcb46c5fa63 31
TheChrisyd 0:8fcb46c5fa63 32 pc.printf("Hello nRF2401A\n\r");
TheChrisyd 0:8fcb46c5fa63 33 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 34 rf1.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 35 .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 36 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 37 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 38 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 39
TheChrisyd 2:440c95f796ac 40 // rf1.printControlPacket(pc);
TheChrisyd 0:8fcb46c5fa63 41
TheChrisyd 0:8fcb46c5fa63 42 #endif
TheChrisyd 0:8fcb46c5fa63 43 #ifdef RX
TheChrisyd 0:8fcb46c5fa63 44 rf2.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 45 .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 46 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 47 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 48 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 49
TheChrisyd 2:440c95f796ac 50 // rf2.printControlPacket(pc);
TheChrisyd 0:8fcb46c5fa63 51
TheChrisyd 2:440c95f796ac 52 rf2.attachRXHandler(nRF2401A_rx(), 0);
TheChrisyd 0:8fcb46c5fa63 53 #endif
TheChrisyd 0:8fcb46c5fa63 54 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 55 rf1.flushControlPacket();
TheChrisyd 0:8fcb46c5fa63 56 #endif
TheChrisyd 0:8fcb46c5fa63 57 #ifdef RX
TheChrisyd 0:8fcb46c5fa63 58 rf2.flushControlPacket();
TheChrisyd 0:8fcb46c5fa63 59 #endif
TheChrisyd 0:8fcb46c5fa63 60 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 61 nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
TheChrisyd 0:8fcb46c5fa63 62 uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
TheChrisyd 0:8fcb46c5fa63 63 uint32_t *msg32 = (uint32_t *) msg;
TheChrisyd 0:8fcb46c5fa63 64 #endif
TheChrisyd 0:8fcb46c5fa63 65
TheChrisyd 0:8fcb46c5fa63 66 while(1) {
TheChrisyd 0:8fcb46c5fa63 67 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 68 rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
TheChrisyd 0:8fcb46c5fa63 69 *msg32 += 1;
TheChrisyd 0:8fcb46c5fa63 70 #endif
TheChrisyd 0:8fcb46c5fa63 71 myled = 1;
TheChrisyd 0:8fcb46c5fa63 72 wait(0.25);
TheChrisyd 2:440c95f796ac 73 #ifdef RX
TheChrisyd 2:440c95f796ac 74 if (rx_recieved)
TheChrisyd 2:440c95f796ac 75 {
TheChrisyd 2:440c95f796ac 76 rf2.printDataPacket(pc);
TheChrisyd 2:440c95f796ac 77 rx_recieved = false;
TheChrisyd 2:440c95f796ac 78 }
TheChrisyd 2:440c95f796ac 79 #endif
TheChrisyd 0:8fcb46c5fa63 80 myled = 0;
TheChrisyd 0:8fcb46c5fa63 81 wait(0.25);
TheChrisyd 0:8fcb46c5fa63 82 }
TheChrisyd 0:8fcb46c5fa63 83 }