读-修改-写问题.doc_第1页
读-修改-写问题.doc_第2页
读-修改-写问题.doc_第3页
读-修改-写问题.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PIC读-修改-写问题何谓读-修改-写,导致的问题及其解决之道:只要PICmicro的命令,所处理的FILE (缓存器,内存,和I/O的统称),其最终的值,和命令处理前的值有关,那么,这种命令便是所谓的读-修改-写命令。因为这类命令的操作,可以再细分为三个小步骤,即是读(READ),修改(MODIFY),接着才是写(WRITE)。如:ADDWF,DECF,IORWF,XORWF,BSF,BCF,等等皆是。在使用这类命令的时候,如果所处理的FILE是I/O, 就必须要特别小心。因为,假设任务电压为5V;而您在PORTB的8支脚,没有串接电阻,分别接了8颗二极管(也许是LED)到地。如果,您在先前下了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作读-修改-写命令。另外还有一类要注意的状况是:如当您将”BSF PORTB,0”和”BSF PORTB,1”两条命令连在一起,没有空运行时,纵使您没有上一段所述线路设计不良之状况,有时候您也会发现PORTB, 0的高电位也会不见了。导致这个现象的原因是:因为也许您这两支脚的外面接了长导线,有相当大的电容、电感性会阻止电位的变化;而且,PIC命令运行是在最后1/4周期时”写出”,而在第一个1/4周期时”读入”,PORTB,0还来不及升至高电位,便被下一个命令读回去了。解决之道是:通常在两道命令间插入个NOP命令就可以了。上面是正解,下面转自21ic可以作为以上的参考关于“读修改写”(转自21ic) 注:翻译也是转的翻译了一个关于“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/1997Discussion: 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/1997Discussion: 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 PORT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论