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