Rev Author Line No. Line
1096 mija 1 /* mija 2008
2 demo for RFM02 - TX 868MHz
3  
4 CPU ATMEGA16
5 fcpu = 1MHz
6  
7 !! define PIN,PORT,DDR for IOpin !!
8  
9 tested with module RFM12B RX 9600 BW 134kHz
10 */
11  
12 #include <avr/io.h>
13 #include <util/delay.h>
14 #include "RFM02.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 PC3
45 #define LED_PORT PORTC
46 #define LED_DDR DDRC
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_TX RF_WRITE_CMD(CMD_POWER|POWER_EX|POWER_ES|POWER_EA|POWER_DC)
76 #define STOP_TX RF_WRITE_CMD(CMD_POWER|POWER_DC)
77  
78 //************************************************************************
79  
80 //uint8_t test[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x03b,0x3c,0x3d,0x3e,0x3f};
81 uint8_t test[17]="\n\rATmega16\n\r ---";
82 //uint8_t test[16]="0123456789abcdef";
83  
84  
85 //************************************************************************
86  
87 void delay_ms(uint16_t time)
88 {
89 while(time--) _delay_ms(1);
90 }
91  
92 void IO_INIT(void)
93 {
94 SDI_INIT;
95 SDO_INIT;
96 SCK_INIT;
97 nIRQ_INIT;
98 nSEL_INIT;
99 FSK_INIT;
100 LED_INIT;
101 }
102  
103 void RF_INIT(void)
104 {
105 nSEL_H;
106 SDI_H;
107 SCK_L;
108 nIRQ_INPUT;
109 SDO_INPUT;
110 FSK_H;
111 }
112  
113 void RF_WRITE_CMD(uint16_t cmd)
114 {
115 uint8_t i;
116  
117 SCK_L;
118 nSEL_L;
119 for (i=0;i<16;i++)
120 {
121 SCK_L;
122 SCK_L;
123 if (cmd & 0x8000) SDI_H;
124 else SDI_L;
125 SCK_H;
126 SCK_H;
127 cmd <<= 1;
128 }
129 SCK_L;
130 nSEL_H;
131 }
132  
133 void RF_WRITE_DATA(uint8_t data)
134 {
135 uint8_t i;
136  
137 for (i=0;i<8;i++)
138 {
139 while (nIRQ_INPUT);
140 while (!nIRQ_INPUT);
141 if (data & 0x80)FSK_H;
142 else FSK_L;
143 data <<= 1;
144 }
145 }
146  
147 int main()
148 {
149 uint8_t i,j,ChkSum;
150  
151 IO_INIT();
152 RF_INIT();
153 LED_H;
154 delay_ms(100);
155  
156 RF_WRITE_CMD(CMD_STATUS);
157 RF_WRITE_CMD(CMD_SETTING |BAND_868 |C_12pF |TX_DEV_90);
158 RF_WRITE_CMD(CMD_FREQUENCY |FREQUENCY_868);
159 RF_WRITE_CMD(0xD040);
160 RF_WRITE_CMD(CMD_RATE |RATE_19200);
161 RF_WRITE_CMD(CMD_BATTERY |TX_EBS);
162 RF_WRITE_CMD(CMD_POWER |POWER_DC);
163 RF_WRITE_CMD(POWER_OUT_0);
164  
165 j= 0;
166 while (1)
167 {
168 LED_H;
169  
170 START_TX;
171 ChkSum = 0;
172 for (i=0;i<3;i++) RF_WRITE_DATA(0xAA);
173 RF_WRITE_DATA(0x2D);
174 RF_WRITE_DATA(0xD4);
175  
176 for (i=0;i<16;i++)
177 {
178 RF_WRITE_DATA(test[i]);
179 ChkSum += test[i];
180 }
181 RF_WRITE_DATA(ChkSum);
182 RF_WRITE_DATA(0xAA);
183 RF_WRITE_DATA(0xAA);
184 STOP_TX;
185  
186 LED_L;
187 delay_ms(500);
188 j++;
189 test[13]=(j/100)+0x30;
190 test[14]=((j%100)/10)+0x30;
191 test[15]=((j%100)%10)+0x30;
192 }
193 return 0;
194 }
195  
196  
197