-
Notifications
You must be signed in to change notification settings - Fork 2
/
RFM69.h
126 lines (86 loc) · 2.63 KB
/
RFM69.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Basic Controls for RFM69 Transceiver using Continuous Mode via DIO2
#include <SPI.h>
struct RFM69 {
uint8_t csPin;
uint8_t resetPin;
//////////////////////////////////////
RFM69(uint8_t csPin, uint8_t resetPin) {
this->csPin=csPin;
this->resetPin=resetPin;
pinMode(csPin,OUTPUT);
digitalWrite(csPin,HIGH);
pinMode(resetPin,OUTPUT);
}
//////////////////////////////////////
void init(){
reset();
SPI.begin();
delay(100);
setRegister(0x02,0x68); // set continuous mode via DIO2 without bit synchronization, OOK modulation, no shaping
setRegister(0x11,0x5F); // disable PA0 and enable PA1 instead - this is required (though undocumented!) to use DIO2 as continuous mode input
if(getRegister(0x02)==0x68 && getRegister(0x11)==0x5F)
Serial.print("Found");
else
Serial.print("*** Warning. Can't find");
Serial.print(" SPI Device with Chip Select Pin=");
Serial.print(csPin);
Serial.print("\n\n");
}
//////////////////////////////////////
void reset(){
digitalWrite(resetPin,HIGH);
delay(10);
digitalWrite(resetPin,LOW);
delay(100);
}
//////////////////////////////////////
void setFrequency(double f){
uint32_t x=f/32.0*(1<<19)+0x87000000;
uint8_t b[4];
for(int i=0;i<4;i++)
b[i]=(x&(0xFF<<(24-i*8)))>>(24-i*8);
digitalWrite(csPin,LOW);
SPI.transfer(b,4);
digitalWrite(csPin,HIGH);
delay(10);
}
//////////////////////////////////////
void setRegister(uint8_t reg, uint8_t val){
if(reg<1 || reg>0x71){
Serial.print("*** Error: Invalid RFM69 register: \n");
return;
}
digitalWrite(csPin,LOW);
SPI.transfer(reg | 0x80);
SPI.transfer(val);
digitalWrite(csPin,HIGH);
delay(10);
}
//////////////////////////////////////
uint8_t getRegister(uint8_t reg){
uint8_t val;
digitalWrite(csPin,LOW);
SPI.transfer(reg);
val=SPI.transfer(0);
digitalWrite(csPin,HIGH);
return(val);
}
//////////////////////////////////////
void printRegisters(uint8_t start=1, uint8_t count=1){
if(start==0) // can't start on FIFO register
start=1;
int maxCount=0x71-start+1; // 0x71 is last valid register
if(count>maxCount)
count=maxCount;
count++; // add one for address byte
uint8_t x[count]={start};
char c[16];
digitalWrite(csPin,LOW);
SPI.transfer(x,count);
digitalWrite(csPin,HIGH);
for(int i=1;i<count;i++){
sprintf(c,"%02X: %02X\n",i+start-1,x[i]);
Serial.print(c);
}
}
}; // RFM69