英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 2990|回复: 0
打印 上一主题 下一主题

关于“读--修改--写”

[复制链接]
跳转到指定楼层
1#
发表于 2009-5-18 11:54:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
翻译了一个关于“Read Modify Write”读-修改-写的QA,希望有帮助。匆匆翻译,如有错误,请以原文为准。
Topic: I have seen references to "Read-Modify-Write" instructions in your datasheet, but I do not know what that is. Can you explain what it is and why I need to know this?
我曾在数据手册里看到“读-修改-写”的相关说明,但我不知道这是什么。你能解释一下它的含义以及为什么要注意它吗?
  Section Instructions
  Posted: 10/25/1997  
  Discussion:
An easy example of a Read-Modify-Write (or RMW) instruction is the bit clear instruction BCF. 一个关于“读-修改-写”的简单例子就是位清零指令BCF。You might think that the processor just clears the bit, which on a port output pin would clear the pin.你可能认为处理器只是清这个位,对于输出端口就是清零这个管脚。 What actually happens is the whole port (or register) is first read, THEN the bit is cleared, then the new modified value is written back to the port (or register). 而实际发生的是整个端口(或寄存器)先被读入,之后相应位被清,然后修改后的新值被写回到这个端口(或寄存器)。Actually, any instruction that depends on a value currently in the register is going to be a Read-Modify-Write instruction. 实际上,任何基于当前寄存器内值的操作指令都是一个“读-修改-写”指令。This includes ADDWF, SUBWF, BCF, BSF, INCF, XORWF, etc... Instructions that do not depend on the current register value, like MOVWF, CLRF, and so on are not RMW instructions.这包括ADDWF,SUBWF,BCF,BSF,INCF,XORWF等等。不基于当前寄存器内值的指令,如MOVWF,CLRF等不是“读-修改-写”指令。

One situation where you would want to consider the affects of a RMW instruction is a port that is continuously changed from input to output and back.如果你不断地改变一个端口的输入输出模式,在这个情形下你应该注意“读-修改-写”指令的影响。 For example, say you have TRISB set to all outputs, and write all ones to the PORTB register, all of the PORTB pins will go high. 例如,如果说你把TRISB设为都作输出,并且将PORTB寄存器都写“1”,所有PORTB的引脚都将变高。Now, say you turn pin RB3 into an input, which happens to go low. 现在,如果说你把RB3变为输入,恰好这时的输入为低。A BCF PORTB,6 is then executed to drive pin RB6 low. 然后你执行一个BCF PORTB,6 的指令把RB6输出低。If you then turn RB3 back into an output, it will also now drive low, even though the last value you put there was a one.这时如果你再把RB3设回输出状态,它将也输出低,尽管你上次你在PORTB里这个位设置的值是“1”。
What happened was tha the BCF of the other pin (RB6) cause the whole port to be read, including the zero on RB3 when it was an input. 之所以这样,是因为对其他引脚(RB6)做BCF时,整个端口都被读进来了,包括当RB3引脚处于输入态时的低电平。The bit 6 was changed as requested, but since RB3 was read as a zero, zero will also be placed back into that port latch, overwriting the one that was there before. 位6按要求改变了,但是由于RB3读入为“0”,于是在RB3位的“0”也被放回了端口锁存器,修改了原来在这个位置的“1”。When the pin is turned back into an output, the new value was reflected. 当这个引脚重新变为输出态时,这个新值反应出来,输出“0”而不是“1”了。

   
Topic: When I perform a BCF, other pins get cleared in the port. Why?
当我执行一个BCF命令时,端口的其他管脚也被清零,为什么?
  Section Instructions
  Posted: 10/25/1997  
  Discussion:
If this is on a PIC16C7X device, you have not configured the I/O pins properly in the ADCON1 register.如果这发生在PIC16C7X的器件上,可能是因为你没有正确设置ADCON1中的IO管脚属性。 If a pin is configured for analog input, any read of that pin will read a zero, regardless of the voltage on the pin. 如果一个管脚被配置为模拟输入,对这个管脚读将始终为“0”,不管在这个引脚上是否有电压。This is an exception to the normal rule that the pin state is always read. 通常情况下这些管脚始终都是作输入的,不会有这种现象。You can still configure an anolog pin as an output in the TRIS register, and drive the pin high or low by writing to it, but you will always read a zero. 你也可以通过配置TRIS寄存器来改变这些模拟输入管脚作为输出,并且通过写寄存器驱动这些引脚为高或低,但是你将一直读到“0”。Therefore if you execute a Read-Modify-Write instruction (see previous question) all analog pins are read as zero, and those not directly modified by the instruction will be written back to the port latch as zero. 所以当你执行一个“读-修改-写”指令(见前一个问题),所有的模拟管脚读为“0”,并且在没有被指令直接修改的情况下,将端口锁存器的值改为“0”。A pin configured as analog is expected to have values that may be neither high nor low to a digital pin, or floating. Floating inputs on digital pins are a no-no, and can lead to high current draw in the input buffer, so the input buffer is disabled. 一个被配置为模拟口的管脚将被认为有一个可能既非高也非低的值,或者是浮空的,相对于数字口而言。在数字口上的输入也是一种非高非低的状态,并且能够导致输入缓冲器的大电流泄漏,所以输入缓冲器被禁止。

Another case where a RMW instruction may seem to change other pin values unexpectedly can be illustrated as follows: 另一种情况下,“读-修改-写”指令好像是违背期望地修改了其他引脚的值,一般能被描述如下:Suppose you make PORTC all outputs and drive the pins low. 假设你把PORTC全作为输出并驱动所有管脚为低。On each of the port pins is an LED connect to ground, such that a high output lights it. Across each LED is a 100uF capacitor. 在这个端口的每个管脚上有一个LED连到地,这样输出高时将点亮LED。并在每一个LED上有一个100uF的电容。Let's also suppose that the processor is running very fast, say 20MHz. 让我们也假设处理器运行得非常快,比如说20MHz。Now if you go down the port setting each pin in order; BSF PORTC,0 then BSF PORTC,1 then BSF PORTC,2 and so on, you may see that only the last pin was set, and only the last LED actually turns on. 现在如果你清端口后依次去置位引脚,如BSF PORTC,0 然后BCF PORTC,1 接着BCF PORTC,2 这样下去,你将看到只有最后的一个管脚被真正置位了,只有一个LED点亮。This is because the capacitors take a while to charge. As each pin was set, the pin before it was not charged yet and so was read as a zero. 这是因为电容需要时间来充电。当一个管脚被置位时,它前面被置位的那个管脚还没有充电完全,所以读到的还是“0”。This zero is written back out to the port latch (RMW, remember) which clears the bit you just tried to set the instruction before. 这个“0”将被写回端口锁存器(读-修改-写),清掉前面你刚用指令来置“1”的那个位。This is usually only a concern at high speeds and for successive port operations, but it can happen so take it into consideration. 这种情况通常只发生在高速并且连续的端口操作中,但这的确会发生,所以要在设计中考虑。
另,明显错误的设计,导致的“读-修改-写”问题,不再赘述。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-12-25 21:30 , Processed in 0.053607 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表