Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12K views10 pages

AT24CXX

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 10

NAME ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;

Control_AT24Cxx

This collection of routines allows an AT89C2051 microcontroller to read and write the AT24Cxx family of serial CMOS EEPROMS. This version of the code is compatible only with the AT89C2051 due to the location of the data buffer and stack in RAM. The code may be modified to work with the AT89C1051 by relocating or resizing the buffer and stack to fit into the smaller amount of RAM available in the AT89C1051. Note that the minimum size of the buffer is determined by the page size of the AT24Cxx. All five AT24Cxx device operations are supported. Byte Write, Current Address Read and Random Read are implemented by the functions WRITE_BYTE, READ_CURRENT and READ_RANDOM, respectively. Page Write and Sequential Read are special cases of functions WRITE_BLOCK and READ_BLOCK, respectively. WRITE_BLOCK and READ_BLOCK process from one byte to the number of bytes in a page and transfer data between the AT24Cxx and the RAM buffer. The code supports multiple AT24Cxx devices per bus, each with a unique address determined by the wiring of the address pins (A0, A1, A2). The three-bit programmable address is passed to the WRITE_BYTE, READ_CURRENT, READ_RANDOM, WRITE_BLOCK and READ_BLOCK functions, where it is combined with the AT24Cxx fixed address (FADDR) and used to address a device on the bus. Refer to the AT24Cxx family data sheets for additional information on device addressing. Functions BYTE_FILL, VERIFY_BYTE_FILL, PAGE_FILL and VERIFY_PAGE_FILL are artifacts from the debug process and serve to illustrate the use of the device read and write functions with an AT24C64. To modify the code for a different member of the AT24Cxx family, simply redefine the values of SIZE (the number of bytes per device) and PSIZE (the number of bytes per page). To change the fill value, redefine FILL. To change the programmable portion of the device address, redefine PADDR to a value from zero to seven. The code meets all AT24Cxx family timing requirements when executed by an AT89Cx051 microcontroller with a 12 MHz clock. Code modifications may be required if a faster clock is substituted. EQU EQU EQU EQU EQU 0a0h 0 2000h 32 55h ; ; ; ; ; fixed address for AT24Cxx EEPROMs programmable address (0..7) bytes per AT24C64 bytes per page for AT24C64 example fill value

FADDR PADDR SIZE PSIZE FILL

; Register definitions. index kount zdata addr_lo addr_hi EQU EQU EQU EQU EQU r0 r1 r1 r2 r3 ; ; ; ; ; buffer pointer byte count register data register 2-byte address register

; Microcontroller connections to AT24Cxx serial bus lines. SCL SDA BIT BIT p1.2 p1.3 ; serial clock ; serial data

DSEG AT 20H buffer: ORG DS ORG 40H PSIZE 60H ; storage for read/write data ; stack origin

stack:

DS CSEG ORG jmp ORG reti ORG reti ORG reti ORG reti ORG reti

20H

; stack depth

0000H on_reset 0003H 000BH 0013H 001BH 0023H

; power on/reset vector ; external interrupt 0 vector ; undefined ; timer 0 overflow vector ; undefined ; external interrupt 1 vector ; undefined ; timer 1 overflow vector ; undefined ; serial I/O interrupt vector ; undefined ; begin code space ; register bank zero ; initialize stack pointer

ORG 0080H USING 0 on_reset: mov

sp, #(stack-1)

; Initialize AT24Cxx serial bus lines. setb setb call jc call jc call jc call jc fault: jmp byte_fill: ; ; ; ; Fill every byte in an AT24Cxx with the same value. Writes one address at a time (page mode is not used). Returns CY set to indicate write timeout. Destroys A, B, DPTR, XDATA, ADDR_HI:ADDR_LO. mov mov x51: mov mov mov mov zdata, #FILL dptr, #0 addr_lo, dpl addr_hi, dph b, #120 a, #PADDR ; set up fill data ; initialize address pointer ; set up address ; ; retry counter ; programmable address $ SDA SCL byte_fill fault verify_byte_fill fault page_fill fault verify_page_fill fault ; high ; high

