-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.LST
More file actions
executable file
·375 lines (362 loc) · 16.2 KB
/
main.LST
File metadata and controls
executable file
·375 lines (362 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 1
C51 COMPILER V9.52.0.0, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE main.c BROWSE INCDIR(C:\Nordic Semiconductor\nRFgo SDK 2.3.0.10040\source_c
-ode\compiler\c51;C:\Nordic Semiconductor\nRFgo SDK 2.3.0.10040\source_code\hal\nrf24le1;C:\Nordic Semiconductor\nRFgo SD
-K 2.3.0.10040\source_code\compiler\common) DEBUG OBJECTEXTEND TABS(2)
line level source
1 /******************************************************/
2 /* I2C Slave example code
3 /* Wait for data, increase the first byte by 1,
4 /* Send back the first byte increased when requested.
5 /******************************************************/
6
7 #include "nrf24le1.h"
8 #include <hal_w2.h>
*** WARNING C320 IN LINE 57 OF C:\Nordic Semiconductor\nRFgo SDK 2.3.0.10040\source_code\hal\nrf24le1\hal_w2.h: "LE1 pac
-kage type not defined! Add project wide definition depending on the LE1 package variant you are using. Valid options are
- NRF24LE1_48, NRF24LE1_32 and NRF24LE1_24."
9 #include "ringbuf.h"
10 #include <stdint.h>
11 #include "API.h"
12 #define LED0 P15
13 #define LED1 P16
14 #define signal P01
15 #define TIMEOUT 53333 //40ms
16 #define RINGBUFSIZE 32
17 uint16_t flag =0;
18 uint8_t flag2 =0;
19 uint8_t int_count=0;
20 uint8_t TX =0;
21 uint8_t first_byte=0;
22
23 uint16_t timeout_flag = 0;
24
25 static struct ringbuf txbuf;
26 uint8_t txbuf_data[RINGBUFSIZE];
27 uint8_t sladdress = 0x07;
28
29 #define INTERRUPT_RFIRQ 9
30 #define TX_ADR_WIDTH 5 // RF收发地址共5 bytes
31 #define TX_PLOAD_WIDTH 9 // 数据包长度为20 bytes
32
33 uint8_t const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x56,0x78,0x90,0x15}; // 定义RF收发地址
34
35 uint8_t data rx_buf[TX_PLOAD_WIDTH];
36
37 uint8_t bdata sta;
38 sbit RX_DR =sta^6;
39 sbit TX_DS =sta^5;
40 sbit MAX_RT =sta^4;
41
42 static uint8_t dat_mod = 0; //0=len,1=data
43 static uint8_t sent_len = 0; //ringbuf len
44
45 //---------------------------------------------------------
46 /**************************************************
47 功能:延时
48 **************************************************/
49 void delay(uint16_t x)
50 {
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 2
51 1 uint16_t i,j;
52 1 i=0;
53 1 for(i=0;i<x;i++)
54 1 {
55 2 j=108;
56 2 ;
57 2 while(j--);
58 2 }
59 1 }
60 /**************************************************
61 功能:硬件SPI读写
62 **************************************************/
63 uint8_t SPI_RW(uint8_t value)
64 {
65 1 SPIRDAT = value;
66 1
67 1 while(!(SPIRSTAT & 0x02)); // 等待SPI传输完成
68 1
69 1 return SPIRDAT; // 返回读出值
70 1 }
71 /**************************************************
72 功能:写RF寄存器,读RF状态值
73 **************************************************/
74 uint8_t SPI_RW_Reg(uint8_t reg, uint8_t value)
75 {
76 1 uint8_t status;
77 1
78 1 RFCSN = 0;
79 1 status = SPI_RW(reg); // 选择RF寄存器
80 1 SPI_RW(value); // 写入数据
81 1 RFCSN = 1;
82 1
83 1 return(status); // 返回RF状态值
84 1 }
85 /**************************************************
86 功能:读RF寄存器
87 **************************************************/
88 uint8_t SPI_Read(uint8_t reg)
89 {
90 1 uint8_t reg_val;
91 1
92 1 RFCSN = 0;
93 1 SPI_RW(reg); // 选择RF寄存器
94 1 reg_val = SPI_RW(0); // 读出数据
95 1 RFCSN = 1;
96 1
97 1 return(reg_val); // 返回RF状态值
98 1 }
99 /**************************************************
100 功能:读RF寄存器多字节数据到缓冲区
101 **************************************************/
102 uint8_t SPI_Read_Buf(uint8_t reg, uint8_t *pBuf, uint8_t bytes)
103 {
104 1 uint8_t status,byte_ctr;
105 1
106 1 RFCSN = 0;
107 1 status = SPI_RW(reg); // 选择RF寄存器
108 1
109 1 for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
110 1 pBuf[byte_ctr] = SPI_RW(0); // 连接读出数据
111 1
112 1 RFCSN = 1;
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 3
113 1
114 1 return(status); // 返回RF状态值
115 1 }
116 /**************************************************
117 功能:把缓冲区的多字节数据写到RF寄存器
118 **************************************************/
119 uint8_t SPI_Write_Buf(uint8_t reg, uint8_t *pBuf, uint8_t bytes)
120 {
121 1 uint8_t status,byte_ctr;
122 1
123 1 RFCSN = 0;
124 1 status = SPI_RW(reg); // 选择RF寄存器
125 1 for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // 连接写入数据
126 1 SPI_RW(*pBuf++);
127 1 RFCSN = 1;
128 1 return(status); // 返回RF状态值
129 1 }
130
131 /**************************************************
132 功能:设置为接收模式
133 **************************************************/
134 void RX_Mode(void)
135 {
136 1 RFCE=0;
137 1 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // 上电, CRC为2 bytes,接收模式,允许RX_DR产生中断
138 1 RFCE = 1; // 启动接收模式
139 1 }
140 /**************************************************
141 功能:RF初始化
142 **************************************************/
143 void rf_init(void)
144 {
145 1 RFCE = 0; // RF关闭
146 1 RFCKEN = 1; // 启动RF时钟
147 1 RF = 1; // 允许RF中断
148 1 IP1 |= 0x02; //RF优先级最高:3
149 1 IP0 |= 0x02;
150 1
151 1 delay(1000);
152 1 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 设置发射地址长度
153 1 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 设置接收地址长度
154 1 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 启动自动应答功能
155 1 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // PIPE0接收数据
156 1 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 自动重传10次
157 1 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // RF频率2440MHz
158 1 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); // 发射功率0dBm, 传输速率2Mbps,
159 1 SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // PIPE0 接收数据包长度
160 1 }
161 /**************************************************
162 功能:RF中断服务程序
163 **************************************************/
164 void RF_IRQ(void) interrupt INTERRUPT_RFIRQ
165 {
166 1 sta=SPI_Read(STATUS); // 读出状态值
167 1
168 1 if(RX_DR)
169 1 {
170 2 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// 读出FIFO的数据
171 2 if(ringbuf_elements(&txbuf) <27) {//只用其中的24个字节的缓存,因为后面8个字节只能用其中的7个
172 3 ringbuf_put(&txbuf,rx_buf[0]); // 显示所有ID值
173 3 ringbuf_put(&txbuf,rx_buf[1]);
174 3 ringbuf_put(&txbuf,rx_buf[2]);
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 4
175 3 ringbuf_put(&txbuf,rx_buf[3]);
176 3 ringbuf_put(&txbuf,rx_buf[4]);
177 3 ringbuf_put(&txbuf,rx_buf[5]);
178 3 ringbuf_put(&txbuf,rx_buf[6]);
179 3 ringbuf_put(&txbuf,rx_buf[7]);
180 3 ringbuf_put(&txbuf,rx_buf[8]);
181 3 }
182 2 LED0=!LED0;
183 2 //trigger reader request from pi
184 2 // make sure to use only 24 bytes buf, to avoid last byte's missing in ringbuf
185 2
186 2 signal = !signal;
187 2
188 2 // flag++;
189 2 // if(1 == flag){
190 2 // signal =0;
191 2 // }
192 2 // if(3 == flag){
193 2 // signal =1;
194 2 // flag = 0;
195 2 // }
196 2
197 2
198 2 SPI_RW_Reg(FLUSH_RX,0); // 清除RX的FIFO
199 2 }
200 1
201 1 SPI_RW_Reg(WRITE_REG+STATUS,0x70); // 清除所有中断标志
202 1 }
203 /**************************************************
204 功能:I2C中断服务程序
205 **************************************************/
206 void I2C_IRQ (void) interrupt INTERRUPT_SERIAL
207 {
208 1
209 1 uint8_t w2con1_value;
210 1 w2con1_value = W2CON1;
211 1
212 1 if ((w2con1_value & 0x04) ) //Interrupt for address matched
213 1 {
214 2 // Check if RX or TX
215 2 int_count++;
216 2 if (W2DAT&0x01) //TX first tx to pi
217 2 {
218 3 TX=1; //TX
219 3 //W2DAT=flag;
220 3 if(0 == dat_mod){//first get len
221 4 W2DAT=ringbuf_elements(&txbuf);
222 4 //W2DAT = 8;
223 4 }else{//second get to get all data in ring buf
224 4 W2DAT = ringbuf_get(&txbuf);
225 4 //W2DAT = 0x06;
226 4 }
227 3
228 3 }
229 2 else //RX
230 2 {
231 3 TX=0; //RX
232 3 first_byte=1;
233 3 }
234 2 }
235 1 else
236 1 {
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 5
237 2 if ((w2con1_value & 0x01)) // Interrupt for byte sent or received
238 2 {
239 3 if (TX==1) //next byte to send
240 3 {
241 4 if (!(w2con1_value & 0x02)) // if not NAK
242 4 {
243 5 if(0 == dat_mod){
244 6
245 6 }else{
246 6 W2DAT = ringbuf_get(&txbuf);
247 6 //W2DAT = 0x06;
248 6 }
249 5 }
250 4 }
251 3 else //byte to receive
252 3 {
253 4 dat_mod=W2DAT;
254 4 }
255 3
256 3 }
257 2 }
258 1 }
259 /**************************************************
260 功能:io初始化
261 **************************************************/
262 void io_init()
263 {
264 1 P0DIR = 0x00; //OUTPUT
265 1 P1DIR = 0x00;//OUTPUT
266 1 P2DIR = 0xFF;
267 1 P3DIR = 0xFF;
268 1 }
269 /**************************************************
270 功能:Timer0初始化
271 **************************************************/
272 void time_init()
273 {
274 1 TMOD |= 0x01; //16 bit timer
275 1 //TMOD |= 0x11; //16 bit timer
276 1 TH0=(65536-TIMEOUT)/256;
277 1 TL0=(65536-TIMEOUT)%256;
278 1 ET0=1; //Enable interupt
279 1 TR0=1;
280 1 }
281 /**************************************************
282 功能:I2C初始化
283 **************************************************/
284 void i2c_init()
285 {
286 1 hal_w2_enable(1);
287 1 hal_w2_set_clk_freq(HAL_W2_400KHZ);
288 1 hal_w2_set_op_mode(HAL_W2_SLAVE);
289 1 hal_w2_set_slave_address(sladdress);// Slave ADDRESS 0x07
290 1 hal_w2_alter_clock(1);
291 1 hal_w2_irq_adr_match_enable(1);
292 1 hal_w2_irq_stop_cond_enable(0);
293 1 INTEXP |= 0x04;
294 1 IEN1 |= 0x04;
295 1
296 1 }
297 /**************************************************
298 功能:main函数
C51 COMPILER V9.52.0.0 MAIN 02/26/2016 11:44:41 PAGE 6
299 **************************************************/
300 void main()
301 {
302 1 io_init();
303 1 rf_init();
304 1 ringbuf_init(&txbuf,txbuf_data,RINGBUFSIZE); //******************************Init ringbuf
305 1 i2c_init();
306 1 time_init();
307 1
308 1 EA= 1; //enable all interrupt
309 1
310 1 RX_Mode(); //进入接收模式
311 1
312 1 while(1)
313 1 {
314 2 if(RX_DR) // 数据已收到
315 2 {
316 3 sta=0;
317 3 }
318 2 }
319 1
320 1 }
321
322 void Timer0_IRQ(void) interrupt INTERRUPT_T0
323 {
324 1 TH0=(65536-TIMEOUT)/256;
325 1 TL0=(65536-TIMEOUT)%256;
326 1
327 1 timeout_flag++;
328 1 LED1 = !LED1;
329 1
330 1 if((timeout_flag >= 250) && (ringbuf_elements(&txbuf) != 0)) //250*40 = 10000ms;
331 1 {
332 2 signal =! signal;
333 2 }
334 1 }
335
336
337
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 838 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 64 11
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)