2112 |
kakl |
1 |
/********************************************* |
|
|
2 |
* |
|
|
3 |
* draft version v 0.1, experimental |
|
|
4 |
* |
|
|
5 |
* code based on the code of "benedikt k." |
|
|
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 |
* |
|
|
19 |
* |
|
|
20 |
*********************************************/ |
|
|
21 |
|
|
|
22 |
|
|
|
23 |
#include <avr/io.h> |
|
|
24 |
#include <avr/interrupt.h> |
|
|
25 |
#include <stdlib.h> |
|
|
26 |
#include <avr/pgmspace.h> |
|
|
27 |
#include <avr/eeprom.h> |
|
|
28 |
|
|
|
29 |
#include <string.h> |
|
|
30 |
|
|
|
31 |
#include "rf02.h" |
|
|
32 |
#include <util/delay.h> |
|
|
33 |
|
|
|
34 |
#define F_CPU 16000000UL |
|
|
35 |
|
|
|
36 |
#define RF_PORT PORTB |
|
|
37 |
#define RF_DDR DDRB |
|
|
38 |
#define RF_PIN PINB |
|
|
39 |
|
|
|
40 |
#define LED_PORT PORTD |
|
|
41 |
#define LED_DDR DDRD |
|
|
42 |
#define LED_PIN PIND |
|
|
43 |
|
|
|
44 |
#define LED0 4 -- PD4 |
|
|
45 |
#define LED1 2 -- PD2 |
|
|
46 |
|
|
|
47 |
#define SDI 0 // SDI, -> RF02 Atmega PB0 Arduino 8 |
|
|
48 |
#define SCK 1 // SCK, -> RF02 Atmega PB1 Arduino 9 |
|
|
49 |
#define CS 2 // nSEL, -> RF02 Atmega PB2 Arduino 10 |
|
|
50 |
#define IRQ 4 // nIRQ, <- RF02 Atmega PB4 Arduino 12 |
|
|
51 |
//------------------// FSK: Pullupto VCC |
|
|
52 |
|
|
|
53 |
#ifndef cbi |
|
|
54 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
|
|
55 |
#endif |
|
|
56 |
#ifndef sbi |
|
|
57 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
|
|
58 |
#endif |
|
|
59 |
|
|
|
60 |
#ifndef cLED0 |
|
|
61 |
#define cLED0() (LED_PORT &= ~(1<<LED0)) |
|
|
62 |
#endif |
|
|
63 |
#ifndef sLED0 |
|
|
64 |
#define sLED0() (LED_PORT |= (1<<LED0) ) |
|
|
65 |
#endif |
|
|
66 |
|
|
|
67 |
#ifndef cLED1 |
|
|
68 |
#define cLED1() (LED_PORT &= ~(1<<LED1)) |
|
|
69 |
#endif |
|
|
70 |
#ifndef sLED1 |
|
|
71 |
#define sLED1() (LED_PORT |= (1<<LED1) ) |
|
|
72 |
#endif |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
unsigned char test[32]=" 55555 \n"; |
|
|
77 |
|
|
|
78 |
void rf02_changeText( unsigned char* ptr, uint8_t number){ |
|
|
79 |
|
|
|
80 |
if (number> 32)number = 32; |
|
|
81 |
memcpy( test, ptr, number); |
|
|
82 |
|
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
void rf02_prepAll434(){ |
|
|
86 |
|
|
|
87 |
rf02_init(); // ein paar Register setzen (z.B. CLK auf 10MHz) |
|
|
88 |
rf02_setfreq(RF02FREQ(434)); // 433,92MHz |
|
|
89 |
rf02_setpower(1); // -12dBm Ausgangangsleistung |
|
|
90 |
rf02_setmodfreq(3); // 120kHz Frequenzshift |
|
|
91 |
rf02_setbaud(19200); // 19200 Baud |
|
|
92 |
|
|
|
93 |
}// |
|
|
94 |
|
|
|
95 |
void rf02_prepAll(unsigned short freq, uint8_t setPower,uint8_t modFreq,unsigned short baudRate){ |
|
|
96 |
|
|
|
97 |
rf02_init(); // ein paar Register setzen (z.B. CLK auf 10MHz) |
|
|
98 |
rf02_setfreq( RF02FREQ(freq) ); // Sende/Empfangsfrequenz auf 433,92MHz einstellen |
|
|
99 |
|
|
|
100 |
if ( setPower < 0 ) setPower = 0; |
|
|
101 |
if ( setPower > 4 ) setPower = 4; |
|
|
102 |
rf02_setpower(setPower); // -12dBm Ausgangangsleistung |
|
|
103 |
|
|
|
104 |
if ( modFreq < 0 ) modFreq = 0; |
|
|
105 |
if ( modFreq > 8 ) modFreq = 8; |
|
|
106 |
rf02_setmodfreq(modFreq); // 120kHz Frequenzshift |
|
|
107 |
|
|
|
108 |
rf02_setbaud(baudRate); // 19200 Baud |
|
|
109 |
|
|
|
110 |
}// |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
void rf02_sendData(){ |
|
|
114 |
|
|
|
115 |
rf02_txdata( test, sizeof test); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
void rf02_trans(unsigned short value) |
|
|
121 |
{ uint8_t i; |
|
|
122 |
|
|
|
123 |
cbi(RF_PORT, CS); |
|
|
124 |
|
|
|
125 |
for (i=0; i<16; i++) |
|
|
126 |
{ if (value&0x8000) //0x8000 |
|
|
127 |
sbi(RF_PORT, SDI); |
|
|
128 |
else |
|
|
129 |
cbi(RF_PORT, SDI); |
|
|
130 |
|
|
|
131 |
sbi(RF_PORT, SCK); |
|
|
132 |
value<<=1; |
|
|
133 |
_delay_us(0.3); |
|
|
134 |
cbi(RF_PORT, SCK); |
|
|
135 |
} |
|
|
136 |
sbi(RF_PORT, CS); |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
void rf02_init(void) |
|
|
142 |
{ |
|
|
143 |
RF_PORT=(1<<CS); |
|
|
144 |
RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS); |
|
|
145 |
|
|
|
146 |
for (unsigned char i=0; i<15; i++) |
|
|
147 |
_delay_ms(10); // wait until POR done |
|
|
148 |
rf02_trans(0xC0E0); // power settings |
|
|
149 |
rf02_trans(0x8A75);// fsk in rfm02 = afc in rf12 |
|
|
150 |
// rf02_trans(0x80C7); |
|
|
151 |
rf02_trans(0xC2A0); // enable tx sync bit, disable low bat detector |
|
|
152 |
|
|
|
153 |
//LED_DDR= 0xFF; |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
void rf02_setmodfreq(uint8_t bandwidth) |
|
|
157 |
{ |
|
|
158 |
rf02_trans(0x8F80|(bandwidth&7)); |
|
|
159 |
//rf02_trans(0x8F70); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
void rf02_setfreq(unsigned short freq) |
|
|
163 |
{ if (freq<96) // 430,2400MHz |
|
|
164 |
freq=96; |
|
|
165 |
else if (freq>3903) // 439,7575MHz |
|
|
166 |
freq=3903; |
|
|
167 |
rf02_trans(0xA000|freq); |
|
|
168 |
|
|
|
169 |
//rf02_trans(0xA640); //= 434 MHz |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
void rf02_setpower(uint8_t power) |
|
|
173 |
{ |
|
|
174 |
rf02_trans(0xB000|((power&7)<<8)); |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
void rf02_setbaud(unsigned short baud) |
|
|
178 |
{ |
|
|
179 |
|
|
|
180 |
if (baud<1345) |
|
|
181 |
baud=1345; |
|
|
182 |
if (baud<19000) |
|
|
183 |
rf02_trans(0xD240); // 25% PLL current |
|
|
184 |
else if (baud<37000) |
|
|
185 |
rf02_trans(0xD2C0); // 33% PLL current |
|
|
186 |
else |
|
|
187 |
rf02_trans(0xD200); // 50% PLL current |
|
|
188 |
|
|
|
189 |
//rf02_trans(0xC800|((344828UL/baud)-1)); // Baudrate= 344827,59/(R+1) |
|
|
190 |
|
|
|
191 |
rf02_trans(0xC806); |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
void rf02_txdata( unsigned char * data, uint8_t number) |
|
|
195 |
{ |
|
|
196 |
uint8_t i,value; |
|
|
197 |
value=0xC6; //1100 0110 |
|
|
198 |
cbi(RF_PORT, CS); //nSel |
|
|
199 |
|
|
|
200 |
for (i=0; i<8; i++) |
|
|
201 |
{ if (value&0x80) //1000 0000 = 80 |
|
|
202 |
sbi(RF_PORT, SDI); |
|
|
203 |
else |
|
|
204 |
cbi(RF_PORT, SDI); |
|
|
205 |
|
|
|
206 |
sbi(RF_PORT, SCK); |
|
|
207 |
value<<=1; |
|
|
208 |
_delay_us(0.2); |
|
|
209 |
cbi(RF_PORT, SCK); |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
rf02_shiftout(0xAA);//10101010 |
|
|
213 |
rf02_shiftout(0xAA); |
|
|
214 |
rf02_shiftout(0xAA); |
|
|
215 |
rf02_shiftout(0x2D);//00101101 |
|
|
216 |
rf02_shiftout(0xD4);//11010100 |
|
|
217 |
|
|
|
218 |
// no checkbit, in experimenting some letters were transmitted wrong! |
|
|
219 |
|
|
|
220 |
for (i=0; i<number; i++) |
|
|
221 |
rf02_shiftout(*data++); |
|
|
222 |
|
|
|
223 |
sbi(RF_PORT, CS); |
|
|
224 |
while(RF_PIN&(1<<IRQ)); // wait until transfer done |
|
|
225 |
rf02_trans(0xC464); // TX off after 10us |
|
|
226 |
|
|
|
227 |
} |
|
|
228 |
|
|
|
229 |
void rf02_shiftout(unsigned char value) |
|
|
230 |
{ uint8_t j; |
|
|
231 |
for (j=0; j<8; j++) |
|
|
232 |
{ while(RF_PIN&(1<<IRQ)); |
|
|
233 |
while(!(RF_PIN&(1<<IRQ))); |
|
|
234 |
|
|
|
235 |
if (value&128) //100101000 |
|
|
236 |
sbi(RF_PORT, SDI); |
|
|
237 |
else |
|
|
238 |
cbi(RF_PORT, SDI); |
|
|
239 |
value<<=1; |
|
|
240 |
} |
|
|
241 |
} |
|
|
242 |
|