📄 f06x_spi0_eeprom_polled_mode.lst
字号:
400 // Calls all device initialization functions.
401 //
402 //-----------------------------------------------------------------------------
403 void Init_Device (void)
404 {
405 1 Reset_Sources_Init ();
406 1 OSCILLATOR_Init ();
407 1 PORT_Init ();
408 1 TIMER2_Init ();
409 1 UART0_Init ();
410 1 SPI0_Init ();
411 1 }
412
413 //-----------------------------------------------------------------------------
414 // Support Subroutines
415 //-----------------------------------------------------------------------------
416
417 //-----------------------------------------------------------------------------
418 // Delay_us
419 //-----------------------------------------------------------------------------
420 //
421 // Return Value : None
422 // Parameters : 1. time_us - time delay in microseconds
423 // range: 1 to 255
424 //
425 // Creates a delay for the specified time (in microseconds) using TIMER2. The
426 // time tolerance is approximately +/-50 ns (1/SYSCLK + function call time).
427 // This function saves and restores SFRPAGE.
C51 COMPILER V8.08 F06X_SPI0_EEPROM_POLLED_MODE 01/31/2008 14:21:16 PAGE 8
428 //
429 //-----------------------------------------------------------------------------
430 void Delay_us (BYTE time_us)
431 {
432 1 BYTE save_sfrpage = SFRPAGE;
433 1 SFRPAGE = TMR2_PAGE;
434 1
435 1 TR2 = 0; // Stop timer
436 1 TF2 = 0; // Clear timer overflow flag
437 1 TMR2 = -( (UINT)(SYSCLK/1000000) * (UINT)(time_us) );
438 1 TR2 = 1; // Start timer
439 1 while (!TF2); // Wait till timer overflow occurs
440 1 TR2 = 0; // Stop timer
441 1
442 1 SFRPAGE = save_sfrpage;
443 1 }
444
445 //-----------------------------------------------------------------------------
446 // Delay_ms
447 //-----------------------------------------------------------------------------
448 //
449 // Return Value : None
450 // Parameters : 1. time_ms - time delay in milliseconds
451 // range: 1 to 255
452 //
453 // Creates a delay for the specified time (in milliseconds) using TIMER2. The
454 // time tolerance is approximately +/-50 ns (1/SYSCLK + function call time).
455 // This function does not use any SFRs (so SFRPAGE is left untouched).
456 //
457 //-----------------------------------------------------------------------------
458 void Delay_ms (BYTE time_ms)
459 {
460 1 BYTE i;
461 1
462 1 while(time_ms--)
463 1 for(i = 0; i< 10; i++) // 10 * 100 microsecond delay
464 1 Delay_us (100);
465 1 }
466
467 //-----------------------------------------------------------------------------
468 // EEPROM_Write
469 //-----------------------------------------------------------------------------
470 //
471 // Return Value : None
472 // Parameters : 1. address - the destination EEPROM address.
473 // range: 0 to EEPROM_CAPACITY
474 // 2. value - the value to write.
475 // range: 0x00 to 0xFF
476 //
477 // Writes one byte to the specified address in the EEPROM. This function polls
478 // the EEPROM status register after the write operation, and returns only after
479 // the status register indicates that the write cycle is complete. This is to
480 // prevent from having to check the status register before a read operation.
481 // This function saves and restores SFRPAGE.
482 //
483 //-----------------------------------------------------------------------------
484 void EEPROM_Write (UINT address, BYTE value)
485 {
486 1 BYTE save_sfrpage = SFRPAGE;
487 1 SFRPAGE = SPI0_PAGE;
488 1
489 1 // Writing a byte to the EEPROM is a five-step operation.
C51 COMPILER V8.08 F06X_SPI0_EEPROM_POLLED_MODE 01/31/2008 14:21:16 PAGE 9
490 1
491 1 // Step1: Set the Write Enable Latch to 1
492 1 NSSMD0 = 0; // Step1.1: Activate Slave Select
493 1 SPI0DAT = EEPROM_CMD_WREN; // Step1.2: Send the WREN command
494 1 while (!SPIF); // Step1.3: Wait for end of transfer
495 1 SPIF = 0; // Step1.4: Clear the SPI intr. flag
496 1 NSSMD0 = 1; // Step1.5: Deactivate Slave Select
497 1 Delay_us (1); // Step1.6: Wait for at least
498 1 // T_NSS_DISABLE_MIN
499 1 // Step2: Send the WRITE command
500 1 NSSMD0 = 0;
501 1 SPI0DAT = EEPROM_CMD_WRITE;
502 1 while (!SPIF);
503 1 SPIF = 0;
504 1
505 1 // Step3: Send the EEPROM destination address (MSB first)
506 1 SPI0DAT = (BYTE)((address >> 8) & 0x00FF);
507 1 while (!SPIF);
508 1 SPIF = 0;
509 1 SPI0DAT = (BYTE)(address & 0x00FF);
510 1 while (!SPIF);
511 1 SPIF = 0;
512 1
513 1 // Step4: Send the value to write
514 1 SPI0DAT = value;
515 1 while (!SPIF);
516 1 SPIF = 0;
517 1 NSSMD0 = 1;
518 1 Delay_us (1);
519 1
520 1 // Step5: Poll on the Write In Progress (WIP) bit in Read Status Register
521 1 do
522 1 {
523 2 NSSMD0 = 0; // Activate Slave Select
524 2 SPI0DAT = EEPROM_CMD_RDSR; // Send the Read Status Register command
525 2 while (!SPIF); // Wait for the command to be sent out
526 2 SPIF = 0;
527 2 SPI0DAT = 0; // Dummy write to output serial clock
528 2 while (!SPIF); // Wait for the register to be read
529 2 SPIF = 0;
530 2 NSSMD0 = 1; // Deactivate Slave Select after read
531 2 Delay_us (1);
532 2 } while( (SPI0DAT & 0x01) == 0x00 );
533 1
534 1 SFRPAGE = save_sfrpage;
535 1 }
536
537 //-----------------------------------------------------------------------------
538 // EEPROM_Read
539 //-----------------------------------------------------------------------------
540 //
541 // Return Value : The value that was read from the EEPROM
542 // range: 0x00 to 0xFF
543 // Parameters : 1. address - the source EEPROM address.
544 // range: 0 to EEPROM_CAPACITY
545 //
546 // Reads one byte from the specified EEPROM address.
547 // This function saves and restores SFRPAGE.
548 //
549 //-----------------------------------------------------------------------------
550 BYTE EEPROM_Read (UINT address)
551 {
C51 COMPILER V8.08 F06X_SPI0_EEPROM_POLLED_MODE 01/31/2008 14:21:16 PAGE 10
552 1 BYTE value;
553 1 BYTE save_sfrpage = SFRPAGE;
554 1 SFRPAGE = SPI0_PAGE;
555 1
556 1 // Reading a byte from the EEPROM is a three-step operation.
557 1
558 1 // Step1: Send the READ command
559 1 NSSMD0 = 0; // Activate Slave Select
560 1 SPI0DAT = EEPROM_CMD_READ;
561 1 while (!SPIF);
562 1 SPIF = 0;
563 1
564 1 // Step2: Send the EEPROM source address (MSB first)
565 1 SPI0DAT = (BYTE)((address >> 8) & 0x00FF);
566 1 while (!SPIF);
567 1 SPIF = 0;
568 1 SPI0DAT = (BYTE)(address & 0x00FF);
569 1 while (!SPIF);
570 1 SPIF = 0;
571 1
572 1 // Step3: Read the value returned
573 1 SPI0DAT = 0; // Dummy write to output serial clock
574 1 while (!SPIF); // Wait for the value to be read
575 1 SPIF = 0;
576 1 NSSMD0 = 1; // Deactivate Slave Select
577 1 Delay_us (1);
578 1 value = SPI0DAT; // Store EEPROM value in local var.
579 1
580 1 SFRPAGE = save_sfrpage;
581 1 return value;
582 1 }
583
584
585 //-----------------------------------------------------------------------------
586 // End Of File
587 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 681 ----
CONSTANT SIZE = 222 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 5
IDATA SIZE = ---- ----
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 + -