Página 1 de 1

Ajuda Memoria Externa

MensagemEnviado: 23 Abr 2014 11:01
por Bugabuga
Boa tarde pessoal.

Estou com duvidas para programar uma memoria externa no 8051, para fazer o uso de uma memoria não volátil e gravar dados importantes que podem ser modificados mas não podem ser perdidos caso não haja alimentação do circuito.
Resumindo... Estou fazendo um programa de acesso por senha, onde a mesma pode ser modificada mas não poder se perder caso desligar o equipamento.

Estou usado o AT89S51 24PC, e como memoria EEprom o CI 24C04...

Nunca fiz isso, e nos documentos fornecidos pelo professor só descreve os comandos básicos para "Trocar" os dados e teorias, mas nenhum exemplo real ou maiores detalhes para a execução de um programa e circuito funcional...


Vim aqui para pedir ajudar daqueles que tem experiencia e mais conhecimento sobre o assunto.
Como devo proceder ? peco que falem coisas básicas que devo saber e até links para documentos, exemplos, etc. Coisas que possam ajudar a entender melhor a execução deste problema.
O conhecimento que eu tenho é 70% do consta aqui, que é o material que o professor faz uso http://iris.sel.eesc.usp.br/sel614/

A momento sei que teria que usar o I2C para comunicar com apenas 2 fios com o 24C04.

OBS: Estou trabalhando em assembly por motivo didático de aprendizado, sei que usar C é mais rentável já que é uma linguagem de alto nível.

Re: Ajuda Memoria Externa

MensagemEnviado: 23 Abr 2014 12:14
por ze2
espero que não atropele seu aprendizado...
no início de carreira usei isso dentro do c com satisfação total. (hmmm acho que esta frase não ficou muito legal). Foi entre um #asm e #endasm.
vai um cafezinho também?
de nada...
asm code
;*****************************************************************************
; This code was designed to demonstrate how the x24c01a/02/04/08/16 family *
; of parts could be interfaced to 8051 microcontrollers. the interface *
; uses 2 lines from port 1 (p1.0 and p1.1) to communicate. Other i2c *
; compatible parts can be added to the bus as long as they do not have $a *
; as their device identifier. The routines rdbyt and wrbyt are tailored *
; specifically to this family. The routines start, stop, ack, nack, outbyt, *
; and inbyt can be considered generic i2c routines. *
; *
; The code shown demonstrates a 'random read' and 'byte write'. The other *
; modes of operation can be created by expanding upon these routines. *
; Acknowledge polling is used to determine when the write cycle is complete. *
; *
; This code will work with all xicor i2c compatible eeproms reguardless of *
; their size. As long as the address pins are configured correctly this *
; code will not know the difference between a bus with a single x24c16 and a *
; bus with eight x24c02s. *
; *
; The mainline of this program reads the data located at address 002dh and *
; then writes that data back to address 0041h. *
; Revised: january 1997 *
;*****************************************************************************

sda equ p1.0 ; the sda bit is port 1 bit 0
scl equ p1.1 ; the scl bit is port 1 bit 1
addr equ 40h ; location for x24c04 address to access
count equ 41h ; counter variable
data1 equ 42h ; location for x24c04 data transfered
temp equ 43h ; scratch pad byte
count2 equ 44h ; counter for ack polling

;***************************
; Reset vector entry point *
;***************************

defseg zero,start=0
seg zero

ljmp begin ; jump to beginning of program

;**********************
; Program entry point *
;**********************

org 50 ; program code starts at 0100h
begin mov sp,#60h ; initialize stack pointer
mov dptr,#002dh ; prepare to read address #002dh
mov addr,dph
mov addr+1,dpl
call rdbyt ; read data from address 002dh
mov dptr,#0041h ; prepare to write value stored in
mov addr,dph ; data1 to dut location #0041h
mov addr+1,dpl
call wrbyt ; write data to address #0041h
call ackpol ; use acknowledge polling
done ljmp done ; loop until reset

;***************************************************************************
; Read a byte "random read sequence". the address to read is stored *
; in addr. The data from the dut (device under test) is stored in data1. *
;***************************************************************************

rdbyt 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 temp,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,temp ; 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
ret

;**********************************************************************
; 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 temp,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,temp
mov data1,a
call outbyt ; send write data
call nack ; send sda hi to receive an acknowledge
call stop ; send stop
ret

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

inbyt setb sda ; read 8 bits, make sda an input
mov count,#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 count,loopi ; loop until 8 bits are read
ret

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

outbyt mov count,#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 count,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 count2,#080h ; max number of times to check the part
akloop djnz count2,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
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
clr sda ; sda low
nop ; make sure the set up time is valid
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
mov c,sda ; read state on sda, save in carry flag
clr scl ; scl low
ret

end

Re: Ajuda Memoria Externa

MensagemEnviado: 23 Abr 2014 15:58
por Bugabuga
Obrigado, vou estudar o código e ver o que consigo extrair para fazer o meu próprio. :)

Re: Ajuda Memoria Externa

MensagemEnviado: 24 Abr 2014 10:28
por Bugabuga
ze escreveu:
Código: Selecionar todos
;
sda     equ p1.0        ; the sda bit is port 1 bit 0
scl     equ p1.1        ; the scl bit is port 1 bit 1
addr    equ 40h         ; location for x24c04 address to access
count   equ 41h         ; counter variable
data1   equ 42h         ; location for x24c04 data transfered
temp    equ 43h         ; scratch pad byte
count2  equ 44h         ; counter for ack polling


Uma duvida... Quando foi endereçado o Slave do X24C04 foi usado o valor 40h (1000000b), nao deveria se o usar o valor minimo de A0 (10100000b) como consta no datasheet para as memorias EEprom`s x24C04, que esta aqui nesta imagem abaixo :

address.png


Com isso pergunto, o pq do 40h ? esta errado ou sou eu que estou comendo "bronha" ushushsu

De resto to indo bem, estou conseguindo entender, consegui alguns outros exemplos pela internet. Daki a pouco ja vou tentar colocar algo em pratica.

Re: Ajuda Memoria Externa

MensagemEnviado: 24 Abr 2014 10:46
por tcpipchip
Não esqueça que o programa faz o OR com o A0 para endereçar o dispositivo...então não te preocupes.....o 40 é um endereço dentro da memoria e não o ID da memória, ok ?