




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ATMEL公司24系列EEPROM编程举例作者:尘封往事 文章来源:本站原创 点击数: 2043 更新时间:2006-1-12 13:07:34 NAMEControl_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.FADDREQU0a0h; fixed address for AT24Cxx EEPROMsPADDREQU0; programmable address (0.7)SIZEEQU2000h; bytes per AT24C64PSIZEEQU32; bytes per page for AT24C64FILLEQU55h; example fill value; Register definitions.indexEQUr0; buffer pointerkountEQUr1; byte count registerzdataEQUr1; data registeraddr_loEQUr2; 2-byte address registeraddr_hiEQUr3; Microcontroller connections to AT24Cxx serial bus lines.SCLBITp1.2; serial clockSDABITp1.3; serial dataDSEG AT 20HORG40Hbuffer:DSPSIZE; storage for read/write dataORG60H; stack originstack:DS20H; stack depthCSEGORG0000H; power on/reset vectorjmpon_resetORG0003H; external interrupt 0 vectorreti; undefinedORG000BH; timer 0 overflow vectorreti; undefinedORG0013H; external interrupt 1 vectorreti; undefinedORG001BH; timer 1 overflow vectorreti; undefinedORG0023H; serial I/O interrupt vectorreti; undefinedORG0080H; begin code spaceUSING0; register bank zeroon_reset:movsp, #(stack-1); initialize stack pointer; Initialize AT24Cxx serial bus lines.setbSDA; highsetbSCL; highcallbyte_filljcfaultcallverify_byte_filljcfaultcallpage_filljcfaultcallverify_page_filljcfaultfault: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.movzdata, #FILL; set up fill datamovdptr, #0; initialize address pointerx51:movaddr_lo, dpl; set up addressmovaddr_hi, dph;movb, #120; retry counterx52:mova, #PADDR; programmable addresscallwrite_byte; try to writejncx53; jump if write OKdjnzb, x52; try againsetbc; set timeout error flagjmpx54; exitx53:incdptr; advance address pointer;mova, dpl; check low byte;cjnea, #(LOW SIZE), x51; jump if not lastmova, dph; check high bytecjnea, #(HIGH SIZE), x51; jump if not lastclrc; 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.movdptr, #0; initialize address pointer/countermovaddr_lo, dpl; set up addressmovaddr_hi, dph;movb, #120; retry counterx81:mova, #PADDR; programmable addresscallread_random; try to readjncx82; jump if read OKdjnzb, x81; try againjmpx86; set error flag and exitx82:cjnea, #FILL, x86; jump if compare errorjmpx85; do remaining addressesx83:mova, #PADDRcallread_currentjcx87; jump if read failscjnea, #FILL, x86; jump if compare errorx85:incdptr; advance address pointermova, dph; check high bytecjnea, #(HIGH SIZE), x83; jump if not lastclrc; clear error flagjmpx87; exitx86:setbc; set error flagx87:retpage_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.movb, #PSIZE; bytes per pagemovindex, #buffer; point to bufferx61:movindex, #FILL; put fill value in bufferincindex; advance pointerdjnzb, x61; next byte; Copy buffer to device, one page at a time.movdptr, #0; initialize address pointerx62:movaddr_lo, dpl; set up addressmovaddr_hi, dph;movkount, #PSIZE; bytes per pagemovb, #120; retry counterx63:mova, #PADDR; programmable addresscallwrite_block; try to writejncx64; jump if write OKdjnzb, x63; try againsetbc; set timeout error flagjmpx66; exitx64:; Add page size to address pointer.mova, dpl; get low byteadda, #PSIZE; add page sizemovdpl, a; save low bytejncx65; jump if high byte not affectedincdph; increment high bytex65:;cjnea, #(LOW SIZE), x62; jump if low byte not lastmova, dph; check high bytecjnea, #(HIGH SIZE), x62; jump if not lastclrc; clear error flagx66:retverify_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.movdptr, #0; initialize address pointerx71:movaddr_lo, dpl; set up addressmovaddr_hi, dph;movkount, #PSIZE; bytes per pagemovb, #120; retry counterx72:mova, #PADDR; programmable addresscallread_block; try to readjncx74; jump if read OKdjnzb, x72; try againx73:setbc; set error flagjmpx77; exitx74:; Verify buffer contents.movb, #PSIZE; bytes per pagemovindex, #buffer; point to bufferx75:cjneindex, #FILL, x73; jump if compare failsincindex; advance pointerdjnzb, x75; next byte; Add page size to address pointer.mova, dpl; get low byteadda, #PSIZE; add page sizemovdpl, a; save low bytejncx76; jump if high byte not affectedincdph; increment high bytex76:;cjnea, #(LOW SIZE), x71; jump if low byte not lastmova, dph; check high bytecjnea, #(HIGH SIZE), x71; jump if not lastclrc; clear error flagx77:retwrite_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.callstartjcx38; abort if bus not availablerla; programmable address to bits 3:1orla, #FADDR; add fixed addressclracc.0; specify write operationcallshout; send device addressjcx37; abort if no acknowledgemova, addr_hi; send high byte of addresscallshout;jcx37; abort if no acknowledgemova, addr_lo; send low byte of addresscallshout;jcx37; abort if no acknowledgemovindex, #buffer; point to bufferx36:mova, index; get datacallshout; send datajcx37; abort if no acknowledgeincindex; advance buffer pointerdjnzkount, x36; next byteclrc; clear error flagx37:callstopx38:retread_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.callstartjcx35; abort if bus not availablerla; programmable address to bits 3:1orla, #FADDR; add fixed addressmovindex, a; save copy of device addressclracc.0; specify write operationcallshout; send device addressjcx34; abort if no acknowledgemova, addr_hi; send high byte of addresscallshout;jcx34; abort if no acknowledgemova, addr_lo; send low byte of addresscallshout;jcx34; abort if no acknowledge; Send read command and receive data.callstart; second start for readjcx34; abort if bus not availablemova, index; get device addresssetbacc.0; specify read operationcallshout; send device addressjcx34; abort if no acknowledgemovindex, #buffer; point to bufferx31:callshin; receive data bytemovindex, a; save datacjnekount, #1, x32; jump if not last bytecallNAK; do not acknowledge last bytejmpx33; donex32:callACK; acknowledge byteincindex; advance buffer pointerdjnzkount, x31; next bytex33:clrc; clear error flagx34:callstopx35:retwrite_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.callstartjcx49; abort if bus not availablerla; programmable address to bits 3:1orla, #FADDR; add fixed addressclracc.0; specify write operationcallshout; send device addressjcx48; abort if no acknowledgemova, addr_hi; send high byte of addresscallshout;jcx48; abort if no acknowledgemova, addr_lo; send low byte of addresscallshout;jcx48; abort if no acknowledgemova, zdata; get datacallshout; send datajcx48; abort if no acknowledgeclrc; clear error flagx48:callstopx49:retread_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.callstartjcx45; abort if bus not availablerla; programmable address to bits 3:1orla, #FADDR; add fixed addresssetbacc.0; specify read operationcallshout; send device addressjcx44; abort if no acknowledgecallshin; receive data bytecallNAK; do not acknowledge byteclrc; clear error flagx44:callstopx45:retread_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.pushbmovb, a; save copy of programmable address; Send dummy write command to set internal address.callstartjcx47; abort if bus not availablerla; programmable address to bits 3:1orla, #FADDR; add fixed addressclracc.0; specify write operationcallshout; send device addressjcx46; abort if no acknowledgemova, addr_hi; send high byte of addresscallshout;jcx46; abort if no acknowledgemova, addr_lo; send low byte of addresscallshout;jcx46; abort if no acknowledge; Call Current Address Read function.mova, b; get programmable addresscallread_currentjmpx47; exitx46:callstopx47:popbretstart:; Send START, defined as high-to-low SDA with SCL high.; Return with SCL, SDA low.; Returns CY set if bus is not available.setbSDAsetbSCL; Verify bus available.jnbSDA, x40; jump if not highjnbSCL, x40; jump if not highnop; enforce setup delay and cycle delayclrSDAnop; enforce hold delaynop;nop;nop;nop;clrSCLclrc; clear error flagjmpx41x40:setbc; set error flagx41:retstop:; Send STOP, defined as low-to-high SDA with SCL high.; SCL expected low on entry. Return with SCL, SDA high.clrSDAnop; enforce SCL low and data setupnopsetbSCLnop; enforce setup delaynop;nop;nop;nop;setbSDAretshout:; Shift out a byte to the AT24Cxx, most significant bit fir
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 操作系统期末考试试题及答案
- 病区备用药品管理
- 物业安全培训试题及答案
- 物权法五套试题及答案
- 2025能源交易所规范交易合同
- 2025年贵州省中考数学试题(解析版)
- 2025国家科研经费资助合同
- 物料考核试题及答案
- 物理卓越计划试题及答案
- 2025影视制作合同书(标准版)
- 慢性咳嗽大讲堂课件
- 2025广东惠州惠城区人民政府河南岸街道办事处招聘编外人员12人笔试备考试题及答案解析
- 呼吸道疾病用药课件
- 特殊情况下紧急供货措施方案
- 工业机器人技术及其应用
- 暂住人员管理办法
- DB 4601∕T 10-2024 二次供水工程技术规范
- 胸部气管损伤的护理课件
- 护理查对不良事件案例分析
- 危大工程考试题目含答案
- 儿童眼保健健康讲座课件
评论
0/150
提交评论