1177 |
mija |
1 |
/* mija 2008 |
|
|
2 |
demo for RFM01 - RX 868MHz |
|
|
3 |
|
|
|
4 |
CPU ATtiny2313 |
|
|
5 |
fcpu = 1MHz |
|
|
6 |
|
|
|
7 |
!! define PIN,PORT,DDR for IOpin !! |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
#include <avr/io.h> |
|
|
13 |
#include <util/delay.h> |
|
|
14 |
#include <avr/interrupt.h> |
|
|
15 |
#include "RFM01.h" |
|
|
16 |
|
|
|
17 |
//************************************************************************ |
|
|
18 |
|
|
|
19 |
#define LENGTH_MSG 20 |
|
|
20 |
|
|
|
21 |
#define SDI PD3 |
|
|
22 |
#define SDI_PORT PORTD |
|
|
23 |
#define SDI_DDR DDRD |
|
|
24 |
|
|
|
25 |
#define SDO PA1 // input for mega |
|
|
26 |
#define SDO_PORT PORTA |
|
|
27 |
#define SDO_DDR DDRA |
|
|
28 |
#define SDO_PIN PINA |
|
|
29 |
|
|
|
30 |
#define SCK PD4 |
|
|
31 |
#define SCK_PORT PORTD |
|
|
32 |
#define SCK_DDR DDRD |
|
|
33 |
|
|
|
34 |
#define nIRQ PD2 // input for mega |
|
|
35 |
#define nIRQ_PORT PORTD |
|
|
36 |
#define nIRQ_DDR DDRD |
|
|
37 |
#define nIRQ_PIN PIND |
|
|
38 |
|
|
|
39 |
#define nSEL PD5 |
|
|
40 |
#define nSEL_PORT PORTD |
|
|
41 |
#define nSEL_DDR DDRD |
|
|
42 |
|
|
|
43 |
#define LED PB0 |
|
|
44 |
#define LED_PORT PORTB |
|
|
45 |
#define LED_DDR DDRB |
|
|
46 |
|
|
|
47 |
// interni |
|
|
48 |
#define SDI_H SDI_PORT |= _BV(SDI) |
|
|
49 |
#define SDI_L SDI_PORT &= (~(_BV(SDI))) |
|
|
50 |
#define SDI_INIT SDI_DDR |= _BV(SDI) |
|
|
51 |
|
|
|
52 |
#define SDO_INPUT (SDO_PIN & _BV(SDO)) |
|
|
53 |
#define SDO_INIT SDO_DDR &= (~(_BV(SDO))) |
|
|
54 |
|
|
|
55 |
#define SCK_H SCK_PORT |= _BV(SCK) |
|
|
56 |
#define SCK_L SCK_PORT &= (~(_BV(SCK))) |
|
|
57 |
#define SCK_INIT SCK_DDR |= _BV(SCK) |
|
|
58 |
|
|
|
59 |
#define nIRQ_INPUT (nIRQ_PIN & _BV(nIRQ)) |
|
|
60 |
#define nIRQ_INIT nIRQ_DDR &= (~(_BV(nIRQ))) |
|
|
61 |
|
|
|
62 |
#define nSEL_H nSEL_PORT |= _BV(nSEL) |
|
|
63 |
#define nSEL_L nSEL_PORT &= (~(_BV(nSEL))) |
|
|
64 |
#define nSEL_INIT nSEL_DDR |= _BV(nSEL) |
|
|
65 |
|
|
|
66 |
#define LED_H LED_PORT |= _BV(LED) |
|
|
67 |
#define LED_L LED_PORT &= (~(_BV(LED))) |
|
|
68 |
#define LED_INIT LED_DDR |= _BV(LED) |
|
|
69 |
|
|
|
70 |
#define START_FIFO RF_WRITE_CMD(CMD_FIFO|FIFO_8|FIFO_VDI_WORD|FIFO_FF|FIFO_FE) |
|
|
71 |
//#define START_FIFO RF_WRITE_CMD(CMD_FIFO|FIFO_8|FIFO_ALWAYS|FIFO_FF|FIFO_FE) |
|
|
72 |
#define STOP_FIFO RF_WRITE_CMD(CMD_FIFO) |
|
|
73 |
#define BLIK_LED tik262ms=1 |
|
|
74 |
|
|
|
75 |
//************************************************************************ |
|
|
76 |
|
|
|
77 |
uint8_t rx_buf[LENGTH_MSG+1]; |
|
|
78 |
volatile uint8_t tik262ms; |
|
|
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 |
LED_INIT; |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
void RF_INIT(void) |
|
|
98 |
{ |
|
|
99 |
nSEL_H; |
|
|
100 |
SDI_H; |
|
|
101 |
SCK_L; |
|
|
102 |
nIRQ_INPUT; |
|
|
103 |
SDO_INPUT; |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
void RS232_INIT(void) |
|
|
107 |
{ |
|
|
108 |
//set baud rate fixed 9600 8N1 for fosc 1Mhz |
|
|
109 |
UCSRA = _BV(U2X) ; |
|
|
110 |
UBRRL = 12; |
|
|
111 |
UCSRB = _BV(TXEN) | _BV(RXCIE);//_BV(RXEN) | |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
void TIMER0_INIT(void) |
|
|
115 |
{ |
|
|
116 |
// enable timer0 for f=fosc/1024 cca 262ms |
|
|
117 |
TCCR0B = _BV(CS02) | _BV(CS00); |
|
|
118 |
TIMSK |= _BV(TOIE0); |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
ISR(TIMER0_OVF_vect) |
|
|
122 |
{ |
|
|
123 |
if (tik262ms) {tik262ms--;LED_H;} |
|
|
124 |
else LED_L; |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
void put_rs232(char data) |
|
|
128 |
{ |
|
|
129 |
while (!( UCSRA & _BV(UDRE))); |
|
|
130 |
UDR = data; |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
uint16_t RF_WRITE_CMD(uint16_t cmd) |
|
|
134 |
{ |
|
|
135 |
uint8_t i; |
|
|
136 |
uint16_t temp; |
|
|
137 |
|
|
|
138 |
SCK_L; |
|
|
139 |
nSEL_L; |
|
|
140 |
temp=0; |
|
|
141 |
for (i=0;i<16;i++) |
|
|
142 |
{ |
|
|
143 |
if (cmd & 0x8000) SDI_H; |
|
|
144 |
else SDI_L; |
|
|
145 |
SCK_H; |
|
|
146 |
cmd <<= 1; |
|
|
147 |
temp <<= 1; |
|
|
148 |
if(SDO_INPUT) temp |= 0x0001; |
|
|
149 |
SCK_L; |
|
|
150 |
} |
|
|
151 |
SCK_L; |
|
|
152 |
nSEL_H; |
|
|
153 |
return (temp); |
|
|
154 |
} |
|
|
155 |
|
|
|
156 |
void RF_WRITE_DATA(uint8_t data) |
|
|
157 |
{ |
|
|
158 |
while (nIRQ_INPUT); |
|
|
159 |
RF_WRITE_CMD(0xB800 + data); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
uint8_t RF_READ_DATA(void) |
|
|
163 |
{ |
|
|
164 |
uint8_t i,result; |
|
|
165 |
|
|
|
166 |
while (nIRQ_INPUT); |
|
|
167 |
SCK_L; |
|
|
168 |
nSEL_L; |
|
|
169 |
SDI_L; |
|
|
170 |
result=0; |
|
|
171 |
for (i=0;i<24;i++) |
|
|
172 |
{ |
|
|
173 |
result <<= 1; |
|
|
174 |
if (SDO_INPUT) result |= 0x01; |
|
|
175 |
SCK_H; |
|
|
176 |
SCK_L; |
|
|
177 |
} |
|
|
178 |
nSEL_H; |
|
|
179 |
return (result); |
|
|
180 |
} |
|
|
181 |
|
|
|
182 |
int main() |
|
|
183 |
{ |
|
|
184 |
uint8_t i,ChkSum; |
|
|
185 |
//uint8_t LED_TRG; |
|
|
186 |
uint8_t b; |
|
|
187 |
|
|
|
188 |
IO_INIT(); |
|
|
189 |
RS232_INIT(); |
|
|
190 |
RF_INIT(); |
|
|
191 |
TIMER0_INIT(); |
|
|
192 |
sei(); |
|
|
193 |
delay_ms(100); |
|
|
194 |
LED_L; |
|
|
195 |
//LED_TRG=0; |
|
|
196 |
|
|
|
197 |
RF_WRITE_CMD(CMD_SETTING |BAND_868 | C_12pF | BANDWIDTH_134 | SETTING_DC); |
|
|
198 |
RF_WRITE_CMD(CMD_FREQUENCY |FREQUENCY_868); |
|
|
199 |
RF_WRITE_CMD(CMD_RATE |RATE_9600); |
|
|
200 |
RF_WRITE_CMD(CMD_FILTER |FILTER_AL | FILTER_S0 | DQD_4); |
|
|
201 |
RF_WRITE_CMD(CMD_AFC |AFC_POWER_ON | AFC_RANG_8 | AFC_ST | AFC_OE | AFC_EN); |
|
|
202 |
RF_WRITE_CMD(CMD_RX); |
|
|
203 |
RF_WRITE_CMD(CMD_RX |VDI_CLOCK | LNA_GAIN_0 | DRSSI_103 | RX_EN); |
|
|
204 |
RF_WRITE_CMD(CMD_FIFO); |
|
|
205 |
|
|
|
206 |
for (b=0;b<3;b++){BLIK_LED;delay_ms(500);} |
|
|
207 |
delay_ms(500); |
|
|
208 |
put_rs232('\f'); |
|
|
209 |
while (1) |
|
|
210 |
{ |
|
|
211 |
|
|
|
212 |
cli(); |
|
|
213 |
START_FIFO; |
|
|
214 |
ChkSum = 0; |
|
|
215 |
for (i=0;i<LENGTH_MSG;i++) |
|
|
216 |
{ |
|
|
217 |
b = RF_READ_DATA(); |
|
|
218 |
LED_H; |
|
|
219 |
rx_buf[i] = b; |
|
|
220 |
ChkSum += b; |
|
|
221 |
} |
|
|
222 |
//BLIK_LED; |
|
|
223 |
b = RF_READ_DATA(); |
|
|
224 |
RF_READ_DATA(); |
|
|
225 |
STOP_FIFO; |
|
|
226 |
LED_L; |
|
|
227 |
//sei(); |
|
|
228 |
|
|
|
229 |
if (ChkSum == b) |
|
|
230 |
{ |
|
|
231 |
//BLIK_LED;//LED_TRG = ~ LED_TRG; |
|
|
232 |
for (i=0;i<LENGTH_MSG;i++) put_rs232(rx_buf[i]); |
|
|
233 |
} |
|
|
234 |
else |
|
|
235 |
{ |
|
|
236 |
put_rs232('\f'); |
|
|
237 |
put_rs232('e'); |
|
|
238 |
put_rs232('r'); |
|
|
239 |
put_rs232('r'); |
|
|
240 |
put_rs232('o'); |
|
|
241 |
put_rs232('r'); |
|
|
242 |
} |
|
|
243 |
//if (LED_TRG) LED_H; |
|
|
244 |
//else LED_L; |
|
|
245 |
} |
|
|
246 |
return 0; |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
|
|
|
250 |
|