1 | #include <avr/io.h>
|
2 | #include <stdlib.h>
|
3 | #include <avr/interrupt.h>
|
4 | #define F_CPU 10000000UL
|
5 | #include <util/delay.h>
|
6 |
|
7 |
|
8 | #define RF_PORT PORTB
|
9 | #define RF_DDR DDRB
|
10 | #define RF_PIN PINB
|
11 |
|
12 | #define DDR_IN 0
|
13 | #define DDR_OUT 1
|
14 |
|
15 | #define PORT_SEL PORTB
|
16 | #define PIN_SEL PINB
|
17 | #define DDR_SEL DDRB
|
18 |
|
19 | #define PORT_SDI PORTB
|
20 | #define PIN_SDI PINB
|
21 | #define DDR_SDI DDRB
|
22 |
|
23 | #define PORT_SCK PORTB
|
24 | #define PIN_SCK PINB
|
25 | #define DDR_SCK DDRB
|
26 |
|
27 | #define PORT_NIRQ PORTB
|
28 | #define PIN_NIRQ PINB
|
29 | #define DDR_NIRQ DDRB
|
30 |
|
31 | #define SCK 7 // SCK, -> RF02
|
32 | #define NIRQ 6 // nIRQ, -> RF02
|
33 | #define SDI 5 // SDI, -> RF02
|
34 | #define SEL 4 // nSEL, -> RF02
|
35 | #define DATA 3 // FSK, -> RF02
|
36 |
|
37 |
|
38 | #define SEL_OUTPUT() DDR_SEL |= (1<<SEL)
|
39 | #define HI_SEL() PORT_SEL |= (1<<SEL)
|
40 | #define LOW_SEL() PORT_SEL &=~(1<<SEL)
|
41 |
|
42 | #define SDI_OUTPUT() DDR_SDI |= (1<<SDI)
|
43 | #define HI_SDI() PORT_SDI |= (1<<SDI)
|
44 | #define LOW_SDI() PORT_SDI &=~(1<<SDI)
|
45 |
|
46 | #define NIRQ_INPUT() DDR_NIRQ &=~(1<<NIRQ)
|
47 | #define NIRQ_HI() PIN_NIRQ&(1<<NIRQ)
|
48 |
|
49 | #define SCK_OUTPUT() DDR_SCK |=(1<<SCK)
|
50 | #define HI_SCK() PORT_SCK |=(1<<SCK)
|
51 | #define LOW_SCK() PORT_SCK &=~(1<<SCK)
|
52 |
|
53 |
|
54 | unsigned int RFXX_WRT_CMD( unsigned int aCmd) { // to send commands
|
55 |
|
56 | unsigned char i;
|
57 | unsigned int temp;
|
58 |
|
59 | LOW_SCK(); // reset bit SCK
|
60 | LOW_SEL(); // reset bit SEL
|
61 | for(i=0;i<16;i++)
|
62 | {
|
63 | temp<<=1; // temp=temp<<1;
|
64 | if(NIRQ_HI()) {
|
65 | temp|=0x0001; // temp is a 16-bit integer
|
66 | }
|
67 | LOW_SCK(); // reset bit SCK
|
68 | if(aCmd&0x8000) { // if((aCmd&0x8000)==0x8000)
|
69 | HI_SDI(); // set bit SDI
|
70 | }
|
71 | else {
|
72 | LOW_SDI();// reset bit SDI
|
73 | }
|
74 | HI_SCK(); // set bit SCK to shift data from SDI pin to RFM
|
75 | aCmd<<=1; // aCmd=aCmd<<1
|
76 | }
|
77 | LOW_SCK(); // reset bit SCK
|
78 | HI_SEL(); // set bit SEL
|
79 | return(temp);
|
80 | }
|
81 |
|
82 |
|
83 | void RF02B_SEND(unsigned char aByte) { // send a byte
|
84 |
|
85 | unsigned char i;
|
86 |
|
87 | for(i=0;i<8;i++)
|
88 | {
|
89 | while(NIRQ_HI()); // polling nIRQ
|
90 | while(!(NIRQ_HI()));
|
91 | if(aByte&0x80)
|
92 | {
|
93 | PORTB|=(1<<DATA); // set bit(FSK): request data input
|
94 | }
|
95 | else {
|
96 | PORTB&=~(1<<DATA); // reset bit(FSK): request data input
|
97 | }
|
98 | aByte<<=1; // aByte=aByte<<1
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 | void RFXX_PORT_INIT(void) { // initialise all neccesary ports
|
104 | HI_SEL(); // set bit SEL
|
105 | HI_SDI(); // set bit SDI
|
106 | LOW_SCK(); // reset bit SCK
|
107 | SEL_OUTPUT(); // set port SEL as output
|
108 | SDI_OUTPUT(); // set port SDI as output
|
109 | NIRQ_INPUT(); // set port SDO as input
|
110 | SCK_OUTPUT(); // set port SCK as output
|
111 | }
|
112 |
|
113 |
|
114 | void Send (unsigned int byte) {
|
115 |
|
116 | unsigned int i, j, ChkSum;
|
117 |
|
118 | ChkSum=0;
|
119 | RF02B_SEND(0xAA); // PREAMBLE
|
120 | RF02B_SEND(0xAA); // PREAMBLE
|
121 | RF02B_SEND(0xAA); // PREAMBLE
|
122 | RF02B_SEND(0x54); // Sync byte
|
123 | RF02B_SEND(byte); // Data0
|
124 | ChkSum+=byte;
|
125 | RF02B_SEND(byte); // Data1
|
126 | ChkSum+=byte;
|
127 | RF02B_SEND(byte); // Data2
|
128 | ChkSum+=byte;
|
129 | RF02B_SEND(byte); // Data3
|
130 | ChkSum+=byte;
|
131 | RF02B_SEND(byte); // Data4
|
132 | ChkSum+=byte;
|
133 | RF02B_SEND(byte); // Data5
|
134 | ChkSum+=byte;
|
135 | RF02B_SEND(byte); // Data6
|
136 | ChkSum+=byte;
|
137 | RF02B_SEND(byte); // Data7
|
138 | ChkSum+=byte;
|
139 | RF02B_SEND(ChkSum);// Data8
|
140 | RF02B_SEND(0xAA); // dummy byte
|
141 |
|
142 | RFXX_WRT_CMD(0xC401); // (8) TX off
|
143 |
|
144 | for(i=0;i<5000;i++)
|
145 | for(j=0;j<123;j++);
|
146 | }
|
147 |
|
148 | void main(void) {
|
149 |
|
150 | RFXX_PORT_INIT(); // initialise all neccesary ports
|
151 |
|
152 | RFXX_WRT_CMD(0xCC00); // read internal status register content
|
153 |
|
154 | RFXX_WRT_CMD(0x9731); // (2)
|
155 | // b1=1,d1=1,d0=1,x1=1,x0=1,m0=1
|
156 | // CLK freq 10MHz
|
157 | // Band 868MHz
|
158 | // 10pF
|
159 | // 60kHz deviation
|
160 |
|
161 | RFXX_WRT_CMD(0xC031); // (3)
|
162 | // ex=1,es=1,dc=1
|
163 | // enable crystal oscillator
|
164 | // enable synthesizer
|
165 | // disable CLK pin
|
166 | // POWER AMPLIFY aanzetten?
|
167 |
|
168 | RFXX_WRT_CMD(0xA640); // (4) 868MHz ( 860MHz + 6MHz )
|
169 |
|
170 | RFXX_WRT_CMD(0xC823); // (5) 4.8kbps 0xC823 << nog delen door 2 (0xD040)
|
171 | // r6=1,r2=1,r1=1,r0=1
|
172 |
|
173 | RFXX_WRT_CMD(0xB000); // (6) no relative power output
|
174 |
|
175 | RFXX_WRT_CMD(0xC220); // (7) ebs=1
|
176 | // enable TX sync
|
177 |
|
178 | RFXX_WRT_CMD(0xC464); // (8) TX off after 10us (0xC464)
|
179 |
|
180 | PORTB|=(1<<DATA); // set bit FSK
|
181 | DDRB|=(1<DATA); // set port FSK as output
|
182 |
|
183 |
|
184 | while(1) {
|
185 | for( int i=0;i<=50;i++)
|
186 | Send(0x0F);
|
187 | for( int i=0;i<=50;i++)
|
188 | Send(0xF0);
|
189 | }
|
190 | }
|