📄 f33x_smbus_eeprom.lst
字号:
516 3 pSMB_DATA_IN++; // Increment data in pointer
517 3 i++; // Increment number of bytes received
518 3 ACK = 1; // Set ACK bit (may be cleared later
519 3 // in the code)
520 3
521 3 }
522 2
523 2 if (i == SMB_DATA_LEN) // This is the last byte
524 2 {
525 3 SMB_BUSY = 0; // Free SMBus interface
526 3 ACK = 0; // Send NACK to indicate last byte
527 3 // of this transfer
528 3 STO = 1; // Send STOP to terminate transfer
529 3 }
530 2
531 2 break;
532 2
533 2 default:
534 2 FAIL = 1; // Indicate failed transfer
535 2 // and handle at end of ISR
536 2 break;
537 2 }
538 1
539 1 if (FAIL) // If the transfer failed,
540 1 {
541 2 SMB0CF &= ~0x80; // Reset communication
542 2 SMB0CF |= 0x80;
543 2 STA = 0;
544 2 STO = 0;
545 2 ACK = 0;
546 2
547 2 SMB_BUSY = 0; // Free SMBus
548 2
549 2 FAIL = 0;
550 2 }
551 1
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 10
552 1 SI = 0; // Clear interrupt flag
553 1 }
554
555 //-----------------------------------------------------------------------------
556 // Timer3 Interrupt Service Routine (ISR)
557 //-----------------------------------------------------------------------------
558 //
559 // A Timer3 interrupt indicates an SMBus SCL low timeout.
560 // The SMBus is disabled and re-enabled if a timeout occurs.
561 //
562 void Timer3_ISR (void) interrupt 14
563 {
564 1 SMB0CF &= ~0x80; // Disable SMBus
565 1 SMB0CF |= 0x80; // Re-enable SMBus
566 1 TMR3CN &= ~0x80; // Clear Timer3 interrupt-pending flag
567 1 SMB_BUSY = 0; // Free bus
568 1 }
569
570 //-----------------------------------------------------------------------------
571 // Support Functions
572 //-----------------------------------------------------------------------------
573
574 //-----------------------------------------------------------------------------
575 // EEPROM_ByteWrite ()
576 //-----------------------------------------------------------------------------
577 //
578 // Return Value : None
579 // Parameters :
580 // 1) unsigned char addr - address to write in the EEPROM
581 // range is full range of character: 0 to 255
582 //
583 // 2) unsigned char dat - data to write to the address <addr> in the EEPROM
584 // range is full range of character: 0 to 255
585 //
586 // This function writes the value in <dat> to location <addr> in the EEPROM
587 // then polls the EEPROM until the write is complete.
588 //
589 void EEPROM_ByteWrite(unsigned char addr, unsigned char dat)
590 {
591 1 while (SMB_BUSY); // Wait for SMBus to be free.
592 1 SMB_BUSY = 1; // Claim SMBus (set to busy)
593 1
594 1 // Set SMBus ISR parameters
595 1 TARGET = EEPROM_ADDR; // Set target slave address
596 1 SMB_RW = WRITE; // Mark next transfer as a write
597 1 SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
598 1 SMB_RANDOMREAD = 0; // Do not send a START signal after
599 1 // the word address
600 1 SMB_ACKPOLL = 1; // Enable Acknowledge Polling (The ISR
601 1 // will automatically restart the
602 1 // transfer if the slave does not
603 1 // acknoledge its address.
604 1
605 1 // Specify the Outgoing Data
606 1 WORD_ADDR = addr; // Set the target address in the
607 1 // EEPROM's internal memory space
608 1
609 1 SMB_SINGLEBYTE_OUT = dat; // Store <dat> (local variable) in a
610 1 // global variable so the ISR can read
611 1 // it after this function exits
612 1
613 1 // The outgoing data pointer points to the <dat> variable
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 11
614 1 pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT;
615 1
616 1 SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
617 1 // will contain one data byte
618 1
619 1 // Initiate SMBus Transfer
620 1 STA = 1;
621 1
622 1 }
623
624 //-----------------------------------------------------------------------------
625 // EEPROM_WriteArray ()
626 //-----------------------------------------------------------------------------
627 //
628 // Return Value : None
629 // Parameters :
630 // 1) unsigned char dest_addr - beginning address to write to in the EEPROM
631 // range is full range of character: 0 to 255
632 //
633 // 2) unsigned char* src_addr - pointer to the array of data to be written
634 // range is full range of character: 0 to 255
635 //
636 // 3) unsigned char len - length of the array to be written to the EEPROM
637 // range is full range of character: 0 to 255
638 //
639 // Writes <len> data bytes to the EEPROM slave specified by the <EEPROM_ADDR>
640 // constant.
641 //
642 void EEPROM_WriteArray(unsigned char dest_addr, unsigned char* src_addr,
643 unsigned char len)
644 {
645 1 unsigned char i;
646 1 unsigned char* pData = (unsigned char*) src_addr;
647 1
648 1 for( i = 0; i < len; i++ ){
649 2 EEPROM_ByteWrite(dest_addr++, *pData++);
650 2 }
651 1
652 1 }
653
654 //-----------------------------------------------------------------------------
655 // EEPROM_ByteRead ()
656 //-----------------------------------------------------------------------------
657 //
658 // Return Value :
659 // 1) unsigned char data - data read from address <addr> in the EEPROM
660 // range is full range of character: 0 to 255
661 //
662 // Parameters :
663 // 1) unsigned char addr - address to read data from the EEPROM
664 // range is full range of character: 0 to 255
665 //
666 // This function returns a single byte from location <addr> in the EEPROM then
667 // polls the <SMB_BUSY> flag until the read is complete.
668 //
669 unsigned char EEPROM_ByteRead(unsigned char addr)
670 {
671 1 unsigned char retval; // Holds the return value
672 1
673 1 while (SMB_BUSY); // Wait for SMBus to be free.
674 1 SMB_BUSY = 1; // Claim SMBus (set to busy)
675 1
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 12
676 1 // Set SMBus ISR parameters
677 1 TARGET = EEPROM_ADDR; // Set target slave address
678 1 SMB_RW = WRITE; // A random read starts as a write
679 1 // then changes to a read after
680 1 // the repeated start is sent. The
681 1 // ISR handles this switchover if
682 1 // the <SMB_RANDOMREAD> bit is set.
683 1 SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
684 1 SMB_RANDOMREAD = 1; // Send a START after the word address
685 1 SMB_ACKPOLL = 1; // Enable Acknowledge Polling
686 1
687 1
688 1 // Specify the Incoming Data
689 1 WORD_ADDR = addr; // Set the target address in the
690 1 // EEPROM's internal memory space
691 1
692 1 pSMB_DATA_IN = &retval; // The incoming data pointer points to
693 1 // the <retval> variable.
694 1
695 1 SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
696 1 // will contain one data byte
697 1
698 1 // Initiate SMBus Transfer
699 1 STA = 1;
700 1 while(SMB_BUSY); // Wait until data is read
701 1
702 1 return retval;
703 1
704 1 }
705
706 //-----------------------------------------------------------------------------
707 // EEPROM_ReadArray ()
708 //-----------------------------------------------------------------------------
709 //
710 // Return Value : None
711 // Parameters :
712 // 1) unsigned char* dest_addr - pointer to the array that will be filled
713 // with the data from the EEPROM
714 // range is full range of character: 0 to 255
715 //
716 // 2) unsigned char src_addr - beginning address to read data from the EEPROM
717 // range is full range of character: 0 to 255
718 //
719 // 3) unsigned char len - length of the array to be read from the EEPROM
720 // range is full range of character: 0 to 255
721 //
722 // Reads up to 256 data bytes from the EEPROM slave specified by the
723 // <EEPROM_ADDR> constant.
724 //
725 void EEPROM_ReadArray (unsigned char* dest_addr, unsigned char src_addr,
726 unsigned char len)
727 {
728 1 while (SMB_BUSY); // Wait for SMBus to be free.
729 1 SMB_BUSY = 1; // Claim SMBus (set to busy)
730 1
731 1 // Set SMBus ISR parameters
732 1 TARGET = EEPROM_ADDR; // Set target slave address
733 1 SMB_RW = WRITE; // A random read starts as a write
734 1 // then changes to a read after
735 1 // the repeated start is sent. The
736 1 // ISR handles this switchover if
737 1 // the <SMB_RANDOMREAD> bit is set.
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 13
738 1 SMB_SENDWORDADDR = 1; // Send Word Address after Slave Address
739 1 SMB_RANDOMREAD = 1; // Send a START after the word address
740 1 SMB_ACKPOLL = 1; // Enable Acknowledge Polling
741 1
742 1 // Specify the Incoming Data
743 1 WORD_ADDR = src_addr; // Set the target address in the
744 1 // EEPROM's internal memory space
745 1
746 1 // Set the the incoming data pointer
747 1 pSMB_DATA_IN = (unsigned char*) dest_addr;
748 1
749 1
750 1 SMB_DATA_LEN = len; // Specify to ISR that the next transfer
751 1 // will contain <len> data bytes
752 1
753 1
754 1 // Initiate SMBus Transfer
755 1 STA = 1;
756 1 while(SMB_BUSY); // Wait until data is read
757 1
758 1 }
759
760 //-----------------------------------------------------------------------------
761 // End Of File
762 //-----------------------------------------------------------------------------
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 713 ----
CONSTANT SIZE = 16 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 11 31
IDATA SIZE = ---- ----
BIT SIZE = 6 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -