main.lst
来自「C8051F320+Nrf24L01无线模块的实现」· LST 代码 · 共 569 行 · 第 1/2 页
LST
569 行
269
270 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
271 SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
272
273 SPI_RW_Reg(WRITE_REG + CONFIG,0x0e);
274
275 CE=1;
276 delays(130);
277 }
278 void init_24L01()
279 { CE=0;
280
281 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
282 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS, TX_ADR_WIDTH);
283
284 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
285 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
286 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
287 SPI_RW_Reg(WRITE_REG + RF_CH,100); // Select RF channel 40
288 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
289 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX_
-DS enabled..
290 CE=1;
291 }
292 /*void RX_Mode(void)
293 {
294 CE=0;
295 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
296
297 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
298 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
299 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
300 SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
301 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
C51 COMPILER V7.50 MAIN 05/25/2010 15:29:58 PAGE 6
302 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
-ed..
303
304 CE = 1; // Set CE pin high to enable RX device
305 delay(130);
306 // This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
-ss
307 // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
308
309 }
310 void TX_Mode(void)
311 {
312 CE=0;
313 // RX_Addr0 same as TX_Adr for Auto.Ack
314 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
315
316 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
317 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
318
319 SPI_RW_Reg(WRITE_REG + RF_CH,40); // Select RF channel 40
320 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
321 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
322 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX
-_DS enabled..
323 CE=1;
324 delays(130);
325
326 */
327
328 /*Got a baby mistake,false key set if key equal 1 enter to function -_-!
329 void led_shake()
330 {
331 led=0;
332 delays(500);
333 led=1;
334 delays(500);
335 } //实现按键循环,不实现连续按键组合 */
336 /*void judge_BT()
337 {
338 int i;
339 uchar *a;
340 uchar *b;
341 uchar *c;
342 switch(t)
343 {
344 case 1:
345
346 a=Original_ADDRESS;
347 b=OnePress_ADDRESS;
348
349 for(i=0;i<5;i++)
350 {
351 *a=*b;
352 a++;
353 b++;
354 } tf=1;
355 break;
356 case 2:
357 i=0;
358 a=Original_ADDRESS;
359 c=TX_ADDRESS;
360
C51 COMPILER V7.50 MAIN 05/25/2010 15:29:58 PAGE 7
361 for(i=0;i<5;i++)
362 {
363 *a=*c;
364 a++;
365 b++;
366 } tf=1;
367 break;
368 }
369 if(tf)
370 {
371 tf=0;
372 Set_TX();
373 }
374 }
375 void CheckButtons()
376 {
377
378 // uchar temp;
379 // key=0xff;
380 // uchar s;
381 if(key==0)
382 {
383 delays(100);
384 if(key==0)
385 {
386 led_shake();
387 t++;
388 }
389 }
390
391 tx_buf[1]='d';
392 Set_TX();
393 delays(100);
394 Set_RX();
395
396 } */
397
398 /*Init HardWare SPI*/
399 void Init_SPI(void)
400 {
401 1 SPI0CFG = 0x40;
402 1 SPI0CN = 0x01; //(This Register can control SCK Pin High Or Low??The function of the pin is Enable/disa
-ble SPI..why add by lsy)
403 1
404 1 SPI0CKR = 0x01;
405 1 }
406 void init_mcu(void)
407 {
408 1 // unsigned int i;
409 1 P0MDIN = 0xff; // 01100010
410 1 P0MDOUT = 0x9d; //0:open-drain, 1:push pull (important Setting,If The port ain't t
-he open-drain Mode,It won't be receive any data from register)add by lsy
411 1 P1MDIN = 0xff; // P1 are digital
412 1 P1MDOUT = 0xff; //0:open-drain, 1:push pull
413 1 P2MDIN = 0xff;
414 1 P2MDOUT = 0xff; // P2.2 and P2.3 set to push-pull
415 1 P3MDIN = 0xff;
416 1 P3MDOUT = 0x00;
417 1 // P0SKIP=0x3f; (This is a question,Wait for answer add by lsy)
418 1 P0 = 0xff;
419 1 P1 = 0xff;
420 1 P2 = 0xff;
C51 COMPILER V7.50 MAIN 05/25/2010 15:29:58 PAGE 8
421 1 P3 = 0xff;
422 1 IT01CF = 0x65;
423 1 XBR0 = 0x02;
424 1 XBR1 = 0x40; // Enable Crossbar
425 1 TMOD = 0x11; //T0,T1 as 16bits Timer mode
426 1 PCON = 0;
427 1 CKCON = 0x76; //Clock Control Register
428 1 TMR2CN = 0x00; //Timer2 Control Register
429 1 TMR3CN = 0xc0; //Timer3 .......
430 1 TMR3RLL = 0x00; //Timer3 Reload Register Low Byte
431 1 TMR3RLH = 0x00; //.......................High..
432 1 TMR2RLL = 0;
433 1 TMR2RLH = 0;
434 1 /*TR0 = 1; //Enable Mcu Timer
435 1 TR1 = 1;
436 1 TR2 = 1;
437 1 ET2 = 1; //Timer interrupt Enable flag
438 1 EX0 = 1;
439 1 EX1 = 1;
440 1 IT0 = 1; //Extern interrput Enable flag
441 1 IT1 = 1;
442 1 PX0 = 1; */
443 1 }
444 /*To check the register value if the register value is ture,make sure the communication pipe */
445 void CheckRS()
446 {
447 1 uchar reg_val;
448 1 reg_val=read_rf_reg(EN_AA);
449 1 reg_val=read_rf_reg(EN_RXADDR);
450 1 reg_val=read_rf_reg(RF_CH);
451 1 reg_val=read_rf_reg(RF_SETUP);
452 1 reg_val=read_rf_reg(SETUP_RETR);
453 1 reg_val=read_rf_reg(CONFIG);
454 1 reg_val=read_rf_reg(RX_PW_P0);
455 1 }
456
457 /*经过测试,CE,CSN,IRQ三个引脚都可以成功的翻转,输出方波,MOSI输出锯齿波,SCK和MISO则无反应*/
458 void main (void)
459 {
460 1
461 1 u16 i = 0;
462 1
463 1 PCA0MD &= ~0x40; // Disable Watchdog timer
464 1 VDM0CN = 0x80; // Enable VDD Monitor
465 1 delay(100); // wait for VDD Monitor stable
466 1 RSTSRC = 0x06;
467 1
468 1 SYSCLK_Init(); //init System Clock //init mcu -_-!
469 1 init_mcu();
470 1 init_io();
471 1 init_int0();
472 1 Init_SPI();
473 1 init_24L01();
474 1 CheckRS();
475 1
476 1 Set_RX();
477 1 while(1)
478 1 {
479 2 if(flag)
480 2 {
481 3 delays(100);
482 3 if(flag)
C51 COMPILER V7.50 MAIN 05/25/2010 15:29:58 PAGE 9
483 3 {
484 4 flag=0;
485 4 led=0;
486 4 delays(500);
487 4 led=~led;
488 4 delays(500);
489 4 }
490 3
491 3 }
492 2 }
493 1 }
494 // CheckButtons();
495 /*for(d=0;d<0xff;d++)
496 {
497 tx_buf[1]=d;
498 }
499 Set_TX();
500 // delays(100);
501
502
503 } */
504
505
506 void ISR_int0(void) interrupt 0
507 {
508 1 sta=read_rf_reg(STATUS); // read register STATUS's value
509 1 if(RX_DR) // if receive data ready (RX_DR) interrupt
510 1 {
511 2 CE=0;
512 2 read_rf_data(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
513 2 CE=1;
514 2 flag=1;
515 2 }
516 1 if(MAX_RT)
517 1 {
518 2 write_rf_reg(FLUSH_TX,0);
519 2 }
520 1
521 1 write_rf_reg(WRITE_REG + STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
522 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 554 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 50 12
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?