赞助论坛
  • 4716阅读
  • 0回复

学习单片机之---WriteEEPROM [复制链接]

楼层直达
jswr  
发帖
339
精华
1
金币
264
威望
8
贡献
7
好评
15
注册
2008-06-30
楼主    jswr 发表于: 2008-08-20 17:19:55 
;;*****************************************************************
WriteEEPROM:
          push   acc
          push   b
reWrite_EEPROM:
          call   start       ;; Start Bit
          mov   a,#07h
          anl   a,r7
          rl     a
          orl   a,#EEP24CXXWrite

          call   shout
          jc     Write_EEPROMErr
          mov   a, r5
          call   shout
          jc     Write_EEPROMErr
          mov   a, r3
          call   shout
          jc     Write_EEPROMErr
          call   stop
          clr   c
          pop   b
          pop   acc
          ret

Write_EEPROMErr:
          call   stop
          jmp   reWrite_EEPROM

;;*****************************************************************************
; I2C   Read Address=r2;Return DATA=A
ReadEEPROM:
            push   acc
          push   b
reRead_EEPROM:
          call   start       ;; Start Bit
          mov   a,#07h
          anl   a,r7
          rl     a
          orl   a,#EEP24CXXRead
          mov   r3,a
          clr   acc.0
          call   shout
          jc     read_EEPROMErr

          mov   a, r5       ;; Register Address
          call   shout
          jc     read_EEPROMErr
          call   read_current   ;; Read in Data
          mov   r7,a     ;; Return Value
          pop   b
          pop   acc
          ret

read_EEPROMErr:
          call   stop
          jmp   reRead_EEPROM

;;--------------------------------------
read_current:
          setb   SDAPin
          setb   SCLPin
          nop
          nop
          nop
          call   start
          mov   a, r3
          call   shout
          jc     x44
read_current1:
          call   shin
          call   ACK
          clr   c
x44:         call   stop
          ret

;;--------------------------------------
start:       setb   SDAPin
          setb   SCLPin       ; enforce SCL high
          nop               ;
          nop               ;
          clr   SDAPin
          nop               ;
          nop               ;
          clr   SCLPin
          nop               ;
          nop               ;
          ret
;;--------------------------------------
stop:       clr   SDAPin
          nop               ;
          nop               ;
          setb   SCLPin
          nop               ;
          nop               ;
          nop               ;
          setb   SDAPin
          nop               ;
          nop               ;
          nop               ;
          nop               ;
          ret
;;--------------------------------------
shout:       push   b
          mov   b, #8
shoutBit_loop: rlc   a
          mov   SDAPin, c
          nop               ;
          setb   SCLPin
          nop               ;
          nop               ;
          nop               ;
          clr   SCLPin
          nop               ;
          nop               ;
          djnz   b, shoutBit_loop

          setb   SDAPin
          nop               ;
          setb   SCLPin
          nop               ;
          nop               ;
          jnb   SDAPin,shoutACK
          setb   c
          jmp   shoutNACK
shoutACK:
          clr   c
shoutNACK:
          clr   SCLPin
          nop               ;
          nop               ;
          nop               ;
          clr   SDAPin
          nop               ;
          nop               ;
          nop               ;
          pop   b
          ret
;;--------------------------------------
shin:       setb   SDAPin
          push   b
          mov   b, #8
shinloop:
          setb   SCLPin
          nop               ;
          nop               ;
          mov   c, SDAPin
          rlc   a
          clr   SCLPin
          nop               ;
          nop               ;
          djnz   b, shinloop
          pop   b
          ret

;;--------------------------------------
ACK:         setb   SDAPin
          nop               ;
          nop               ;
          setb   SCLPin
          nop               ;
          nop               ;
          clr   SCLPin
          ret