关于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的电容。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. 这种情况通常只发生在高速并且连续的端口操作中,但这的确会发生,所以要在设计中考虑。
另,明显错误的设计,导致的“读-修改-写”问题,不再赘述。
上一篇:有关PIC单片机在线编程器、在线仿真器的问题
下一篇:PIC16F877A.H头文件详细注释
推荐阅读最新更新时间:2024-03-16 15:07
设计资源 培训 开发板 精华推荐
- 希润医疗孟铭强:手功能软体机器人,让脑卒中患者重获新生
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 赛灵思工业与医疗专题有奖问答
- 中秋“国宴“ :拍摄板卡上的国产元器件,抽开心小礼品
- 【泰克注册观看有礼】 PCI-SIG 前主席解析:PCI Express5.0测试方案和测量挑战
- 有奖直播 | 同质化严重,缺乏创新,ST60毫米波非接触连接器,赋予你独特的产品设计,重拾市场话语权
- 有奖直播:助力AI算力,下一代GPU服务器中卡缘高速互连解决方案哪里找?
- 【EEWORLD第二十六届】2011年05月社区明星人物揭晓!
- 报名STM32全国巡回研讨会,打卡轻松得ST开发板
- ADI 全新技术资料集锦
- 泰克送你三板斧,招招解决电源测试大难题!答题赢好礼喽!
- ADI有奖下载活动之14 ADI公司针对pH计和电导率仪的演示系统
- 万能电路板正面布线的方法
- InstaSPIN-FOC lab2a与lab2b请教
- 在DC/DC转换器中采用陶瓷或电解输出电容器——为什么不能兼用呢?
- 传输线原理与Smith Chart
- Altium designer 15.0更新PCB后,为什么整个图都变透明了?
- 求教,大神们,看看我这个串口采集数据发送和另一个机子接收显示的程序哪里不对啊!
- ise上做的分频程序创建约束时出现your design has no clocks !!!
- Altium Designer 18官方教程 中英文版本
- 【晒样片】+TI 样片免费申,好礼大放送~
- 求一个msp430f149的摄像头图像采集的程序?????????