Rev 2112 Rev 2125
Line 39... Line 39...
39 #define CS 3 // RF01 nSEL, arduino 11 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 40 #define SDO 2 // RF01 SDO, arduino 10 cannot be changed
41 //----------------- // RF01 niRQ, arduino 02 cannot be changed 41 //----------------- // RF01 niRQ, arduino 02 cannot be changed
42 //------------------// RF01 nFFS: 1-10k Pullup too Vcc 42 //------------------// RF01 nFFS: 1-10k Pullup too Vcc
43   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   44  
53 #ifndef cbi 45 #ifndef cbi
54 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) 46 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
55 #endif 47 #endif
56 #ifndef sbi 48 #ifndef sbi
57 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) 49 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
58 #endif 50 #endif
59   51  
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 52 // maximum receive buffer
75 #define RF_MAX 32 53 #define RF_MAX 32
76 unsigned char rf01_buf[RF_MAX]; // recv buf 54 unsigned char rf01_buf[RF_MAX]; // recv buf
77   55  
78 #include <util/delay.h> 56 #include <util/delay.h>
79   57  
80 void rf01_receive(){ 58 void rf01_receive(){
81 rf01_rxdata(rf01_data, 32); 59 rf01_rxdata(rf01_data, 23); //!!!32
82 } 60 }
83   61  
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 62 static unsigned char sdrssi, sgain;
90   63  
-   64 void rf01_prepAll()
91 } 65 {
-   66 RF_PORT=(1<<CS);
-   67 RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
92   68  
-   69 for (unsigned char i=0; i<11; i++) _delay_ms(10); // wait until POR done
-   70  
-   71 // rf01_trans(0xC2E0); // AVR CLK: 10MHz
-   72 // rf01_trans(0xC42B); // Data Filter: internal
-   73 // rf01_trans(0xC6F7); // AFC settings: autotuning: -10kHz...+7,5kHz
-   74 // rf01_trans(0xE000); // disable wakeuptimer
-   75 // rf01_trans(0xCC00); // disable low duty cycle
-   76 // rf01_trans(0x8978); // band 433MHz, enable crystal + 12pF, 200kHz bandwidth
-   77  
-   78 rf01_trans(0x0000);
-   79 // rf01_trans(0x898A); // band 433MHz, 134kHz bandwidth
-   80 rf01_trans(0x8000|0x1000|0x70|0x02); //band
-   81 rf01_trans(0xA640); //434MHz
-   82 rf01_trans(0xC823); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 9600 Bd
-   83 rf01_trans(0xC69B);
-   84 rf01_trans(0xC42A);
-   85 rf01_trans(0xC240); //*
-   86 rf01_trans(0xC080); //*
93 static unsigned char sdrssi, sgain; 87 rf01_trans(0xCE88); // FIFO mode
-   88 rf01_trans(0xCE8B); //*
-   89 rf01_trans(0xC081); //*
-   90 }
94   91  
95 void rf01_trans(unsigned short wert) 92 void rf01_trans(unsigned short wert)
96 { unsigned char i; 93 { unsigned char i;
97   94  
98 cbi(RF_PORT, CS); 95 cbi(RF_PORT, CS);
Line 107... Line 104...
107 cbi(RF_PORT, SCK); 104 cbi(RF_PORT, SCK);
108 } 105 }
109 sbi(RF_PORT, CS); 106 sbi(RF_PORT, CS);
110 } 107 }
111   108  
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) 109 void rf01_rxdata(unsigned char *data, unsigned char number)
165 { unsigned char i,j,c; 110 { unsigned char i,j,c;
166   111  
-   112 //!!!
-   113 // sgain=2; //2,4 -6dB LNA gain, DRSSI threshold: -79dBm
-   114 // sdrssi=4;
167 rf01_trans(0xC0C1|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX on 115 //!!! rf01_trans(0xC0C1|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX on
168 rf01_trans(0xCE89); // set FIFO mode 116 rf01_trans(0xCE89); // set FIFO mode
169 rf01_trans(0xCE8B); // enable FIFO 117 rf01_trans(0xCE8B); // enable FIFO
170 cbi(RF_PORT, SDI); 118 cbi(RF_PORT, SDI);
-   119 asm("nop");
-   120 asm("nop");
-   121 asm("nop");
171 for (i=0; i<number; i++) 122 for (i=0; i<number; i++)
172 { cbi(RF_PORT, CS); 123 { cbi(RF_PORT, CS);
-   124 asm("nop");
-   125 asm("nop");
-   126 asm("nop");
173 while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO 127 while (!(RF_PIN&(1<<SDO))); // wait until data in FIFO
174 for (j=0; j<16; j++) // read and discard status register 128 for (j=0; j<16; j++) // read and discard status register
-   129 {
175 { sbi(RF_PORT, SCK); 130 sbi(RF_PORT, SCK);
-   131 asm("nop");
-   132 asm("nop");
176 asm("nop"); 133 asm("nop");
177 cbi(RF_PORT, SCK); 134 cbi(RF_PORT, SCK);
-   135 asm("nop");
-   136 asm("nop");
-   137 asm("nop");
178 } 138 }
179 c=0; 139 c=0;
180 for (j=0; j<8; j++) 140 for (j=0; j<8; j++)
181 { c<<=1; 141 { c<<=1;
182 if (RF_PIN&(1<<SDO)) 142 if (RF_PIN&(1<<SDO))
183 c|=1; 143 c|=1;
184 sbi(RF_PORT, SCK); 144 sbi(RF_PORT, SCK);
-   145 asm("nop");
-   146 asm("nop");
-   147 asm("nop");
185 _delay_us(0.2); 148 //!!! _delay_us(0.2);
186 cbi(RF_PORT, SCK); 149 cbi(RF_PORT, SCK);
-   150 asm("nop");
-   151 asm("nop");
-   152 asm("nop");
187 } 153 }
188 *data++=c; 154 *data++=c;
189 sbi(RF_PORT, CS); 155 sbi(RF_PORT, CS);
-   156 asm("nop");
-   157 asm("nop");
-   158 asm("nop");
190 } 159 }
191 //blinkLED(); -  
192 rf01_trans(0xC0C0|((sgain&3)<<4)|((sdrssi&7)<<1)); // RX off 160 //!!! 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 } 161 }
220   -