51操作AT24C256,使用页写页读出现操作错误,请大家帮忙【恢复】

2020-02-05 09:16发布

先将故障现象说清楚:

51单片机使用P1.2,P1.3操作AT24C256

硬件连接没问题,软件是汇编编写的,每次操作64个字节,AT24C256型号是ATMLU828(2EB),括号内是第二行

写的时侯昨晚是连续写两页,没有问题,用编程器读出是对的,读出两页的时侯,第一页是对的,第二页错,今天再调试时更离谱

写也是写第一页的对,第二页不对.程序如下:

;TEST111:

  ;MOV   R0,#80H

 ; MOV   C512AD0,#00H

 ; MOV                C512AD1,#00H

         ;MOV                IICRCO,#64

         ;LCALL        I2C_READ                                        ; 调用形式





;变量定义

IICRCO                DATA        56H        ;IIC总线发送和接收数据字节时所用的位计数器

C512AD0                DATA        58H        ;24C512读写地址0

C512AD1                DATA        59H        ;24C512读写地址1

C512DATA        DATA        5AH        ;24C512读写数据









;/*------------------------------------------------------

;;读一个字节        Address地址        Data读出的数据

;;------------------------------------------------------*/



I2C_READ:

        CLR                EEWP

I2C_READ_A:

        LCALL        I2C_START

        MOV        A,#10100000B

        LCALL        I2C_SEND8BIT

        LCALL        I2C_GETACK

        JC        I2C_READEND        ;=1,表示无确认,再次发送



        MOV                A,C512AD0                        ;放入高地址

        LCALL         I2C_SEND8BIT

        LCALL                I2C_GETACK

        JC                I2C_READEND

        MOV                A,C512AD1

        LCALL                I2C_SEND8BIT

        LCALL                I2C_GETACK

        JC                        I2C_READEND



        LCALL        I2C_START

        MOV        A,#10100001B

        LCALL        I2C_SEND8BIT

        LCALL        I2C_GETACK

        JC        I2C_READEND        ;=1,表示无确认,再次发送



I2C_READ_LOOP:

        LCALL        I2C_RECEIVE8BIT

        MOV                @R0,A                        ;I2CData,A

        MOV                C512DATA,A

        INC                R0

        CLR         C

        LCALL        I2C_PUTACK

        DJNZ        IICRCO,I2C_READ_LOOP

        SETB                C

        LCALL        I2C_PUTACK

        LCALL        I2C_STOP

        CLR                C

        SETB        EEWP

        RET

I2C_READEND:

        LCALL        I2C_STOP

        SETB        C

        SETB        EEWP

        RET



NOP_1US:

        NOP

        NOP

        NOP

        RET



;;发送开始信号

I2C_START:

        SETB        SCL

        SETB        SDA

        NOP

        NOP

  NOP

  NOP

  NOP

        CLR        SDA

        NOP

        NOP

  NOP

  NOP

  NOP

        CLR        SCL

RET

;;发送结束信号

I2C_STOP:

  CLR        SDA

        NOP

        NOP

  NOP

  NOP

  NOP

  NOP

  SETB        SCL

        NOP

        NOP

  NOP

  NOP

  NOP

  NOP

        SETB        SDA

        RET

;;发送接收确认信号

I2C_PUTACK:

        MOV                SDA,C

        NOP

        NOP

        NOP

        SETB        SCL

        NOP

        NOP

        NOP

        NOP

        CLR        SCL

        RET

I2C_GETACK:

        SETB        SDA

        MOV                R7,#50H

        CLR                C

        SETB        SCL

I2C_GETACK_1:

        JNB                SDA,I2C_GETACK_2

        DJNZ        R7,I2C_GETACK_1

        SETB        C

I2C_GETACK_2:

        CLR                SCL

        RET

;送八位数据

I2C_SEND8BIT:

        MOV        B,#08H

        CLR                SCL

I2C_SEND8BIT_A:

        RLC                A

        MOV                SDA,C

        NOP

        NOP

        NOP

  NOP

        SETB        SCL

        NOP

        NOP

        NOP

        NOP

        NOP

        NOP

  NOP

  NOP

  NOP

        CLR                SCL

        DJNZ        B,I2C_SEND8BIT_A

RET





;接收八位数据

I2C_RECEIVE8BIT:

        MOV        B,#08H

        CLR        A

        SETB        SDA

I2C_RECEIVE8IT_A:

        SETB        SCL

        NOP

        NOP

        NOP

        NOP

  NOP

        MOV        C,SDA

        RLC        A

  NOP

  NOP

  NOP

  NOP

  NOP

  NOP

  NOP

  NOP

        CLR        SCL

        NOP

        NOP

        NOP

  NOP

  NOP

  NOP

  NOP

  NOP

        DJNZ        B,I2C_RECEIVE8IT_A



        RET
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。