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, 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 SDI 0 // SDI, -> RF02 Atmega PB0 Arduino 8
41 #define SCK 1 // SCK, -> RF02 Atmega PB1 Arduino 9
42 #define CS 2 // nSEL, -> RF02 Atmega PB2 Arduino 10
43 #define IRQ 4 // nIRQ, <- RF02 Atmega PB4 Arduino 12
2125 kakl 44 #define FSK 3 // FSK, <- RF02 Atmega PB3 Arduino ???
2112 kakl 45 //------------------// FSK: Pullupto VCC
46  
47 #ifndef cbi
48 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
49 #endif
50 #ifndef sbi
51 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
52 #endif
53  
54  
55 unsigned char test[32]=" 55555 \n";
56  
2125 kakl 57 void rf02_changeText( unsigned char* ptr, uint8_t number)
58 {
2112 kakl 59  
60 if (number> 32)number = 32;
61 memcpy( test, ptr, number);
62  
63 }
64  
2125 kakl 65 void rf02_prepAll()
66 {
67 RF_PORT=(1<<CS);
68 RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
2112 kakl 69  
2125 kakl 70 sbi(RF_PORT, FSK);
2112 kakl 71  
72  
2125 kakl 73 for (unsigned char i=0; i<15; i++) _delay_ms(10); // wait until POR done
2112 kakl 74  
75  
2125 kakl 76 rf02_trans(0xCC00); //*//
2112 kakl 77  
2125 kakl 78 // rf02_trans(0x8B61); //*//
79 rf02_trans(0x8000|0x1000|0x70|0x02); //*//
80 rf02_trans(0xA000|0x640); //*//
81 // rf02_trans(0xA640); ///= 434 MHz
2112 kakl 82  
2125 kakl 83 // rf02_trans(0xD2C0); // 33% PLL current
84 // rf02_trans(0xC823); // 9600 Bd
85 rf02_trans(0xD040); // RATE/2
86 rf02_trans(0xC811); // 19200 => 9600 Bd
2112 kakl 87  
2125 kakl 88 rf02_trans(0xC0E0); // power settings
89  
90 rf02_trans(0xC220); //0xC2A0
91 // rf02_trans(0xB100);
92 // rf02_trans(0x8F83);
93 //!!!
94 // rf02_trans(0xC001);
95  
96  
2112 kakl 97 }
98  
2125 kakl 99 void rf02_sendData()
100 {
2112 kakl 101  
2125 kakl 102 //!!! rf02_txdata( test, sizeof test);
103 rf02_txdata( test, 23);
104 }
2112 kakl 105  
106 void rf02_trans(unsigned short value)
2125 kakl 107 {
108 uint8_t i;
2112 kakl 109  
110 cbi(RF_PORT, CS);
111  
112 for (i=0; i<16; i++)
113 { if (value&0x8000) //0x8000
114 sbi(RF_PORT, SDI);
115 else
116 cbi(RF_PORT, SDI);
117  
118 sbi(RF_PORT, SCK);
119 value<<=1;
120 _delay_us(0.3);
121 cbi(RF_PORT, SCK);
122 }
123 sbi(RF_PORT, CS);
124 }
125  
126  
127 void rf02_txdata( unsigned char * data, uint8_t number)
128 {
129 uint8_t i,value;
130 value=0xC6; //1100 0110
131 cbi(RF_PORT, CS); //nSel
132  
2125 kakl 133 //!!!!
134 // rf02_trans(0xC039); // TX start
135  
136  
2112 kakl 137 for (i=0; i<8; i++)
138 { if (value&0x80) //1000 0000 = 80
139 sbi(RF_PORT, SDI);
140 else
141 cbi(RF_PORT, SDI);
142  
143 sbi(RF_PORT, SCK);
144 value<<=1;
145 _delay_us(0.2);
146 cbi(RF_PORT, SCK);
147 }
148  
149 rf02_shiftout(0xAA);//10101010
150 rf02_shiftout(0xAA);
151 rf02_shiftout(0xAA);
152 rf02_shiftout(0x2D);//00101101
153 rf02_shiftout(0xD4);//11010100
154  
155 // no checkbit, in experimenting some letters were transmitted wrong!
156  
157 for (i=0; i<number; i++)
158 rf02_shiftout(*data++);
159  
160 sbi(RF_PORT, CS);
161 while(RF_PIN&(1<<IRQ)); // wait until transfer done
162 rf02_trans(0xC464); // TX off after 10us
2125 kakl 163 //rf02_trans(0xC001); // TX close
164 _delay_ms(10);
2112 kakl 165 }
166  
167 void rf02_shiftout(unsigned char value)
168 { uint8_t j;
169 for (j=0; j<8; j++)
170 { while(RF_PIN&(1<<IRQ));
171 while(!(RF_PIN&(1<<IRQ)));
172  
173 if (value&128) //100101000
2125 kakl 174 {
175 sbi(RF_PORT, SDI);
176 }
2112 kakl 177 else
2125 kakl 178 {
2112 kakl 179 cbi(RF_PORT, SDI);
2125 kakl 180 }
2112 kakl 181 value<<=1;
182 }
183 }
184