1 |
/********************************************* |
1 |
/********************************************* |
2 |
* |
2 |
* |
3 |
* RFM02 simple library |
3 |
* RFM02 simple library |
4 |
* |
4 |
* |
- |
|
5 |
* $HeadURL: file:///data/svnData/MLAB/Designs/duckweed_collector/SW/library/RF02/RF02.cpp $ |
5 |
* $Id: RF02.cpp 2128 2011-08-18 18:36:59Z kakl $ |
6 |
* $Id: RF02.cpp 2129 2011-08-18 18:40:43Z kakl $ |
6 |
* |
7 |
* |
7 |
* code based on the code of "benedikt k." and "contrechoc" |
8 |
* code based on the code of "benedikt k." and "contrechoc" |
8 |
* |
9 |
* |
9 |
* arduino 22 |
10 |
* arduino 22 |
10 |
* |
11 |
* |
11 |
*********************************************/ |
12 |
*********************************************/ |
12 |
|
13 |
|
13 |
|
14 |
|
14 |
#include <avr/io.h> |
15 |
#include <avr/io.h> |
15 |
#include <avr/interrupt.h> |
16 |
#include <avr/interrupt.h> |
16 |
#include <stdlib.h> |
17 |
#include <stdlib.h> |
17 |
#include <avr/pgmspace.h> |
18 |
#include <avr/pgmspace.h> |
18 |
#include <avr/eeprom.h> |
19 |
#include <avr/eeprom.h> |
19 |
|
20 |
|
20 |
#include <string.h> |
21 |
#include <string.h> |
21 |
|
22 |
|
22 |
#include "rf02.h" |
23 |
#include "rf02.h" |
23 |
#include <util/delay.h> |
24 |
#include <util/delay.h> |
24 |
|
25 |
|
25 |
#define F_CPU 16000000UL |
26 |
#define F_CPU 16000000UL |
26 |
|
27 |
|
27 |
#define RF_PORT PORTB |
28 |
#define RF_PORT PORTB |
28 |
#define RF_DDR DDRB |
29 |
#define RF_DDR DDRB |
29 |
#define RF_PIN PINB |
30 |
#define RF_PIN PINB |
30 |
|
31 |
|
31 |
#define SDI 0 // SDI, -> RF02 Atmega PB0 Arduino 8 |
32 |
#define SDI 0 // SDI, -> RF02 Atmega PB0 Arduino 8 |
32 |
#define SCK 1 // SCK, -> RF02 Atmega PB1 Arduino 9 |
33 |
#define SCK 1 // SCK, -> RF02 Atmega PB1 Arduino 9 |
33 |
#define CS 2 // nSEL, -> RF02 Atmega PB2 Arduino 10 |
34 |
#define CS 2 // nSEL, -> RF02 Atmega PB2 Arduino 10 |
34 |
#define IRQ 4 // nIRQ, <- RF02 Atmega PB4 Arduino 12 |
35 |
#define IRQ 4 // nIRQ, <- RF02 Atmega PB4 Arduino 12 |
35 |
//------------------// FSK: Pullupto VCC |
36 |
//------------------// FSK: Pullupto VCC |
36 |
|
37 |
|
37 |
#ifndef cbi |
38 |
#ifndef cbi |
38 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
39 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
39 |
#endif |
40 |
#endif |
40 |
#ifndef sbi |
41 |
#ifndef sbi |
41 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
42 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
42 |
#endif |
43 |
#endif |
43 |
|
44 |
|
44 |
|
45 |
|
45 |
unsigned char test[32]=" 55555 \n"; |
46 |
unsigned char test[32]=" 55555 \n"; |
46 |
|
47 |
|
47 |
void rf02_changeText( unsigned char* ptr, uint8_t number) |
48 |
void rf02_changeText( unsigned char* ptr, uint8_t number) |
48 |
{ |
49 |
{ |
49 |
|
50 |
|
50 |
if (number> 32)number = 32; |
51 |
if (number> 32)number = 32; |
51 |
memcpy( test, ptr, number); |
52 |
memcpy( test, ptr, number); |
52 |
|
53 |
|
53 |
} |
54 |
} |
54 |
|
55 |
|
55 |
void rf02_prepAll() |
56 |
void rf02_prepAll() |
56 |
{ |
57 |
{ |
57 |
RF_PORT=(1<<CS); |
58 |
RF_PORT=(1<<CS); |
58 |
RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS); |
59 |
RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS); |
59 |
|
60 |
|
60 |
sbi(RF_PORT, FSK); |
61 |
sbi(RF_PORT, FSK); |
61 |
|
62 |
|
62 |
|
63 |
|
63 |
for (unsigned char i=0; i<15; i++) _delay_ms(10); // wait until POR done |
64 |
for (unsigned char i=0; i<15; i++) _delay_ms(10); // wait until POR done |
64 |
|
65 |
|
65 |
|
66 |
|
66 |
rf02_trans(0xCC00); //*// |
67 |
rf02_trans(0xCC00); //*// |
67 |
|
68 |
|
68 |
// rf02_trans(0x8B61); //*// |
69 |
// rf02_trans(0x8B61); //*// |
69 |
rf02_trans(0x8000|0x1000|0x70|0x02); //*// |
70 |
rf02_trans(0x8000|0x1000|0x70|0x02); //*// |
70 |
rf02_trans(0xA000|0x640); //*// |
71 |
rf02_trans(0xA000|0x640); //*// |
71 |
// rf02_trans(0xA640); ///= 434 MHz |
72 |
// rf02_trans(0xA640); ///= 434 MHz |
72 |
|
73 |
|
73 |
// rf02_trans(0xD2C0); // 33% PLL current |
74 |
// rf02_trans(0xD2C0); // 33% PLL current |
74 |
// rf02_trans(0xC823); // 9600 Bd |
75 |
// rf02_trans(0xC823); // 9600 Bd |
75 |
rf02_trans(0xD040); // RATE/2 |
76 |
rf02_trans(0xD040); // RATE/2 |
76 |
rf02_trans(0xC811); // 19200 => 9600 Bd |
77 |
rf02_trans(0xC811); // 19200 => 9600 Bd |
77 |
|
78 |
|
78 |
rf02_trans(0xC0E0); // power settings |
79 |
rf02_trans(0xC0E0); // power settings |
79 |
|
80 |
|
80 |
rf02_trans(0xC220); //0xC2A0 |
81 |
rf02_trans(0xC220); //0xC2A0 |
81 |
// rf02_trans(0xB100); |
82 |
// rf02_trans(0xB100); |
82 |
// rf02_trans(0x8F83); |
83 |
// rf02_trans(0x8F83); |
83 |
//!!! |
84 |
//!!! |
84 |
// rf02_trans(0xC001); |
85 |
// rf02_trans(0xC001); |
85 |
|
86 |
|
86 |
|
87 |
|
87 |
} |
88 |
} |
88 |
|
89 |
|
89 |
void rf02_sendData() |
90 |
void rf02_sendData() |
90 |
{ |
91 |
{ |
91 |
|
92 |
|
92 |
//!!! rf02_txdata( test, sizeof test); |
93 |
//!!! rf02_txdata( test, sizeof test); |
93 |
rf02_txdata( test, 23); |
94 |
rf02_txdata( test, 23); |
94 |
} |
95 |
} |
95 |
|
96 |
|
96 |
void rf02_trans(unsigned short value) |
97 |
void rf02_trans(unsigned short value) |
97 |
{ |
98 |
{ |
98 |
uint8_t i; |
99 |
uint8_t i; |
99 |
|
100 |
|
100 |
cbi(RF_PORT, CS); |
101 |
cbi(RF_PORT, CS); |
101 |
|
102 |
|
102 |
for (i=0; i<16; i++) |
103 |
for (i=0; i<16; i++) |
103 |
{ if (value&0x8000) //0x8000 |
104 |
{ if (value&0x8000) //0x8000 |
104 |
sbi(RF_PORT, SDI); |
105 |
sbi(RF_PORT, SDI); |
105 |
else |
106 |
else |
106 |
cbi(RF_PORT, SDI); |
107 |
cbi(RF_PORT, SDI); |
107 |
|
108 |
|
108 |
sbi(RF_PORT, SCK); |
109 |
sbi(RF_PORT, SCK); |
109 |
value<<=1; |
110 |
value<<=1; |
110 |
_delay_us(0.3); |
111 |
_delay_us(0.3); |
111 |
cbi(RF_PORT, SCK); |
112 |
cbi(RF_PORT, SCK); |
112 |
} |
113 |
} |
113 |
sbi(RF_PORT, CS); |
114 |
sbi(RF_PORT, CS); |
114 |
} |
115 |
} |
115 |
|
116 |
|
116 |
|
117 |
|
117 |
void rf02_txdata( unsigned char * data, uint8_t number) |
118 |
void rf02_txdata( unsigned char * data, uint8_t number) |
118 |
{ |
119 |
{ |
119 |
uint8_t i,value; |
120 |
uint8_t i,value; |
120 |
value=0xC6; //1100 0110 |
121 |
value=0xC6; //1100 0110 |
121 |
cbi(RF_PORT, CS); //nSel |
122 |
cbi(RF_PORT, CS); //nSel |
122 |
|
123 |
|
123 |
//!!!! |
124 |
//!!!! |
124 |
// rf02_trans(0xC039); // TX start |
125 |
// rf02_trans(0xC039); // TX start |
125 |
|
126 |
|
126 |
|
127 |
|
127 |
for (i=0; i<8; i++) |
128 |
for (i=0; i<8; i++) |
128 |
{ if (value&0x80) //1000 0000 = 80 |
129 |
{ if (value&0x80) //1000 0000 = 80 |
129 |
sbi(RF_PORT, SDI); |
130 |
sbi(RF_PORT, SDI); |
130 |
else |
131 |
else |
131 |
cbi(RF_PORT, SDI); |
132 |
cbi(RF_PORT, SDI); |
132 |
|
133 |
|
133 |
sbi(RF_PORT, SCK); |
134 |
sbi(RF_PORT, SCK); |
134 |
value<<=1; |
135 |
value<<=1; |
135 |
_delay_us(0.2); |
136 |
_delay_us(0.2); |
136 |
cbi(RF_PORT, SCK); |
137 |
cbi(RF_PORT, SCK); |
137 |
} |
138 |
} |
138 |
|
139 |
|
139 |
rf02_shiftout(0xAA);//10101010 |
140 |
rf02_shiftout(0xAA);//10101010 |
140 |
rf02_shiftout(0xAA); |
141 |
rf02_shiftout(0xAA); |
141 |
rf02_shiftout(0xAA); |
142 |
rf02_shiftout(0xAA); |
142 |
rf02_shiftout(0x2D);//00101101 |
143 |
rf02_shiftout(0x2D);//00101101 |
143 |
rf02_shiftout(0xD4);//11010100 |
144 |
rf02_shiftout(0xD4);//11010100 |
144 |
|
145 |
|
145 |
// no checkbit, in experimenting some letters were transmitted wrong! |
146 |
// no checkbit, in experimenting some letters were transmitted wrong! |
146 |
|
147 |
|
147 |
for (i=0; i<number; i++) |
148 |
for (i=0; i<number; i++) |
148 |
rf02_shiftout(*data++); |
149 |
rf02_shiftout(*data++); |
149 |
|
150 |
|
150 |
sbi(RF_PORT, CS); |
151 |
sbi(RF_PORT, CS); |
151 |
while(RF_PIN&(1<<IRQ)); // wait until transfer done |
152 |
while(RF_PIN&(1<<IRQ)); // wait until transfer done |
152 |
rf02_trans(0xC464); // TX off after 10us |
153 |
rf02_trans(0xC464); // TX off after 10us |
153 |
//rf02_trans(0xC001); // TX close |
154 |
//rf02_trans(0xC001); // TX close |
154 |
_delay_ms(10); |
155 |
_delay_ms(10); |
155 |
} |
156 |
} |
156 |
|
157 |
|
157 |
void rf02_shiftout(unsigned char value) |
158 |
void rf02_shiftout(unsigned char value) |
158 |
{ uint8_t j; |
159 |
{ uint8_t j; |
159 |
for (j=0; j<8; j++) |
160 |
for (j=0; j<8; j++) |
160 |
{ while(RF_PIN&(1<<IRQ)); |
161 |
{ while(RF_PIN&(1<<IRQ)); |
161 |
while(!(RF_PIN&(1<<IRQ))); |
162 |
while(!(RF_PIN&(1<<IRQ))); |
162 |
|
163 |
|
163 |
if (value&128) //100101000 |
164 |
if (value&128) //100101000 |
164 |
{ |
165 |
{ |
165 |
sbi(RF_PORT, SDI); |
166 |
sbi(RF_PORT, SDI); |
166 |
} |
167 |
} |
167 |
else |
168 |
else |
168 |
{ |
169 |
{ |
169 |
cbi(RF_PORT, SDI); |
170 |
cbi(RF_PORT, SDI); |
170 |
} |
171 |
} |
171 |
value<<=1; |
172 |
value<<=1; |
172 |
} |
173 |
} |
173 |
} |
174 |
} |
174 |
|
175 |
|