📄 89lv51.lst
字号:
213 /**************************************************/
214
215 /**************************************************
216 Function: RX_Mode();
217
218 Description:
219 This function initializes one nRF24L01 device to
220 RX Mode, set RX address, writes RX payload width,
221 select RF channel, datarate & LNA HCURR.
222 After init, CE is toggled high, which means that
223 this device is now ready to receive a datapacket.
224 /**************************************************/
225 void RX_Mode(void)
226 {
227 1 CE=0;
228 1 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX devi
-ce as the TX device
229 1
230 1 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
231 1 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
232 1 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
233 1 SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
234 1 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
235 1 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
-ed..
236 1
237 1 CE = 1; // Set CE pin high to enable RX device
238 1
C51 COMPILER V6.12 89LV51 05/24/2008 09:40:24 PAGE 5
239 1 // This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
-ss
240 1 // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
241 1
242 1 }
243 /**************************************************/
244
245 /**************************************************
246 Function: TX_Mode();
247
248 Description:
249 This function initializes one nRF24L01 device to
250 TX mode, set TX address, set RX address for auto.ack,
251 fill TX payload, select RF channel, datarate & TX pwr.
252 PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
253
254 ToDo: One high pulse(>10us) on CE will now send this
255 packet and expext an acknowledgment from the RX device.
256 /**************************************************/
257 void TX_Mode(void)
258 {
259 1 CE=0;
260 1
261 1 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
262 1 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ac
-k
263 1 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
264 1
265 1 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
266 1 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
267 1 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
268 1 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
269 1 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
270 1 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX
-_DS enabled..
271 1 CE=1;
272 1
273 1 }
274 /**************************************************/
275
276 /**************************************************
277 Function: check_ACK();
278
279 Description:
280 check if have "Data sent TX FIFO interrupt",if TX_DS=1,
281 all led light and after delay 100ms all led close
282 /**************************************************
283 void check_ACK()
284 {
285 uchar test;
286 test=SPI_Read(READ_REG+STATUS); // read register STATUS's
287 test=test&0x20; // check if have Data sent TX FIFO interrupt (TX_DS=1)
288 if(test==0x20) // TX_DS =1
289 {
290 P0=0x00; // turn on all led
291 delay100(); // delay 100ms
292 P0=0xff;
293 }
294 }
295 /**************************************************/
296
297 /**************************************************
C51 COMPILER V6.12 89LV51 05/24/2008 09:40:24 PAGE 6
298 Function: TxData();
299
300 Description:
301 write data x to SBUF
302 /**************************************************/
303 void TxData (uchar x)
304 {
305 1 SBUF=x; // write data x to SBUF
306 1 while(TI==0);
307 1 TI=0;
308 1 }
309 /**************************************************/
310
311 /**************************************************
312 Function: CheckButtons();
313
314 Description:
315 check buttons ,if have press,read the key values,
316 turn on led and transmit it; after transmition,
317 if received ACK, clear TX_DS interrupt and enter RX Mode;
318 turn off the led
319 /**************************************************/
320 void CheckButtons()
321 {
322 1 uchar Temp,xx,Tempi;
323 1
324 1 P0=0xff;
325 1 Temp=P0&KEY; //read key value from port P0
326 1 if (Temp!=KEY)
327 1 {
328 2 delay_ms(10);
329 2 Temp=P0&KEY; // read key value from port P0
330 2 if (Temp!=KEY)
331 2 {
332 3 xx=Temp;
333 3 Tempi=Temp>>1; // Left shift 4 bits
334 3 P0=Tempi; // Turn On the led
335 3 tx_buf[0]=Tempi; // Save to tx_buf[0]
336 3 TX_Mode(); // set TX Mode and transmitting
337 3 TxData(xx); // send data to uart
338 3 //check_ACK(); // if have acknowledgment from RX device,turn on all led
339 3 SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
340 3 delay_ms(500);
341 3 P0=0xff; // Turn off the led
342 3 RX_Mode(); // set receive mode
343 3
344 3 while((P0&KEY)!=KEY);
345 3 }
346 2 }
347 1 }
348 /**************************************************/
349
350 /**************************************************
351 Function: main();
352
353 Description:
354 control all subprogrammes;
355 /**************************************************/
356 void main(void)
357 {
358 1 uchar xx;
359 1 init_io(); // Initialize IO port
C51 COMPILER V6.12 89LV51 05/24/2008 09:40:24 PAGE 7
360 1 Inituart(); // initialize 232 uart
361 1 //init_int0(); // enable int0 interrupt
362 1 RX_Mode(); // set RX mode
363 1 while(1)
364 1 {
365 2 CheckButtons(); // scan key value and transmit
366 2 sta=SPI_Read(STATUS); // read register STATUS's value
367 2 if(RX_DR) // if receive data ready (RX_DR) interrupt
368 2 {
369 3 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
370 3 flag=1;
371 3 }
372 2 if(MAX_RT)
373 2 {
374 3 SPI_RW_Reg(FLUSH_TX,0);
375 3 }
376 2 SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
377 2 if(flag) // finish received
378 2 {
379 3 flag=0; // set flag=0
380 3 P0=rx_buf[0]; // turn on led
381 3 delay_ms(500);
382 3 P0=0xff; // turn off led
383 3 xx=rx_buf[0]>>1;// right shift 4 bits
384 3 TxData(xx); // send data to uart
385 3 }
386 2 }
387 1 }
388 /**************************************************/
389
390 /**************************************************
391 Function: ISR_int0() interrupt 0;
392
393 Description:
394 if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
395 if RX_DR=1,read the payload from RX_FIFO and set flag;
396 /**************************************************/
397 void ISR_int0(void) interrupt 0
398 {
399 1 sta=SPI_Read(STATUS); // read register STATUS's value
400 1 if(RX_DR) // if receive data ready (RX_DR) interrupt
401 1 {
402 2 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
403 2 flag=1;
404 2 }
405 1 if(MAX_RT)
406 1 {
407 2 SPI_RW_Reg(FLUSH_TX,0);
408 2 }
409 1 SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
410 1 }
411 /**************************************************/
412
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 633 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 47 13
IDATA SIZE = ---- ----
C51 COMPILER V6.12 89LV51 05/24/2008 09:40:24 PAGE 8
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -