我有一个增加频率的程序,当按下按钮时,频率应该保持恒定,下一次按下应该设法使频率继续增加到10Hz,这对我来说是随机工作的,没有使用中断。为什么这不起作用?当我按下按钮时,我只会增加频率。感谢您的回答
DigitalOut myled(LED3);
InterruptIn button(PA_12);
PwmOut ledPWM(PB_0);
int volatile state=0;
void pb_hit_interrupt (void) {
state++;
}
int main()
{
button.mode(PullUp);
wait(.01);
button.fall(&pb_hit_interrupt);
float pulse =0;
float perioda =0;
int frequency = 1;
perioda=1.0f/frequency;
pulse = (perioda/2.0f);
while(1) {
if(state==1){
frequency++;
perioda=1.0f/frequency;
pulse = (perioda/2.0f);
ledPWM.period(perioda);
ledPWM.pulsewidth(pulse);
wait(1.0f);
if(frequency==10) frequency = 0;
}
if(state==2) {
ledPWM.period(perioda);
ledPWM.pulsewidth(pulse);
wait(1.0f);
}
if(state > 2) state=0;
}
}