AT24C64

Software e Hardware para linha x51

Moderadores: 51, guest2003, Renie, gpenga

AT24C64

Mensagempor rebelk » 16 Dez 2009 11:27

alguem poderia me enviar ou postar o codigo.h para a memoria serial AT24C64 em C para o keil ?


kleberp.emarf@oi.com.br

www.emarf.com.br
rebelk
Byte
 
Mensagens: 301
Registrado em: 15 Nov 2006 20:16

Mensagempor ze » 16 Dez 2009 12:38

existe 8051 com interf i2c? na minha infância não tinha. Portanto serve um genérico e pessoal (bom, agora num é+) do século passado feito +- na raça?
coloque isso num include qualquer. Não sei como o keil trata do #asm. Cuidado se interrupts usam mesmos r5,6,7

Código: Selecionar todos
unsigned char data1; //tem que
unsigned int addr;    //ser globais

#define sda p2.0 //não sei se o keil trata assim. check.
#define scl p2.1
...
void e2promwrite(unsigned int a,unsigned char d)
{

addr=a;
data1=d;
#asm   

;**********************************************************************
; Write a byte "byte write sequence". The address to write is stored  *
; in addr. The data to write is stored in data1.                      *
;**********************************************************************

wrbyt:  mov      a,_data1    ; write to byte pointed to by addr the
        mov      r5,a     ; value in location 'data1'
        call     start      ; send start command
        mov      a,_addr     ; build slave address for write
        clr      c
        rlc      a
        orl      a,#0a0h
        mov      _data1,a
        call     outbyt     ; send slave address
        call     nack       ; send sda hi to receive an acknowledge
        mov      a,_addr+1
        mov      _data1,a
        call     outbyt     ; send word address
        call     nack       ; send sda hi to receive an acknowledge
        mov      a,r5
        mov      _data1,a
        call     outbyt     ; send write data
        call     nack       ; send sda hi to receive an acknowledge
        call     stop       ; send stop
        call     ackpol       ; use acknowledge polling

#endasm

}

unsigned char e2promread(unsigned int a)
{
addr=a;

#asm

   call     start      ; read a byte from the address indicated
        mov      a,_addr     ; in 'addr'
        clr      c
        rlc      a
        orl      a,#0a0h    ; build slave address for write
        mov      r5,a
        mov      _data1,a
        call     outbyt     ; send slave address
        call     nack       ; send sda hi to receive an acknowledge
        mov      a,_addr+1
        mov      _data1,a
        call     outbyt     ; send word address
        call     nack       ; send sda hi to receive an acknowledge
        call     start      ; send start command
        mov      a,r5     ; build slave address for read
        orl      a,#01h     ; r/w bit = 1
        mov      _data1,a
        call     outbyt     ; send slave address
        call     nack       ; send sda hi to receive an acknowledge
        call     inbyt      ; read data from x24c04
        call     nack       ; clock without acknowledge
        call     stop       ; send stop command

#endasm;

return(data1);

}
/*************************************************/
/* Rotinas usadas pelas funcoes e2prom asm acima */
/*************************************************/

#asm

;***************************************************************
; Read 8 bits from the dut. The results are returned in data1. *
;***************************************************************

inbyt:  setb     sda         ; read 8 bits, make sda an input
        mov      r6,#08h
loopi:  call     clock       ; clock data
        mov      a,_data1     ; build byte using data1
        rlc      a           ; roll in next bit from dut
        mov      _data1,a     ; save data
        djnz     r6,loopi ; loop until 8 bits are read
        ret

;*********************************************************
; Write 8 bits to the dut. The data to send is in data1. *
;*********************************************************

outbyt: mov      r6,#08h   ; prepare to shift out 8 bits
loopo:  mov      a,_data1      ; load data to be sent to dut
        rlc      a            ; rotate bit to send into carry flag
        mov      sda,c        ; send carry to sda
        mov      _data1,a      ; save rotated data
        call     clock        ; send clock signal to dut
        djnz     r6,loopo  ; loop until all 8 bits have been sent
        ret

;*********************************************************************
; Perform acknowledge polling to determine when the write cycle      *
; completes. Upon return from this routine the carry bit indicates   *
; whether the dut ever acknowledged the write. carry=0 part          *
; acknowledged, carry=1 no acknowledge received.                     *
;*********************************************************************

ackpol: mov      r7,#080h ; max number of times to check the part
akloop: djnz     r7,look  ; return if the part
        sjmp     outack       ; never issues an acknowledge.
look:   call     start        ; set up for a read
        mov      a,#0a0h      ; make slave address for a read
        mov      _data1,a
        call     outbyt       ; send slave address
        call     nack         ; get acknowledge
        jc       akloop       ; loop if no acknowledge received
outack: call     stop         ; issue a stop before returning
        ret

;***********************
; Issue a stop command *
;***********************

stop:   clr      sda          ; send stop condition to dut, sda low
        setb     scl          ; scl high
        nop                   ; make sure the set up time is valid
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop

        setb     sda          ; sda high
        ret

;************************
; Issue a start command *
;************************

start:  setb     sda          ; send start condition to dut, sda high
        setb     scl          ; scl high
        nop                   ; make sure scl set up time is valid
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        clr      sda          ; sda low
        nop                   ; make sure the set up time is valid
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        clr      scl          ; scl low
        ret

;************************
; Issue an acknowledge. *
;************************

ack:    clr      sda          ; perform an acknowledge, sda low
        call     clock        ; generate a clock pulse
        ret

;****************************************************
; Send sda high. The nack routine does not check    *
; to see if the dut actually issues an acknowledge. *
;****************************************************

nack:   setb     sda          ; sda high
        call     clock        ; generate a clock pulse
        ret

;*****************************************************************
; Issue a clock pulse. While the clock is high the value on the  *
; sda line is placed in the carry flag. when a read is taking    *
; place the carry flag will indicate the value from the dut.     *
;*****************************************************************

clock:  nop                  ; make sure the data set up time is valid
        setb    scl         ; generate a clock pulse, scl high
        nop                  ; make sure clock high time is valid
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        mov     c,sda       ; read state on sda, save in carry flag
        clr     scl         ; scl low
        ret

#endasm
de nada.
Código: Selecionar todos
Avatar do usuário
ze
Dword
 
Mensagens: 1655
Registrado em: 05 Jun 2007 14:32


Voltar para 8051

Quem está online

Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante

x