资源预览内容
第1页 / 共22页
第2页 / 共22页
第3页 / 共22页
第4页 / 共22页
第5页 / 共22页
第6页 / 共22页
第7页 / 共22页
第8页 / 共22页
第9页 / 共22页
第10页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
作者:尘封往事文章来源:本站原创点击数:惡2043更新时间:2006-1-12 13:07:34NAME 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 thedevice read and write functions with an AT24C64. To modify the code for adifferent 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.FADDREQU 0a0h; fixed address for AT24Cxx EEPROMsPADDREQU 0 ;programmable address (0.7)SIZEEQU 2000h; bytes per AT24C64PSIZEEQU 32bytes per page for AT24C64FILLEQU 55hexample fill value; Register definitions.index EQU r0 ; buffer pointerkount EQU r1 ; byte count registerzdata EQU r1 ; data registeraddr_lo EQU r2 ; 2-byte address registeraddr_hi EQU r3 ; Microcontroller connections to AT24Cxx serial bus lines.SCL BIT p1.2 ; serial clockSDA BIT p1.3 ; serial dataDSEG AT 20HORG 40H buffer: DS PSIZE ; storage for read/write dataORG 60H ; stack originstack: DS 20H ; stack depthCSEGORG 0000H ; power on/reset vectorjmp on_resetORG 0003H ; external interrupt 0 vectorreti ; undefinedORG 000BH ; timer 0 overflow vectorreti ; undefinedORG 0013H ; external interrupt 1 vectorreti ; undefinedORG 001BH ; timer 1 overflow vectorreti ; undefinedORG 0023H ; serial I/O interrupt vectorreti ; undefinedORG 0080H ; begin code spaceUSING 0 ; register bank zeroon_reset:mov sp, #(stack-1) ; initialize stack pointerInitialize AT24Cxx serial bus lines.setb SDA ; highsetb SCL ; highcall byte_filljc faultcall verify_byte_filljc faultcall page_filljc faultcall verify_page_filljc faultfault: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 zdata, #FILL ; set up fill datamov dptr, #0 ; initialize address pointerx51:mov addr_lo, dpl ; set up addressmov addr_hi, dphmov b, #120 ; retry counterx52:mov a, #PADDR ; programmable addresscall write_byte ; try to writejnc x53 ; jump if write OKdjnz b, x52 ; try againsetb c ; set timeout error flagjmp x54 ; exitx53:inc dptr ; advance address pointer; mov a, dpl ; check low byte; cjne a, #(LOW SIZE), x51 ; jump if not lastmov a, dph ; check high bytecjne a, #(HIGH SIZE), x51 ; jump if not lastclr c ; clear error flagx54:retverify_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 dptr, #0 ; initialize address pointer/countermov addr_lo, dpl ; set up addressmov addr_hi, dph ;mov b, #120 ; retry counterx81:mov a, #PADDR ; programmable addresscall read_random ; try to readjnc x82; jump if read OKdjnz b,x81 ; try againjmp x86
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号