微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 关于PIC单片机“读-修改-写”的相关说明

关于PIC单片机“读-修改-写”的相关说明

时间:11-13 来源:互联网 点击:

关于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”

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top