读-修改-写问题

上传人:wt****50 文档编号:34329892 上传时间:2018-02-23 格式:DOC 页数:4 大小:42KB
返回 下载 相关 举报
读-修改-写问题_第1页
第1页 / 共4页
读-修改-写问题_第2页
第2页 / 共4页
读-修改-写问题_第3页
第3页 / 共4页
读-修改-写问题_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《读-修改-写问题》由会员分享,可在线阅读,更多相关《读-修改-写问题(4页珍藏版)》请在金锄头文库上搜索。

1、PIC 读-修改-写问题何谓读-修改-写,导致的问题及其解决之道:只要 PICmicro的命令,所处理的 FILE (缓存器,内存,和 I/O的统称),其最终的值,和命令处理前的值有关,那么,这种命令便是所谓的读-修改-写命令。因为这类命令的操作,可以再细分为三个小步骤,即是读(READ),修改(MODIFY),接着才是写(WRITE)。如:ADDWF,DECF,IORWF,XORWF,BSF,BCF,等等皆是。在使用这类命令的时候,如果所处理的 FILE是 I/O, 就必须要特别小心。因为,假设任务电压为 5V;而您在 PORTB的 8支脚,没有串接电阻,分别接了 8颗二极管(也许是 LED

2、)到地。如果,您在先前下了BSF PORTB,0(将 b0口置 1)这条命令,使得 PORTB第 0脚上的 LED亮了。当您再运行了”BSF PORTB,1这条命令后,虽然 PORTB第 1脚上的 LED亮了,但是,PORTB 第 0脚上的 LED却会熄掉。原因是因为,运行”BSF PORTB,1”时,会先将整个 PORTB的值往回读,而它读到的 PORTB第 0脚,却是相当于低电位(被二极管拉为 0.7V)。当它再把整个 PORTB的值写出去的时候,PORTB 第 0脚就被写成低电位,自然灯就熄了。解决之道是:串电阻,或者不要直接对 I/O作读-修改-写命令。另外还有一类要注意的状况是:如当

3、您将”BSF PORTB,0”和”BSF PORTB,1”两条命令连在一起,没有空运行时,纵使您没有上一段所述线路设计不良之状况,有时候您也会发现 PORTB, 0的高电位也会不见了。导致这个现象的原因是:因为也许您这两支脚的外面接了长导线,有相当大的电容、电感性会阻止电位的变化;而且,PIC 命令运行是在最后1/4周期时”写出”,而在第一个 1/4周期时”读入”,PORTB,0 还来不及升至高电位,便被下一个命令读回去了。解决之道是:通常在两道命令间插入个 NOP命令就可以了。上面是正解,下面转自 21ic 可以作为以上的参考关于“读修改写”(转自 21ic) 注:翻译也是转的翻译了一个关于

4、“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 Pos

5、ted: 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 hap

6、pens 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-M

7、odify-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 等不是

8、“读修改写”指令。 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, al

9、l 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 in

10、to 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 )

11、做 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 i

12、nto 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 pi

13、ns 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 pi

14、n 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-

15、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 v

16、alues 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: 另一种情况下,“读修改写”指令好像是违背期望地修改了其他引脚的值,一般能被描述

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号