x52:

call jnc djnz setb jmp inc mov cjne mov cjne clr x54: ret

write_byte x53 b, x52 c x54

; try to write ; jump if write OK ; try again ; set timeout error flag ; exit

x53: ; ;

dptr ; advance address pointer a, dpl ; check low byte a, #(LOW SIZE), x51 ; jump if not last a, dph ; check high byte a, #(HIGH SIZE), x51 ; jump if not last c ; clear error flag

verify_byte_fill: ; ; ; ; ; ; ; ; Verify that all bytes in an AT24Cxx match a fill value. Reads and verifies one byte at a time (page mode is not used). Performs a Random Read function to initialize the internal address counter and checks the contents of the first address. Then performs multiple Current Address Read functions to step through the remaining addressess. Returns CY set to indicate read timeout or compare fail. Destroys A, B, DPTR. mov mov mov mov x81: mov call jnc djnz jmp cjne jmp x83: mov call jc cjne x85: inc mov cjne clr jmp setb ret dptr, #0 addr_lo, dpl addr_hi, dph b, #120 a, #PADDR read_random x82 b, x81 x86 a, #FILL, x86 x85 a, #PADDR read_current x87 a, #FILL, x86 ; initialize address pointer/counter ; set up address ; ; retry counter ; programmable address ; try to read ; jump if read OK ; try again ; set error flag and exit ; jump if compare error ; do remaining addresses

x82:

; jump if read fails ; jump if compare error

dptr ; advance address pointer a, dph ; check high byte a, #(HIGH SIZE), x83 ; jump if not last c ; clear error flag x87 ; exit c ; set error flag

x86: x87:

page_fill: ; Fill every byte in an AT24Cxx with the same value. ; Writes one page at a time. ; Returns CY set to indicate write timeout.

; Destroys A, B, DPTR, KOUNT, INDEX, ADDR_HI:ADDR_LO. ; First fill buffer. mov mov mov inc djnz b, #PSIZE index, #buffer @index, #FILL index b, x61 ; bytes per page ; point to buffer ; put fill value in buffer ; advance pointer ; next byte

x61:

; Copy buffer to device, one page at a time. x62: mov mov mov mov x63: mov mov call jnc djnz setb jmp dptr, #0 addr_lo, dpl addr_hi, dph kount, #PSIZE b, #120 a, #PADDR write_block x64 b, x63 c x66 ; initialize address pointer ; set up address ; ; bytes per page ; retry counter ; programmable address ; try to write ; jump if write OK ; try again ; set timeout error flag ; exit

x64:

; Add page size to address pointer. mov add mov jnc inc cjne mov cjne clr x66: ret a, dpl a, #PSIZE dpl, a x65 dph ; get low byte ; add page size ; save low byte ; jump if high byte not affected ; increment high byte ; ; ; ; jump if low byte not last check high byte jump if not last clear error flag

x65: ;

a, #(LOW SIZE), x62 a, dph a, #(HIGH SIZE), x62 c

verify_page_fill: ; ; ; ; Verify that all bytes in an AT24Cxx match a fill value. Reads and verifies one page at a time. Returns CY set to indicate read timeout or compare fail. Destroys A, B, DPTR, KOUNT, INDEX, ADDR_HI:ADDR_LO. ; Copy device page to buffer. mov x71: mov mov mov mov x72: mov call jnc dptr, #0 addr_lo, dpl addr_hi, dph kount, #PSIZE b, #120 a, #PADDR read_block x74 ; initialize address pointer ; set up address ; ; bytes per page ; retry counter ; programmable address ; try to read ; jump if read OK

djnz x73: x74: setb jmp

b, x72 c x77

; try again ; set error flag ; exit

; Verify buffer contents. mov mov x75: cjne inc djnz b, #PSIZE index, #buffer ; bytes per page ; point to buffer

@index, #FILL, x73 ; jump if compare fails index ; advance pointer b, x75 ; next byte

