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