1178 |
mija |
1 |
/* mija 2008 |
|
|
2 |
demo for RFM02 - TX 868MHz |
|
|
3 |
|
|
|
4 |
CPU ATtiny2313 |
|
|
5 |
fcpu = 1MHz |
|
|
6 |
|
|
|
7 |
!! define PIN,PORT,DDR for IOpin !! |
|
|
8 |
|
|
|
9 |
*/ |
|
|
10 |
|
|
|
11 |
#include <avr/io.h> |
|
|
12 |
#include <util/delay.h> |
|
|
13 |
#include "RFM02.h" |
|
|
14 |
|
|
|
15 |
//************************************************************************ |
|
|
16 |
|
|
|
17 |
#define LENGTH_MSG 20 |
|
|
18 |
|
|
|
19 |
#define SDI PA1 |
|
|
20 |
#define SDI_PORT PORTA |
|
|
21 |
#define SDI_DDR DDRA |
|
|
22 |
|
|
|
23 |
#define FSK PD4 |
|
|
24 |
#define FSK_PORT PORTD |
|
|
25 |
#define FSK_DDR DDRD |
|
|
26 |
|
|
|
27 |
/*#define SDO PB4 // input for mega |
|
|
28 |
#define SDO_PORT PORTB |
|
|
29 |
#define SDO_DDR DDRB |
|
|
30 |
#define SDO_PIN PINB |
|
|
31 |
*/ |
|
|
32 |
|
|
|
33 |
#define SCK PD2 |
|
|
34 |
#define SCK_PORT PORTD |
|
|
35 |
#define SCK_DDR DDRD |
|
|
36 |
|
|
|
37 |
#define nIRQ PD5 // input for mega |
|
|
38 |
#define nIRQ_PORT PORTD |
|
|
39 |
#define nIRQ_DDR DDRD |
|
|
40 |
#define nIRQ_PIN PIND |
|
|
41 |
|
|
|
42 |
#define nSEL PD3 |
|
|
43 |
#define nSEL_PORT PORTD |
|
|
44 |
#define nSEL_DDR DDRD |
|
|
45 |
|
|
|
46 |
#define LED PB0 |
|
|
47 |
#define LED_PORT PORTB |
|
|
48 |
#define LED_DDR DDRB |
|
|
49 |
|
|
|
50 |
// interni |
|
|
51 |
#define SDI_H SDI_PORT |= _BV(SDI) |
|
|
52 |
#define SDI_L SDI_PORT &= (~(_BV(SDI))) |
|
|
53 |
#define SDI_INIT SDI_DDR |= _BV(SDI) |
|
|
54 |
|
|
|
55 |
#define FSK_H FSK_PORT |= _BV(FSK) |
|
|
56 |
#define FSK_L FSK_PORT &= (~(_BV(FSK))) |
|
|
57 |
#define FSK_INIT FSK_DDR |= _BV(FSK) |
|
|
58 |
|
|
|
59 |
#define SDO_INPUT (SDO_PIN & _BV(SDO)) |
|
|
60 |
#define SDO_INIT SDO_DDR &= (~(_BV(SDO))) |
|
|
61 |
|
|
|
62 |
#define SCK_H SCK_PORT |= _BV(SCK) |
|
|
63 |
#define SCK_L SCK_PORT &= (~(_BV(SCK))) |
|
|
64 |
#define SCK_INIT SCK_DDR |= _BV(SCK) |
|
|
65 |
|
|
|
66 |
#define nIRQ_INPUT (nIRQ_PIN & _BV(nIRQ)) |
|
|
67 |
#define nIRQ_INIT nIRQ_DDR &= (~(_BV(nIRQ))) |
|
|
68 |
|
|
|
69 |
#define nSEL_H nSEL_PORT |= _BV(nSEL) |
|
|
70 |
#define nSEL_L nSEL_PORT &= (~(_BV(nSEL))) |
|
|
71 |
#define nSEL_INIT nSEL_DDR |= _BV(nSEL) |
|
|
72 |
|
|
|
73 |
#define LED_H LED_PORT |= _BV(LED) |
|
|
74 |
#define LED_L LED_PORT &= (~(_BV(LED))) |
|
|
75 |
#define LED_INIT LED_DDR |= _BV(LED) |
|
|
76 |
|
|
|
77 |
#define START_TX RF_WRITE_CMD(CMD_POWER|POWER_EX|POWER_ES|POWER_EA|POWER_DC) |
|
|
78 |
#define STOP_TX RF_WRITE_CMD(CMD_POWER|POWER_DC) |
|
|
79 |
|
|
|
80 |
#define RX_ENABLE UCSRB = _BV(RXEN) |
|
|
81 |
#define RX_DISABLE UCSRB &= ~(_BV(RXEN)) |
|
|
82 |
//************************************************************************ |
|
|
83 |
|
|
|
84 |
//uint8_t test[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x03b,0x3c,0x3d,0x3e,0x3f}; |
|
|
85 |
uint8_t test[LENGTH_MSG]; |
|
|
86 |
//uint8_t test[16]="0123456789abcdef"; |
|
|
87 |
|
|
|
88 |
|
|
|
89 |
//************************************************************************ |
|
|
90 |
|
|
|
91 |
void delay_ms(uint16_t time) |
|
|
92 |
{ |
|
|
93 |
while(time--) _delay_ms(1); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
void IO_INIT(void) |
|
|
97 |
{ |
|
|
98 |
SDI_INIT; |
|
|
99 |
//SDO_INIT; |
|
|
100 |
SCK_INIT; |
|
|
101 |
nIRQ_INIT; |
|
|
102 |
nSEL_INIT; |
|
|
103 |
FSK_INIT; |
|
|
104 |
LED_INIT; |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
void rs232_init(void) |
|
|
108 |
{ |
|
|
109 |
//set baud rate fixed 9600 8N1 for fosc 1Mhz |
|
|
110 |
UCSRA = _BV(U2X) ; |
|
|
111 |
UBRRL = 12; |
|
|
112 |
UCSRB = _BV(RXEN) ; |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
void RF_INIT(void) |
|
|
116 |
{ |
|
|
117 |
nSEL_H; |
|
|
118 |
SDI_H; |
|
|
119 |
SCK_L; |
|
|
120 |
nIRQ_INPUT; |
|
|
121 |
//SDO_INPUT; |
|
|
122 |
FSK_H; |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
void RF_WRITE_CMD(uint16_t cmd) |
|
|
126 |
{ |
|
|
127 |
uint8_t i; |
|
|
128 |
|
|
|
129 |
SCK_L; |
|
|
130 |
nSEL_L; |
|
|
131 |
for (i=0;i<16;i++) |
|
|
132 |
{ |
|
|
133 |
SCK_L; |
|
|
134 |
SCK_L; |
|
|
135 |
if (cmd & 0x8000) SDI_H; |
|
|
136 |
else SDI_L; |
|
|
137 |
SCK_H; |
|
|
138 |
SCK_H; |
|
|
139 |
cmd <<= 1; |
|
|
140 |
} |
|
|
141 |
SCK_L; |
|
|
142 |
nSEL_H; |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
void RF_WRITE_DATA(uint8_t data) |
|
|
146 |
{ |
|
|
147 |
uint8_t i; |
|
|
148 |
|
|
|
149 |
for (i=0;i<8;i++) |
|
|
150 |
{ |
|
|
151 |
while (nIRQ_INPUT); |
|
|
152 |
while (!nIRQ_INPUT); |
|
|
153 |
if (data & 0x80)FSK_H; |
|
|
154 |
else FSK_L; |
|
|
155 |
data <<= 1; |
|
|
156 |
} |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
int main() |
|
|
160 |
{ |
|
|
161 |
uint8_t i,j,ChkSum; |
|
|
162 |
|
|
|
163 |
IO_INIT(); |
|
|
164 |
RF_INIT(); |
|
|
165 |
rs232_init(); |
|
|
166 |
LED_H; |
|
|
167 |
delay_ms(100); |
|
|
168 |
|
|
|
169 |
RF_WRITE_CMD(CMD_STATUS); |
|
|
170 |
RF_WRITE_CMD(CMD_SETTING |BAND_868 |C_12pF |TX_DEV_90); |
|
|
171 |
RF_WRITE_CMD(CMD_FREQUENCY |FREQUENCY_868); |
|
|
172 |
RF_WRITE_CMD(0xD040); |
|
|
173 |
RF_WRITE_CMD(CMD_RATE |RATE_19200); |
|
|
174 |
RF_WRITE_CMD(CMD_BATTERY |TX_EBS); |
|
|
175 |
RF_WRITE_CMD(CMD_POWER |POWER_DC); |
|
|
176 |
RF_WRITE_CMD(POWER_OUT_0); |
|
|
177 |
|
|
|
178 |
j= 0; |
|
|
179 |
while (1) |
|
|
180 |
{ |
|
|
181 |
|
|
|
182 |
|
|
|
183 |
LED_L; |
|
|
184 |
RX_ENABLE; |
|
|
185 |
|
|
|
186 |
while ( !(UCSRA & (_BV(RXC)))); |
|
|
187 |
test[0]= UDR; |
|
|
188 |
for (i=1;i<LENGTH_MSG;i++) |
|
|
189 |
{ |
|
|
190 |
j=255; |
|
|
191 |
while ( !(UCSRA & (_BV(RXC))) && --j); |
|
|
192 |
{ |
|
|
193 |
/*asm volatile( "nop" "\n\t" |
|
|
194 |
"nop" "\n\t" |
|
|
195 |
"nop" "\n\t" |
|
|
196 |
"nop" "\n\t" |
|
|
197 |
: |
|
|
198 |
: |
|
|
199 |
);*/ |
|
|
200 |
} |
|
|
201 |
if(j) test[i]= UDR; |
|
|
202 |
else while (i<LENGTH_MSG) test[i++] = 0; |
|
|
203 |
} |
|
|
204 |
i=UDR; |
|
|
205 |
RX_DISABLE; |
|
|
206 |
|
|
|
207 |
LED_H; |
|
|
208 |
|
|
|
209 |
START_TX; |
|
|
210 |
ChkSum = 0; |
|
|
211 |
for (i=0;i<3;i++) RF_WRITE_DATA(0xAA); |
|
|
212 |
RF_WRITE_DATA(0x2D); |
|
|
213 |
RF_WRITE_DATA(0xD4); |
|
|
214 |
|
|
|
215 |
for (i=0;i<LENGTH_MSG;i++) |
|
|
216 |
{ |
|
|
217 |
RF_WRITE_DATA(test[i]); |
|
|
218 |
//putc |
|
|
219 |
ChkSum += test[i]; |
|
|
220 |
} |
|
|
221 |
RF_WRITE_DATA(ChkSum); |
|
|
222 |
RF_WRITE_DATA(0xAA); |
|
|
223 |
RF_WRITE_DATA(0xAA); |
|
|
224 |
STOP_TX; |
|
|
225 |
|
|
|
226 |
|
|
|
227 |
//delay_ms(5000); |
|
|
228 |
//j++; |
|
|
229 |
//test[13]=(j/100)+0x30; |
|
|
230 |
//test[14]=((j%100)/10)+0x30; |
|
|
231 |
//test[15]=((j%100)%10)+0x30; |
|
|
232 |
} |
|
|
233 |
return 0; |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|