; Add page size to address pointer. mov add mov jnc inc ; x76: cjne mov cjne clr ret write_block: ; ; ; ; ; ; ; ; Write from one byte to one page of data to an AT24Cxx. Called with programmable address in A, address of first byte in register pair ADDR_HI:ADDR_LO, data in BUFFER, byte count in register KOUNT. Does not wait for write cycle to complete. Returns CY set to indicate that the bus is not available or that the addressed device failed to acknowledge. Destroys A, KOUNT, INDEX. call jc rl orl clr call jc mov call jc mov call jc mov x36: mov call jc inc djnz start x38 a a, #FADDR acc.0 shout x37 a, addr_hi shout x37 a, addr_lo shout x37 ; abort if bus not available ; ; ; ; ; programmable address to bits 3:1 add fixed address specify write operation send device address abort if no acknowledge a, dpl a, #PSIZE dpl, a x76 dph ; ; ; ; ; get low byte add page size save low byte jump if high byte not affected increment high byte ; ; ; ; jump if low byte not last check high byte jump if not last clear error flag

x77:

a, #(LOW SIZE), x71 a, dph a, #(HIGH SIZE), x71 c

; send high byte of address ; ; abort if no acknowledge ; send low byte of address ; ; abort if no acknowledge ; point to buffer

index, #buffer a, @index shout x37 index kount, x36

; get data ; send data ; abort if no acknowledge ; advance buffer pointer ; next byte

clr x37: x38: call ret

c stop

; clear error flag

read_block: ; ; ; ; ; ; ; ; Read from one byte to one page of data from an AT24Cxx. Performs a Random Read which is extended into a Sequential Read when more than one byte is read. Called with programmable address in A, address of first byte in register pair ADDR_HI:ADDR_LO, byte count in register KOUNT. Returns data in BUFFER. Returns CY set to indicate that the bus is not available or that the addressed device failed to acknowledge. Destroys A, KOUNT, INDEX. ; Send dummy write command to address first byte. call jc rl orl mov clr call jc mov call jc mov call jc start x35 a a, #FADDR index, a acc.0 shout x34 a, addr_hi shout x34 a, addr_lo shout x34 ; abort if bus not available ; ; ; ; ; ; programmable address to bits 3:1 add fixed address save copy of device address specify write operation send device address abort if no acknowledge

; send high byte of address ; ; abort if no acknowledge ; send low byte of address ; ; abort if no acknowledge

; Send read command and receive data. call jc mov setb call jc mov x31: call mov cjne call jmp call inc djnz clr call ret start x34 a, index acc.0 shout x34 ; second start for read ; abort if bus not available ; ; ; ; get device address specify read operation send device address abort if no acknowledge ; point to buffer

index, #buffer shin @index, a

; receive data byte ; save data

x32:

kount, #1, x32 ; jump if not last byte NAK ; do not acknowledge last byte x33 ; done ACK index kount, x31 c stop ; acknowledge byte ; advance buffer pointer ; next byte ; clear error flag

x33: x34: x35:

write_byte: ; ; ; ; ; ; ; AT24Cxx Byte Write function. Called with programmable address in A, byte address in register pair ADDR_HI:ADDR_LO, data in register XDATA. Does not wait for write cycle to complete. Returns CY set to indicate that the bus is not available or that the addressed device failed to acknowledge. Destroys A. call jc rl orl clr call jc mov call jc mov call jc mov call jc x48: x49: clr call ret read_current: ; ; ; ; AT24Cxx Current Address Read function. Called with programmable address in A. Returns data in A. Returns CY set to indicate that the bus is not available or that the addressed device failed to acknowledge. call jc rl orl setb call jc call call clr x44: x45: call ret start x45 a a, #FADDR acc.0 shout x44 shin NAK c stop ; abort if bus not available ; ; ; ; ; programmable address to bits 3:1 add fixed address specify read operation send device address abort if no acknowledge start x49 a a, #FADDR acc.0 shout x48 a, addr_hi shout x48 a, addr_lo shout x48 a, zdata shout x48 c stop ; abort if bus not available ; ; ; ; ; programmable address to bits 3:1 add fixed address specify write operation send device address abort if no acknowledge

