1 | #include "SPI.h"
|
2 |
|
3 |
|
4 | unsigned short int spiTransfer16(unsigned char nSEL, unsigned short int a)
|
5 | {
|
6 | unsigned short int ret = 0;
|
7 | digitalWrite(nSEL, LOW); //chip select low
|
8 | ret = SPI.transfer16(a);
|
9 | digitalWrite(nSEL, HIGH); //chip select high
|
10 | delay(1); //wait 1 ms before transmitting the next word
|
11 | return ret;
|
12 | }
|
13 |
|
14 | unsigned char spiTransfer(unsigned char nSEL, unsigned char a)
|
15 | {
|
16 | unsigned char ret = 0;
|
17 | digitalWrite(nSEL, LOW); //chip select low
|
18 | ret = SPI.transfer(a);
|
19 | digitalWrite(nSEL, HIGH); //chip select high
|
20 | delayMicroseconds(20); //wait 20us before transmitting the next word
|
21 | return ret;
|
22 | }
|
23 |
|
24 | class RFM01
|
25 | {
|
26 | private:
|
27 | int nSel = 0;
|
28 | int nIRQ = 0;
|
29 |
|
30 | char receivedChar = '0';
|
31 | unsigned short int output[10];
|
32 |
|
33 | public:
|
34 | RFM01(int nSelPin, int nIRQPin)
|
35 | {
|
36 | nSel = nSelPin;
|
37 | nIRQ = nIRQPin;
|
38 | }
|
39 |
|
40 | void init( void )
|
41 | {
|
42 | pinMode(nSel, OUTPUT);
|
43 | digitalWrite(nSel, HIGH);
|
44 | pinMode(nIRQ, INPUT);
|
45 |
|
46 |
|
47 | SPI.begin();
|
48 |
|
49 | SPI.beginTransaction(SPISettings(140000, MSBFIRST, SPI_MODE0));
|
50 | output[0] = spiTransfer16(nSel, 0x0000); //initial transfer -status read
|
51 | output[1] = spiTransfer16(nSel, 0x89FB); //config setting - 433MHz, crystal active, 16pF crystal CAP, 134kHz Baseband Bandwidth, module on
|
52 | output[2] = spiTransfer16(nSel, 0xA640); //434Mhz center freq
|
53 | output[3] = spiTransfer16(nSel, 0xC847); //2.4kbps
|
54 | output[4] = spiTransfer16(nSel, 0xC69B); //AFC
|
55 | output[5] = spiTransfer16(nSel, 0xC42A); //clock recovery manual ctl, digital filter, DQD = 4
|
56 | output[6] = spiTransfer16(nSel, 0xCE84); //use FIFO
|
57 | output[7] = spiTransfer16(nSel, 0xCE87); //sync
|
58 | output[8] = spiTransfer16(nSel, 0xC081); //activate rx, 0db gain, enable
|
59 | SPI.endTransaction();
|
60 | }
|
61 |
|
62 | bool hasData( void )
|
63 | {
|
64 | if (!digitalRead(nIRQ))
|
65 | {
|
66 | return true;
|
67 | }
|
68 |
|
69 | return false;
|
70 | }
|
71 |
|
72 | unsigned char read8( void )
|
73 | {
|
74 | SPI.beginTransaction(SPISettings(140000, MSBFIRST, SPI_MODE0));
|
75 | output[0] = spiTransfer16(nSel, 0x0000); //read status byte
|
76 | output[1] = spiTransfer(nSel, 0x00); //read input byte
|
77 | output[2] = spiTransfer(nSel, 0x00); //read input byte
|
78 | output[3] = spiTransfer(nSel, 0x00); //read input byte
|
79 | output[4] = spiTransfer(nSel, 0x00); //read input byte
|
80 | output[5] = spiTransfer(nSel, 0x00); //read input byte
|
81 | output[6] = spiTransfer(nSel, 0x00); //read input byte
|
82 | output[7] = spiTransfer(nSel, 0x00); //read input byte
|
83 | output[8] = spiTransfer16(nSel, 0xCE84); //use FIFO
|
84 | output[9] = spiTransfer16(nSel, 0xCE87); //sync
|
85 | SPI.endTransaction();
|
86 |
|
87 | receivedChar = (char) output[0];
|
88 |
|
89 | return (unsigned char) output[0];
|
90 | }
|
91 |
|
92 | void putOutOutPut( int s )
|
93 | {
|
94 | for (int i = 0; i < s; i++)
|
95 | {
|
96 | Serial.print((output[i]&0xFF00)>>8);
|
97 | Serial.print("|");
|
98 | Serial.println(output[i]&0x00FF);
|
99 | }
|
100 | }
|
101 |
|
102 | char getReceivedChar( void )
|
103 | {
|
104 | return receivedChar;
|
105 | }
|
106 | };
|
107 |
|
108 | class RFM02
|
109 | {
|
110 | private:
|
111 |
|
112 | int nSel = 0;
|
113 | int nIRQ = 0;
|
114 | int FSK = 0;
|
115 |
|
116 | unsigned short int output[10];
|
117 |
|
118 | public:
|
119 |
|
120 | RFM02(int nSelPin, int nIRQPin, int FSKPin)
|
121 | {
|
122 | nSel = nSelPin;
|
123 | nIRQ = nIRQPin;
|
124 | FSK = FSKPin;
|
125 | }
|
126 |
|
127 | void init( void )
|
128 | {
|
129 | pinMode(nSel, OUTPUT);
|
130 | digitalWrite(nSel, HIGH);
|
131 | pinMode(nIRQ, INPUT);
|
132 | pinMode(FSK, OUTPUT);
|
133 | digitalWrite(FSK, LOW);
|
134 |
|
135 | SPI.begin();
|
136 |
|
137 | SPI.beginTransaction(SPISettings(140000, MSBFIRST, SPI_MODE0));
|
138 | output[0] = spiTransfer16(nSel, 0xCC00); //initial transfer - read status register
|
139 | output[1] = spiTransfer16(nSel, 0x8B81); //433MHz Freqency band, clock frequency = 2 MHz, big crystal cap,
|
140 | output[2] = spiTransfer16(nSel, 0xA640); //434Mhz center freq
|
141 | output[3] = spiTransfer16(nSel, 0xC823); //data rate = 9578,5 bps
|
142 | output[4] = spiTransfer16(nSel, 0xD040); //RATE/2 //measured rate 2.4kbps
|
143 | output[5] = spiTransfer16(nSel, 0xC220); //tx bit sync on
|
144 | output[6] = spiTransfer16(nSel, 0xC001); //pwer management command
|
145 |
|
146 | SPI.endTransaction();
|
147 | }
|
148 |
|
149 | void putByte(unsigned char c)
|
150 | {
|
151 | unsigned char i = 0;
|
152 | for (i = 0; i < 8; i ++)
|
153 | {
|
154 | while (!digitalRead(nIRQ));
|
155 | while (digitalRead(nIRQ));
|
156 | if (c & 0x80)
|
157 | {
|
158 | digitalWrite(FSK, HIGH);
|
159 | } else {
|
160 | digitalWrite(FSK, LOW);
|
161 | }
|
162 | c = c<<1;
|
163 | }
|
164 | }
|
165 |
|
166 | void send8(unsigned char c)
|
167 | {
|
168 | output[0] = spiTransfer16(nSel, 0xC039); //Power mgmt, activate crystal, synthesizer, power amp
|
169 |
|
170 | putByte(0xAA); //preamble
|
171 | putByte(0xAA); //preamble
|
172 | putByte(0xAA); //preamble
|
173 | putByte(0x2D); //sync word
|
174 | putByte(0xD4); //sync word
|
175 | putByte(c); //data byte
|
176 | putByte(c); //data byte
|
177 | putByte(0xAA); //schlusswort
|
178 | delayMicroseconds(1); // 1us delay
|
179 | output[2] = spiTransfer16(nSel, 0xC001); //deactivate tx
|
180 | }
|
181 |
|
182 | void putOutOutPut( int s )
|
183 | {
|
184 | for (int i = 0; i < s; i++)
|
185 | {
|
186 | Serial.println(output[i]);
|
187 | }
|
188 | }
|
189 | };
|
190 |
|
191 | #define DATASIZE 50
|
192 |
|
193 | RFM01 rfm01(44, 18);
|
194 | RFM02 rfm02(45, 19, 43);
|
195 |
|
196 | char inputData[DATASIZE];
|
197 | int inputDataIterator = 0;
|
198 |
|
199 | void setup() {
|
200 | delay(200); //rfm modules need at least 150ms on start up before being talked to over spi
|
201 | Serial.begin(57600);
|
202 | Serial.println("Serial is working");
|
203 |
|
204 | Serial.println("init rfm01...");
|
205 | rfm01.init();
|
206 |
|
207 | Serial.println("output:");
|
208 | rfm01.putOutOutPut(9);
|
209 |
|
210 | Serial.println("init rfm02...");
|
211 | rfm02.init();
|
212 |
|
213 | Serial.println("output:");
|
214 | rfm01.putOutOutPut(7);
|
215 |
|
216 | }
|
217 |
|
218 | void loop() {
|
219 | unsigned char input = 0;
|
220 |
|
221 | if (rfm01.hasData())
|
222 | {
|
223 | Serial.print("rfm01 data available: ");
|
224 | input = rfm01.read8();
|
225 | delay(5);
|
226 | Serial.println(input);
|
227 | Serial.println("rfm01 output:");
|
228 | rfm01.putOutOutPut(10);
|
229 | inputData[inputDataIterator] = rfm01.getReceivedChar();
|
230 | if ((inputDataIterator++) == DATASIZE)
|
231 | {
|
232 | inputDataIterator = 0;
|
233 | Serial.println("Received Data as string:");
|
234 | for (int u = 0; u< DATASIZE; u++)
|
235 | {
|
236 | Serial.print(inputData[u]);
|
237 | }
|
238 | Serial.println("");
|
239 | }
|
240 |
|
241 | }
|
242 |
|
243 | if (Serial.available())
|
244 | {
|
245 | Serial.println("rfm02 sending something");
|
246 | Serial.read();
|
247 | rfm02.send8(127);
|
248 | Serial.println("output:");
|
249 | rfm02.putOutOutPut(2);
|
250 | }
|
251 |
|
252 | }
|