pic18f 为什么使用 LATx?读修改写解释

pic18f why use LATx? Read modify write explanation

我想知道 PORTx 上的读取修改写入指令究竟是如何工作的,以及它们为什么会导致问题。我之前使用过 pic12f683,我不记得在写入 GPIO 时遇到过任何问题。为什么有些示例有效而其他示例无效?

//works
while(1) {
  LATA++;
  Delay_ms(1000);
}

//works
char i = 0;
while(1) {
  PORTA = i++;
  Delay_ms(1000);
}


//doesn't work
while(1) {
  PORTA++;
  Delay_ms(1000);
}

//doesn't work
char i = 0;
while(1) {
  i = PORTA;
  PORTA = i++;
  Delay_ms(1000);
}

//doesn't work either for some reason (PORTA seems to be read as 0 always)
char i = 0;
while(1) {
  i = PORTA;
  LATA = i++;
  Delay_ms(1000);
}

//neither does this one
char i = 0;
while(1) {
  i = LATA;
  LATA = i++;
  Delay_ms(1000);
}

pic18f4550 数据表第 10 节说:

Reading the PORTA register reads the status of the pins; writing to it will write to the port latch.

这不是说写 PORTA 和写 LATA 是一样的吗(端口锁存器……或者是)? 这是图片,以防它有助于解释。

Reading the PORTA register reads the status of the pins; writing to it will write to the port latch.

如上所说,意思就是你要写的东西永远写在一个LAT(锁存寄存器)里,但不代表就是端口输出的状态。例如,您可能有错误的 TRIS 配置(将端口或 IO 引脚设置为输入),或者硬件故障将引脚拉低或拉高等。所有这些都意味着您可以写入端口(LAT)但是您将阅读的结果不必与您编写的结果相同。

无论 LAT 中的内容如何,​​从端口读取总是 return 这些线路的当前(物理)状态。