【问题标题】:Simulating PIC16F877A with MPLAB XC8 trouble resetting port with push button使用带按钮的 MPLAB XC8 故障复位端口模拟 PIC16F877A
【发布时间】:2020-05-04 23:06:52
【问题描述】:
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 为高时,它应该回到零,但它会变为 5,并且如果RD2 再次变高,则归零。

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

【问题讨论】:

  • break将在您的解释中退出您的whileloop. Is this what you want? Another issue: I guess you are mixing RB2and RD2`。

标签: controller embedded pic mplab


【解决方案1】:
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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2014-11-28
    • 2023-03-31
    相关资源
    最近更新 更多