Rev Author Line No. Line
1096 mija 1 /* mija 2008
2 demo for RFM01 - RX 868MHz
3  
4 CPU ATMEGA8
5 fcpu = 1MHz
6  
7 !! define PIN,PORT,DDR for IOpin !!
8  
9 tested with module RFM12B - TX,9600,Fdev 15kHz
10 */
11  
12 #include <avr/io.h>
13 #include <util/delay.h>
14 #include "RFM01.h"
15  
16 //************************************************************************
17  
18 #define SDI PB3
19 #define SDI_PORT PORTB
20 #define SDI_DDR DDRB
21  
22 #define FSK PC1
23 #define FSK_PORT PORTC
24 #define FSK_DDR DDRC
25  
26 #define SDO PB4 // input for mega
27 #define SDO_PORT PORTB
28 #define SDO_DDR DDRB
29 #define SDO_PIN PINB
30  
31 #define SCK PB5
32 #define SCK_PORT PORTB
33 #define SCK_DDR DDRB
34  
35 #define nIRQ PD2 // input for mega
36 #define nIRQ_PORT PORTD
37 #define nIRQ_DDR DDRD
38 #define nIRQ_PIN PIND
39  
40 #define nSEL PB2
41 #define nSEL_PORT PORTB
42 #define nSEL_DDR DDRB
43  
44 #define LED PD3
45 #define LED_PORT PORTD
46 #define LED_DDR DDRD
47  
48 // interni
49 #define SDI_H SDI_PORT |= _BV(SDI)
50 #define SDI_L SDI_PORT &= (~(_BV(SDI)))
51 #define SDI_INIT SDI_DDR |= _BV(SDI)
52  
53 #define FSK_H FSK_PORT |= _BV(FSK)
54 #define FSK_L FSK_PORT &= (~(_BV(FSK)))
55 #define FSK_INIT FSK_DDR |= _BV(FSK)
56  
57 #define SDO_INPUT (SDO_PIN & _BV(SDO))
58 #define SDO_INIT SDO_DDR &= (~(_BV(SDO)))
59  
60 #define SCK_H SCK_PORT |= _BV(SCK)
61 #define SCK_L SCK_PORT &= (~(_BV(SCK)))
62 #define SCK_INIT SCK_DDR |= _BV(SCK)
63  
64 #define nIRQ_INPUT (nIRQ_PIN & _BV(nIRQ))
65 #define nIRQ_INIT nIRQ_DDR &= (~(_BV(nIRQ)))
66  
67 #define nSEL_H nSEL_PORT |= _BV(nSEL)
68 #define nSEL_L nSEL_PORT &= (~(_BV(nSEL)))
69 #define nSEL_INIT nSEL_DDR |= _BV(nSEL)
70  
71 #define LED_H LED_PORT |= _BV(LED)
72 #define LED_L LED_PORT &= (~(_BV(LED)))
73 #define LED_INIT LED_DDR |= _BV(LED)
74  
75 #define START_FIFO RF_WRITE_CMD(CMD_FIFO|FIFO_8|FIFO_VDI_WORD|FIFO_FF|FIFO_FE)
76 #define STOP_FIFO RF_WRITE_CMD(CMD_FIFO)
77  
78 //************************************************************************
79  
80 uint8_t rx_buf[16];
81  
82 //************************************************************************
83  
84 void delay_ms(uint16_t time)
85 {
86 while(time--) _delay_ms(1);
87 }
88  
89 void IO_INIT(void)
90 {
91 SDI_INIT;
92 SDO_INIT;
93 SCK_INIT;
94 nIRQ_INIT;
95 nSEL_INIT;
96 FSK_INIT;
97 LED_INIT;
98 }
99  
100 void RF_INIT(void)
101 {
102 nSEL_H;
103 SDI_H;
104 SCK_L;
105 nIRQ_INPUT;
106 SDO_INPUT;
107 FSK_H;
108 }
109  
110 uint16_t RF_WRITE_CMD(uint16_t cmd)
111 {
112 uint8_t i;
113 uint16_t temp;
114  
115 SCK_L;
116 nSEL_L;
117 temp=0;
118 for (i=0;i<16;i++)
119 {
120 if (cmd & 0x8000) SDI_H;
121 else SDI_L;
122 SCK_H;
123 cmd <<= 1;
124 temp <<= 1;
125 if(SDO_INPUT) temp |= 0x0001;
126 SCK_L;
127 }
128 SCK_L;
129 nSEL_H;
130 return (temp);
131 }
132  
133 void RF_WRITE_DATA(uint8_t data)
134 {
135 while (nIRQ_INPUT);
136 RF_WRITE_CMD(0xB800 + data);
137 }
138  
139 uint8_t RF_READ_DATA(void)
140 {
141 uint8_t i,result;
142  
143 while (nIRQ_INPUT);
144 SCK_L;
145 nSEL_L;
146 SDI_L;
147 result=0;
148 for (i=0;i<24;i++)
149 {
150 result <<= 1;
151 if (SDO_INPUT) result |= 0x01;
152 SCK_H;
153 SCK_L;
154 }
155 nSEL_H;
156 return (result);
157 }
158  
159 void RS232_INIT(void)
160 {
161 //set baud rate 9600 8N1 for Fosc 1MHz
162 UBRRH = 0;
163 UBRRL = 12;
164 UCSRB = (1<<RXEN)|(1<<TXEN); //enable RX TX
165 UCSRC = (1<<URSEL) |(3<<UCSZ0); //8N1
166 UCSRA |= _BV(U2X);
167 }
168  
169 void put_rs232(char data)
170 {
171 // Wait for empty transmit buffer
172 while ( !( UCSRA & (1<<UDRE)) );
173 // Put data into buffer, sends the data
174 UDR = data;
175 }
176  
177 int main()
178 {
179 uint8_t i,ChkSum;
180 uint8_t LED_TRG;
181 uint8_t b;
182  
183 IO_INIT();
184 RS232_INIT();
185 RF_INIT();
186 delay_ms(100);
187 LED_L;
188 LED_TRG=0;
189  
190  
191  
192 RF_WRITE_CMD(CMD_SETTING |BAND_868 | C_12pF | BANDWIDTH_67 | SETTING_DC);
193 RF_WRITE_CMD(CMD_FREQUENCY |FREQUENCY_868);
194 RF_WRITE_CMD(CMD_RATE |RATE_9600);
195 RF_WRITE_CMD(CMD_FILTER |FILTER_AL | FILTER_S0 | DQD_4);
196 RF_WRITE_CMD(CMD_AFC |AFC_POWER_ON | AFC_RANG_8 | AFC_ST | AFC_OE | AFC_EN);
197 RF_WRITE_CMD(CMD_RX);
198 RF_WRITE_CMD(CMD_RX |VDI_CLOCK | LNA_GAIN_0 | DRSSI_103 | RX_EN);
199 RF_WRITE_CMD(CMD_FIFO);
200  
201 while (1)
202 {
203  
204 START_FIFO;
205 ChkSum = 0;
206 for (i=0;i<16;i++)
207 {
208 b = RF_READ_DATA();
209 rx_buf[i] = b;
210 ChkSum += b;
211 }
212 b = RF_READ_DATA();
213 RF_READ_DATA();
214 STOP_FIFO;
215  
216 for (i=0;i<16;i++)
217 {
218 put_rs232(rx_buf[i]);
219 delay_ms(2);
220 }
221 if (ChkSum == b)
222 {
223 LED_TRG = ~ LED_TRG;
224 put_rs232(' ');
225 put_rs232('o');
226 put_rs232('k');
227 }
228 else
229 {
230 put_rs232('\n');
231 delay_ms(2);
232 put_rs232('\r');
233 delay_ms(2);
234 put_rs232('e');
235 put_rs232('r');
236 put_rs232('r');
237 }
238 if (LED_TRG) LED_H;
239 else LED_L;
240 }
241 return 0;
242 }
243  
244  
245