您的位置 首页 报告

关于PIC单片机“读-修正-写”的相关阐明

关于PIC单片机读-修改-写的相关说明Topic:IhaveseenreferencestoRead-Modify-Writeinstructionsinyourdatasheet,…

关于PIC单片机修正”的相关阐明

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?

我曾在数据手册里看到“读-修正-写”的相关阐明,但我不知道这是什么。你能解释一下它的意义以及为什么要留意它吗?

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指令时,端口的其他管脚也被清零,为什么?

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的电容。Lets 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. 这种状况一般只产生在高速而且接连的端口操作中,但这确实会产生,所以要在规划中考虑。

另,显着过错的规划,导致的“读-修正-写”问题,不再赘述。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/baogao/272600.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部