繁体   English   中英

使用带按钮的 MPLAB XC8 故障复位端口模拟 PIC16F877A

[英]Simulating PIC16F877A with MPLAB XC8 trouble resetting port with push button

void main(void) { 

    TRISB=0x07; 
    PORTB=0x00; 
    TRISD=0x00; 
    PORTD=0x00;  

    while(1) 
    { 
        if(RB0)  // when RB0 is high
        { 
            __delay_ms(100);
            PORTD++;    // PORTD increments, PORTD outputs to a LED
            __delay_ms(100); 
        } 

        if(RB2) //when RB2 is high
        {
            __delay_ms(100);
            PORTD = 0x00; // reset PORTD to 0
            break;
        } 
    } 

} 

我正在使用上面的代码尝试在RB2变高时重置PORTD 我在 MPLAB 中设置我的激励,以便在单击时向RD0RD2发送一个 20ms 脉冲高信号,并观察PORTD的 SFR 值。

RD2应该在设置为高电平时清除PORTD ,但是当我观察PORTD的 SFR 值时, PORTD总是先递增,然后在第二次点击时变为 0。

所以假设PORTD的 SFR 值为 3,当 RB0 为高电平时, PORTD递增到 4,当RD2为高电平时,它应该 go 回零,但它变为 5,然后如果RD2再次为高电平则变为零.

我将如何修改我的代码以使RD2立即重置PORTD

if(RB0) // when RB0 is high 
{ 
   PORTD++; //Post increment 
}


if(RB2) //when RB2 is high
{
    
    PORTD = 0x00; 
    // Since ypu used post increment first PORTD will increment by 1 and then 
    it will be assigned 0
  
} 

我将如何修改我的代码以使 RD2 立即重置 PORTD?

解决方法:使用前置自增或者PORTD = PORTD + 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM