Rev Author Line No. Line
1095 mija 1 /* mija 2008
2 demo for RFM12B - TX 868MHz
3  
4 CPU ATMEGA16
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 PC3
43 #define LED_PORT PORTC
44 #define LED_DDR DDRC
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_TX RF_WRITE_CMD(CMD_POWER|POWER_ET|POWER_ES|POWER_EX|POWER_DC)
74 #define STOP_TX RF_WRITE_CMD(CMD_POWER|POWER_DC)
75  
76 //************************************************************************
77  
78 //uint8_t test[16]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x03b,0x3c,0x3d,0x3e,0x3f};
79 uint8_t test[17]="\n\rATmega16\n\r ---";
80 //uint8_t test[16]="0123456789abcdef";
81  
82  
83 //************************************************************************
84  
85 void delay_ms(uint16_t time)
86 {
87 while(time--) _delay_ms(1);
88 }
89  
90 void IO_INIT(void)
91 {
92 SDI_INIT;
93 SDO_INIT;
94 SCK_INIT;
95 nIRQ_INIT;
96 nSEL_INIT;
97 FSK_INIT;
98 LED_INIT;
99 }
100  
101 void RF_INIT(void)
102 {
103 nSEL_H;
104 SDI_H;
105 SCK_L;
106 nIRQ_INPUT;
107 SDO_INPUT;
108 FSK_H;
109 }
110  
111 void RF_WRITE_CMD(uint16_t cmd)
112 {
113 uint8_t i;
114  
115 SCK_L;
116 nSEL_L;
117 for (i=0;i<16;i++)
118 {
119 SCK_L;
120 SCK_L;
121 if (cmd & 0x8000) SDI_H;
122 else SDI_L;
123 SCK_H;
124 SCK_H;
125 cmd <<= 1;
126 }
127 SCK_L;
128 nSEL_H;
129 }
130  
131 void RF_WRITE_DATA(uint8_t data)
132 {
133 while (nIRQ_INPUT);
134 RF_WRITE_CMD(0xB800 + data);
135 }
136  
137 int main()
138 {
139 uint8_t i,j,ChkSum;
140  
141 IO_INIT();
142 RF_INIT();
143 LED_H;
144 delay_ms(100);
145  
146 RF_WRITE_CMD(CMD_SETTING |SETTING_EL |SETTING_EF |BAND_868 |C_12pF );
147 RF_WRITE_CMD(CMD_POWER |POWER_ET |POWER_ES |POWER_EX |POWER_DC );
148 RF_WRITE_CMD(CMD_FREQUENCY |FREQUENCY_868 );
149 RF_WRITE_CMD(CMD_RATE |RATE_9600 );
150 //RF_WRITE_CMD(CMD_RX |RX_P16|VDI_FAST|BANDWIDTH_134|LNA_GAIN_0|DRSSI_103 );
151 //RF_WRITE_CMD(CMD_FILTER |FILTER_AL |DQD_4 );
152 //RF_WRITE_CMD(CMD_FIFO |FIFO_8 |FIFO_DR );
153 //RF_WRITE_CMD(CMD_SYNCFIFO |0xD4 );
154 //RF_WRITE_CMD(CMD_AFC |AFC_POWER_ON|AFC_RANG_8|AFC_OE |AFC_EN );
155 RF_WRITE_CMD(CMD_TX |TX_DEV_45 |TX_POWER_0 );
156 //RF_WRITE_CMD(CMD_PLL |PLL_CLK_25 |PLL_LPX |PLL_DDIT |PLL_BW0 );
157  
158 j= 0;
159 while (1)
160 {
161 LED_H;
162 RF_WRITE_CMD(0x0000);
163  
164 START_TX;
165 ChkSum = 0;
166 for (i=0;i<3;i++) RF_WRITE_DATA(0xAA);
167 RF_WRITE_DATA(0x2D);
168 RF_WRITE_DATA(0xD4);
169  
170 for (i=0;i<16;i++)
171 {
172 RF_WRITE_DATA(test[i]);
173 ChkSum += test[i];
174 }
175 RF_WRITE_DATA(ChkSum);
176 RF_WRITE_DATA(0xAA);
177 RF_WRITE_DATA(0xAA);
178 STOP_TX;
179 LED_L;
180 delay_ms(500);
181 j++;
182 test[13]=(j/100)+0x30;
183 test[14]=((j%100)/10)+0x30;
184 test[15]=((j%100)%10)+0x30;
185 }
186 return 0;
187 }
188  
189  
190