Hello world example for the nRF2401A Library

Dependencies:   mbed nRF2401A

Committer:
TheChrisyd
Date:
Sun Oct 06 16:19:52 2013 +0000
Revision:
3:34ae527e9d41
Parent:
2:440c95f796ac
Child:
4:aa6866d2014a
Receive callback corrected.

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 3:34ae527e9d41 8 Serial pc(USBTX, USBRX);
TheChrisyd 0:8fcb46c5fa63 9 DigitalOut myled(LED1);
TheChrisyd 3:34ae527e9d41 10
TheChrisyd 0:8fcb46c5fa63 11 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 12 nRF2401A rf1(p10, p11, p12, p13, p14);
TheChrisyd 0:8fcb46c5fa63 13 #endif
TheChrisyd 3:34ae527e9d41 14
TheChrisyd 0:8fcb46c5fa63 15 #ifdef RX
TheChrisyd 0:8fcb46c5fa63 16 nRF2401A rf2(p21, p22, p23, p24, p25);
TheChrisyd 0:8fcb46c5fa63 17
TheChrisyd 2:440c95f796ac 18 bool rx_recieved = false;
TheChrisyd 3:34ae527e9d41 19
TheChrisyd 3:34ae527e9d41 20 void nRF2401A_rx (void *arg)
TheChrisyd 2:440c95f796ac 21 {
TheChrisyd 2:440c95f796ac 22 rx_recieved = true;
TheChrisyd 2:440c95f796ac 23 }
TheChrisyd 2:440c95f796ac 24 #endif
TheChrisyd 3:34ae527e9d41 25 int main()
TheChrisyd 3:34ae527e9d41 26 {
TheChrisyd 0:8fcb46c5fa63 27 wait(0.005);
TheChrisyd 0:8fcb46c5fa63 28 pc.printf("Hello nRF2401A\n\r");
TheChrisyd 3:34ae527e9d41 29
TheChrisyd 0:8fcb46c5fa63 30 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 31 rf1.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 32 .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 33 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 34 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 35 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 36
TheChrisyd 3:34ae527e9d41 37 rf1.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 38 rf1.flushControlPacket();
TheChrisyd 3:34ae527e9d41 39
TheChrisyd 3:34ae527e9d41 40 nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
TheChrisyd 3:34ae527e9d41 41 uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
TheChrisyd 3:34ae527e9d41 42 uint32_t *msg32 = (uint32_t *) msg;
TheChrisyd 3:34ae527e9d41 43 #endif
TheChrisyd 0:8fcb46c5fa63 44
TheChrisyd 0:8fcb46c5fa63 45 #ifdef RX
TheChrisyd 0:8fcb46c5fa63 46 rf2.setDataPayloadLength(4 << 3)
TheChrisyd 0:8fcb46c5fa63 47 .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
TheChrisyd 0:8fcb46c5fa63 48 .setCRCMode(nRF2401A::NO_CRC)
TheChrisyd 0:8fcb46c5fa63 49 .setDataRate(nRF2401A::BIT_RATE_250KBITS)
TheChrisyd 0:8fcb46c5fa63 50 .setChannel(0x02);
TheChrisyd 0:8fcb46c5fa63 51
TheChrisyd 3:34ae527e9d41 52 rf2.printControlPacket(pc);
TheChrisyd 3:34ae527e9d41 53 rf2.flushControlPacket();
TheChrisyd 3:34ae527e9d41 54 rf2.attachRXHandler(&nRF2401A_rx, 0);
TheChrisyd 0:8fcb46c5fa63 55 #endif
TheChrisyd 0:8fcb46c5fa63 56
TheChrisyd 3:34ae527e9d41 57 while(1)
TheChrisyd 3:34ae527e9d41 58 {
TheChrisyd 3:34ae527e9d41 59
TheChrisyd 0:8fcb46c5fa63 60 #ifdef TX
TheChrisyd 0:8fcb46c5fa63 61 rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
TheChrisyd 0:8fcb46c5fa63 62 *msg32 += 1;
TheChrisyd 0:8fcb46c5fa63 63 #endif
TheChrisyd 3:34ae527e9d41 64
TheChrisyd 0:8fcb46c5fa63 65 myled = 1;
TheChrisyd 0:8fcb46c5fa63 66 wait(0.25);
TheChrisyd 3:34ae527e9d41 67
TheChrisyd 2:440c95f796ac 68 #ifdef RX
TheChrisyd 2:440c95f796ac 69 if (rx_recieved)
TheChrisyd 2:440c95f796ac 70 {
TheChrisyd 2:440c95f796ac 71 rf2.printDataPacket(pc);
TheChrisyd 2:440c95f796ac 72 rx_recieved = false;
TheChrisyd 2:440c95f796ac 73 }
TheChrisyd 3:34ae527e9d41 74 #endif
TheChrisyd 3:34ae527e9d41 75
TheChrisyd 0:8fcb46c5fa63 76 myled = 0;
TheChrisyd 0:8fcb46c5fa63 77 wait(0.25);
TheChrisyd 0:8fcb46c5fa63 78 }
TheChrisyd 0:8fcb46c5fa63 79 }