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