P1DIR |= 0x01; // Set P1.0 (GREEN LED) to output direction
P4DIR |= 0x40; // Set P4.6 (RED LED) to output direction
P1OUT |= 0x01; // Set GREEN LED on
P4OUT |= 0x40; // Set RED LED on
P1REN |= 0x02; // enable P1.1 (Pushbutton S2)
P1DIR &= ~0x02; // enable read port P1.1 (Pushbutton S2)
P4REN |= 0x20; // enable P4.5 (Pushbutton S1)
P4DIR &= ~0x20; // enable read port P4.5 (PushButton S1)
volatile unsigned int i;
unsigned int counter = 0;
while(1)
{
if((P4IN & BIT5)== 0) // is the pushbutton S2 pressed? (P1.1)
{
printf("c= %d\r\n",counter);
P4OUT ^= 0x40; // toggle RED LED
i = 10000;
do i--;
while(i != 0);
counter++;
}
if((P1IN & 0x02)==0)
{
printf("c= %d\r\n",counter);
counter--;
}
if((P1IN & 0x02)==0)
{
P1OUT |= 0x01; // turn on GREEN LED when pushbutton is pressed
P4OUT ^= 0x40; // toggle RED LED
i = 10000;
do i--;
while(i != 0);
}
else
{
P1OUT &= ~0x01; // Turns off GREEN LED
P4OUT ^= 0x40; // toggle RED LED
i = 10000;
do i--;
while(i != 0);
}
}
return 0;
}
您好,我当前的代码允许我按下按钮 S1 并向控制台打印一条语句,同时每次按下它都会增加它。它还允许我在每次按下按钮 S2 时减小它。但是,我的问题是,当我开始增加并想减少该值时,它会在减少之前再次增加(反之亦然,如果我尝试在减少后增加,它会在增加之前再次减少)。我想知道为什么会这样,我该怎么做才能使程序不会发生这种情况并立即减少/增加。谢谢