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