Rev Author Line No. Line
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, june 2010 , october 2010
18 *
19 *
20 *********************************************/
21  
22 #include <avr/io.h>
23 #include <avr/interrupt.h>
24 #include <avr/pgmspace.h>
25 #include <avr/eeprom.h>
26 #include <stdlib.h>
27  
28 #include "rf01.h"
29  
30 #define F_CPU 16000000UL
31 #include <util/delay.h>
32  
33 #define RF_PORT PORTB
34 #define RF_DDR DDRB
35 #define RF_PIN PINB
36  
37 #define SDI 5 // RF01 SDI, arduino 13 cannot be changed
38 #define SCK 4 // RF01 SCK, arduino 12 cannot be changed
39 #define CS 3 // RF01 nSEL, arduino 11 cannot be changed
40 #define SDO 2 // RF01 SDO, arduino 10 cannot be changed
41 //----------------- // RF01 niRQ, arduino 02 cannot be changed
42 //------------------// RF01 nFFS: 1-10k Pullup too Vcc
43  
44 #define LED_PORT PORTD
45 #define LED_DDR DDRD
46 #define LED_PIN PIND
47  
48 #define LED0 5
49 #define LED1 6 //not used
50  
51 // nFFS: 1-10k Pullup an 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 // maximum receive buffer
75 #define RF_MAX 32
76 unsigned char rf01_buf[RF_MAX]; // recv buf
77  
78 #include <util/delay.h>
79  
80 void rf01_receive(){
81 rf01_rxdata(rf01_data, 32);
82 }
83  
84 void rf01_prepAll(){
85 rf01_init(); // ein paar Register setzen (z.B. CLK auf 10MHz)
86 rf01_setfreq(RF01FREQ(434)); // Sende/Empfangsfrequenz auf 433,92MHz einstellen
87 rf01_setbandwidth(4); // 4 200kHz Bandbreite
88 rf01_setreceiver(2,4); //2,4 -6dB Verstärkung, DRSSI threshold: -79dBm
89 rf01_setbaud(57600); // 19200 Baud
90  
91 }
92  
93 static unsigned char sdrssi, sgain;
94  
95 void rf01_trans(unsigned short wert)
96 { unsigned char i;
97  
98 cbi(RF_PORT, CS);
99 for (i=0; i<16; i++)
100 { if (wert&32768)
101 sbi(RF_PORT, SDI);
102 else
103 cbi(RF_PORT, SDI);
104 sbi(RF_PORT, SCK);
105 wert<<=1;
106 _delay_us(0.2);
107 cbi(RF_PORT, SCK);
108 }
109 sbi(RF_PORT, CS);
110 }
111  
112 void rf01_init(void)
113 { unsigned char i;
114  
115 RF_PORT=(1<<CS);
116 RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
117  
118 for (i=0; i<11; i++)
119 _delay_ms(10); // wait until POR done
120  
121 rf01_trans(0xC2E0); // AVR CLK: 10MHz
122 rf01_trans(0xC42B); // Data Filter: internal
123 rf01_trans(0xCE88); // FIFO mode
124 rf01_trans(0xC6F7); // AFC settings: autotuning: -10kHz...+7,5kHz
125 rf01_trans(0xE000); // disable wakeuptimer
126 rf01_trans(0xCC00); // disable low duty cycle
127  
128 LED_DDR= 0xFF;//(1<<LED0)|(1<<LED1);
129 blinkLED();
130  
131 }
132  
133 void rf01_setbandwidth(unsigned char bandwidth)
134 {
135 rf01_trans(0x8970|((bandwidth&7)<<1));
136 }
137  
138 void rf01_setreceiver(unsigned char gain, unsigned char drssi)
139 {
140 sdrssi=drssi;
141 sgain=gain;
142 }
143  
144 void rf01_setfreq(unsigned short freq)
145 { if (freq<96) // 430,2400MHz
146 freq=96;
147 else if (freq>3903) // 439,7575MHz
148 freq=3903;
149 rf01_trans(0xA000|freq);
150 }
151  
152 void rf01_setbaud(unsigned short baud)
153 {
154 if (baud<336)
155 return;
156 if (baud<5400) // Baudrate= 344827,58621/(R+1)/(1+CS*7)
157 rf01_trans(0xC880|((43104/baud)-1));
158 else
159 rf01_trans(0xC800|((344828UL/baud)-1));
160  
161 rf01_trans(0xC806);
162 }
163  
164 void rf01_rxdata(unsigned char *data, unsigned char number)
165 { unsigned char i,j,c;
166  
167 rf01_trans(0xC0C1|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX on
168 rf01_trans(0xCE89); // set FIFO mode
169 rf01_trans(0xCE8B); // enable FIFO
170 cbi(RF_PORT, SDI);
171 for (i=0; i<number; i++)
172 { cbi(RF_PORT, CS);
173 while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO
174 for (j=0; j<16; j++) // read and discard status register
175 { sbi(RF_PORT, SCK);
176 asm("nop");
177 cbi(RF_PORT, SCK);
178 }
179 c=0;
180 for (j=0; j<8; j++)
181 { c<<=1;
182 if (RF_PIN&(1<<SDO))
183 c|=1;
184 sbi(RF_PORT, SCK);
185 _delay_us(0.2);
186 cbi(RF_PORT, SCK);
187 }
188 *data++=c;
189 sbi(RF_PORT, CS);
190 }
191 //blinkLED();
192 rf01_trans(0xC0C0|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX off
193 }
194  
195 void blinkLED(void){
196 for (unsigned char i=0; i<15; i++)
197 _delay_ms(5);
198 sLED1();
199  
200 for (unsigned char i=0; i<15; i++)
201 _delay_ms(5);
202 cLED1();
203  
204 }
205  
206 void makePulse(int numberOfPulses){
207 if ( numberOfPulses > 0)
208 {
209 for (unsigned char i=0; i<numberOfPulses; i++)
210 {
211 _delay_ms(20);
212 sLED0();
213 _delay_ms(20);
214 cLED0();
215 }
216 _delay_ms(50);
217 }
218  
219 }
220