-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReceiveWithInterrupt.ino
71 lines (60 loc) · 1.86 KB
/
ReceiveWithInterrupt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Receive raw data via interrupt using the MRF24J40 module.
*
* The INT pin from MRF24J40 must be connected to the INT1 pin in the ATmega328 (Arduino D3 pin).
*
* Copyright (c) 2015 Circuitar
* All rights reserved.
*
* This software is released under a BSD license. See the attached LICENSE file for details.
*/
#include <SPI.h>
#include <Nanoshield_MRF.h>
#include <util/atomic.h>
// Create wireless module object (MRF24J40MA/B/C/D/E)
Nanoshield_MRF mrf/*(MRF24J40MA)*/; // Make sure to select the right module
// Indicates a packet has been received
volatile bool received = false;
void setup() {
Serial.begin(9600);
// Initialize module
mrf.begin();
mrf.setAddress(2); // Network address
mrf.setCoordinator(true); // Module is coordinator
// Check reception for the first time
receive();
// Get packet from module when there is an interrupt on pin INT1 (D3 on Arduino)
attachInterrupt(1, receive, FALLING);
}
void loop() {
// Check if packet was received (variables shared with ISR must be read atomically)
bool ready;
ATOMIC_BLOCK(ATOMIC_FORCEON) {
ready = received;
}
if (ready) {
// Print data length
Serial.print("Packet received: ");
Serial.print(mrf.bytesLeftToRead());
Serial.println(" bytes");
// Print data in hexadecimal format
while (mrf.bytesLeftToRead()) {
char buf[4];
sprintf(buf, "%02X ", mrf.read());
Serial.print(buf);
}
Serial.println();
Serial.print("Received Signal Power: ");
Serial.print(mrf.getSignalStrength(), 1);
Serial.print(" dBm\r\nLink Quality Indicator (LQI): ");
Serial.println(mrf.getLinkQuality());
Serial.println();
// Allow interrupt to continue receiving packets (write atomically)
ATOMIC_BLOCK(ATOMIC_FORCEON) {
received = false;
}
}
}
void receive() {
received = !received && mrf.receivePacket();
}