/** this is to use at24c32/64 with 8051 type controllers with Keil C51 compiler . before compile please right click on source Group1 on the toolbar and check Generate Assembler SRC file and Assemble SRC file. to get data use function unsigned char getDataVal(unsigned int addr2); to get data use function void setDataVal(int addr1,char zdata); thanks Indeep Singh Sidhu Dimension **/ #include "reg52.h" sbit SCL = P1^5; sbit SDA = P1^6; unsigned char data addr_hi; unsigned char data addr_lo; #define FADDR 0x0a0 #define PADDR 0 unsigned char getDataVal(unsigned int addr2); void setDataVal(int addr1,char zdata); unsigned char getDataVal(unsigned int addr2) { addr2=addr2; #pragma asm getVAL: mov dph,R6 mov dpl,R7 mov addr_lo, dpl ; set up address mov addr_hi, dph ; mov b,#120 ; retry counter x811: mov a,#PADDR ; programmable address lcall read_random ; try to read jnc x822 djnz b,x811 ; try again x822: mov R7,a #pragma endasm return addr2; } void setDataVal(int addr1,char zdata) { addr1=addr1; zdata=zdata; #pragma asm setVal: mov dph,R6 mov dpl,R7 mov addr_lo, dpl ; set up address mov addr_hi, dph ; mov b,#120 ; retry counter x81: mov a,#PADDR ; programmable address lcall write_byte jnc x86 ; jump if read OK djnz b,x81 ; try again x86: ljmp exit111 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. lcall start jc x49 ; abort if bus not available rl a ; programmable address to bits 3:1 orl a,#FADDR ; add fixed address clr acc.0 ; specify write operation lcall shout ; send device address jc x48 ; abort if no acknowledge mov a,addr_hi ; send high byte of address lcall shout ; jc x48 ; abort if no acknowledge mov a,addr_lo ; send low byte of address lcall shout ; jc x48 ; abort if no acknowledge mov a,R5 ; get data lcall shout ; send data jc x48 ; abort if no acknowledge clr c ; clear error flag x48: lcall stop x49: 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. lcall start jc x45 ; abort if bus not available rl a ; programmable address to bits 3:1 orl a,#FADDR ; add fixed address setb acc.0 ; specify read operation lcall shout ; send device address jc x44 ; abort if no acknowledge lcall shin ; receive data byte lcall NAK ; do not acknowledge byte clr c ; clear error flag x44: lcall stop x45: ret 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 b mov b,a ; save copy of programmable address ; Send dummy write command to set internal address. lcall start jc x47 ; abort if bus not available rl a ; programmable address to bits 3:1 orl a,#FADDR ; add fixed address clr acc.0 ; specify write operation lcall shout ; send device address jc x46 ; abort if no acknowledge mov a,addr_hi ; send high byte of address lcall shout ; jc x46 ; abort if no acknowledge mov a,addr_lo ; send low byte of address lcall shout ; jc x46 ; abort if no acknowledge ; Call Current Address Read function. mov a,b ; get programmable address lcall read_current ljmp x47 ; exit x46: lcall stop x47: pop b 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 SDA setb SCL ; Verify bus available. jnb SDA,x40 ; jump if not high jnb SCL,x40 ; jump if not high nop ; enforce setup delay and cycle delay clr SDA nop ; enforce hold delay nop ; nop ; nop ; nop ; clr SCL clr c ; clear error flag sjmp x41 x40: setb c ; set error flag x41: ret stop: ; Send STOP,defined as low-to-high SDA with SCL high. ; SCL expected low on entry. Return with SCL,SDA high. clr SDA nop ; enforce SCL low and data setup nop setb SCL nop ; enforce setup delay nop ; nop ; nop ; nop ; setb SDA 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 b mov b,#8 ; bit counter x42: rlc a ; move bit into CY mov SDA,c ; output bit nop ; enforce SCL low and data setup setb SCL ; raise clock nop ; enforce SCL high nop ; nop ; nop ; clr SCL ; drop clock djnz b,x42 ; next bit setb SDA ; release SDA for ACK nop ; enforce SCL low and tAA nop ; setb SCL ; raise ACK clock nop ; enforce SCL high nop ; nop ; nop ; mov c,SDA ; get ACK bit clr SCL ; drop ACK clock pop b 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. setb SDA ; make SDA an input push b mov b,#8 ; bit count x43: nop ; enforce SCL low and data setup nop ; nop ; setb SCL ; raise clock nop ; enforce SCL high nop ; mov c,SDA ; input bit rlc a ; move bit into byte clr SCL ; drop clock djnz b,x43 ; next bit pop b ret ACK: ; Clock out an acknowledge bit (low). ; SCL expected low on entry. Return with SCL,SDA low. clr SDA ; ACK bit nop ; enforce SCL low and data setup nop ; setb SCL ; raise clock nop ; enforce SCL high nop ; nop ; nop ; clr SCL ; drop clock ret NAK: ; Clock out a negative acknowledge bit (high). ; SCL expected low on entry. Return with SCL low,SDA high. setb SDA ; NAK bit nop ; enforce SCL low and data setup nop ; setb SCL ; raise clock nop ; enforce SCL high nop ; nop ; nop ; clr SCL ; drop clock ret exit111: #pragma endasm return; }