; send high byte of address ; ; abort if no acknowledge ; send low byte of address ; ; abort if no acknowledge ; get data ; send data ; abort if no acknowledge ; clear error flag

; receive data byte ; do not acknowledge byte ; clear error flag

read_random:

; ; ; ; ;

AT24Cxx Random Read function. Called with programmable address in A, byte address in register pair ADDR_HI:ADDR_LO. Returns data in A. Returns CY set to indicate that the bus is not available or that the addressed device failed to acknowledge. push mov b b, a ; save copy of programmable address

; Send dummy write command to set internal address. call jc rl orl clr call jc mov call jc mov call jc start x47 a a, #FADDR acc.0 shout x46 a, addr_hi shout x46 a, addr_lo shout x46 ; abort if bus not available ; ; ; ; ; programmable address to bits 3:1 add fixed address specify write operation send device address abort if no acknowledge

; send high byte of address ; ; abort if no acknowledge ; send low byte of address ; ; abort if no acknowledge

; Call Current Address Read function. mov call jmp call pop ret start: ; Send START, defined as high-to-low SDA with SCL high. ; Return with SCL, SDA low. ; Returns CY set if bus is not available. setb setb SDA SCL a, b ; get programmable address read_current x47 ; exit stop b

x46: x47:

; Verify bus available. jnb jnb nop clr nop nop nop nop nop clr clr jmp SDA, x40 SCL, x40 SDA ; jump if not high ; jump if not high ; enforce setup delay and cycle delay ; enforce hold delay ; ; ; ; ; clear error flag

SCL c x41

x40: x41: setb ret stop: ; Send STOP, defined as low-to-high SDA with SCL high. ; SCL expected low on entry. Return with SCL, SDA high. clr nop nop setb nop nop nop nop nop setb ret shout: ; ; ; ; ; Shift out a byte to the AT24Cxx, most significant bit first. SCL, SDA expected low on entry. Return with SCL low. Called with data to send in A. Returns CY set to indicate failure by slave to acknowledge. Destroys A. push mov rlc mov nop setb nop nop nop nop clr djnz setb nop nop setb nop nop nop nop mov clr pop ret shin: ; Shift in a byte from the AT24Cxx, most significant bit first. ; SCL expected low on entry. Return with SCL low. ; Returns received data byte in A. b b, #8 a SDA, c SCL ; bit counter ; move bit into CY ; output bit ; enforce SCL low and data setup ; raise clock ; enforce SCL high ; ; ; ; drop clock ; next bit ; ; ; ; ; ; ; ; release SDA for ACK enforce SCL low and tAA raise ACK clock enforce SCL high SDA SCL ; enforce setup delay ; ; ; ; ; enforce SCL low and data setup c ; set error flag

SDA

x42:

SCL b, x42 SDA SCL

c, SDA SCL b

; get ACK bit ; drop ACK clock

setb push mov x43: nop nop nop setb nop nop mov rlc clr djnz pop ret ACK:

SDA b b, #8

; make SDA an input ; bit count ; enforce SCL low and data setup ; ; ; raise clock ; enforce SCL high ; ; input bit ; move bit into byte ; drop clock ; next bit

SCL c, SDA a SCL b, x43 b

; Clock out an acknowledge bit (low). ; SCL expected low on entry. Return with SCL, SDA low. clr nop nop setb nop nop nop nop clr ret NAK: ; Clock out a negative acknowledge bit (high). ; SCL expected low on entry. Return with SCL low, SDA high. setb nop nop setb nop nop nop nop clr ret END SDA SCL ; ; ; ; ; ; ; ; ; NAK bit enforce SCL low and data setup raise clock enforce SCL high SDA SCL ; ; ; ; ; ; ; ; ; ACK bit enforce SCL low and data setup raise clock enforce SCL high

SCL

drop clock

SCL

drop clock

You might also like