Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microcontroller 如果使用延迟功能会造成不良影响(如何解决这种情况)?_Microcontroller_Pic - Fatal编程技术网

Microcontroller 如果使用延迟功能会造成不良影响(如何解决这种情况)?

Microcontroller 如果使用延迟功能会造成不良影响(如何解决这种情况)?,microcontroller,pic,Microcontroller,Pic,我有一个场景问题,我不想使用中断,因为同样的问题也会出现,我将被迫使用延迟,以确保特定的时间确实过去了 问题是: 我不想使用delay函数,因为它会停止要检查或执行的其他任务,从而出现以下情况: 我有一个电机,如果按下按钮1,电机会运行6秒,如果有人按下按钮2,电机2需要立即打开并工作2秒 因此,代码如下所示: main () { if( Rd0 == 0 ) // is button1 is pressed { RunMotre1(); __delay_ms(6000); StopM

我有一个场景问题,我不想使用中断,因为同样的问题也会出现,我将被迫使用延迟,以确保特定的时间确实过去了

问题是:

我不想使用delay函数,因为它会停止要检查或执行的其他任务,从而出现以下情况:

我有一个电机,如果按下按钮1,电机会运行6秒,如果有人按下按钮2,电机2需要立即打开并工作2秒

因此,代码如下所示:

main ()
{

if( Rd0 == 0 )   // is button1 is pressed
{
RunMotre1(); 

__delay_ms(6000);
StopMotor1();

}


if( Rd1 == 0 )  // is button2 is pressed
{
RunMotor2();
__delay_ms(2000);
StopMotor2();
}

}
此代码的问题如果我们按下按钮b启动马达2,他将 不工作导致系统延迟阻塞并等待完成

这导致如果我们启动->按按钮1->电机1运行->按按钮2启动电机2->电机2直到6秒钟过去后才工作

那么有没有办法解决这个问题呢

例如,如果存在比较谁运行了mutch time motor 1,如果为greate或=6 secound,则停止电机1示例(如果存在类似的情况):

或任何解决方案


使用xc8编译器的平台pic微控制器预设:我不知道您使用的是哪个pic,但我假设它至少有一个空闲计时器

这里的问题是,你需要一些东西来跟踪时间的流逝。我的建议是使用Arduino FW使用的相同技术:设置一个计时器,每X毫秒/秒触发一次中断,然后在中断服务例程(ISR)中更新一个计数器

按下按钮时,设置计数器,然后等待计数器达到另一个值,然后再关闭电机

我将在这里编写一个小的伪代码示例,因为我不记得如何使用PIC编译器正确编写ISR,也不知道PIC是什么(以及初始化计时器的命令)

我选择使用100ms的计时器粒度,但您可以根据自己的喜好进行更改。 我还选择将
计数器设置为8位变量;它最多可以计算25.5秒,但由于最高延迟为6秒,因此也可以

请注意,您只需要一个计时器,因此可以添加任意数量的电机,而无需添加计数器

char counter;
char motor1StartTime;
char motor2StartTime;
bool motor1Running;
bool motor2Running;

Timer1_ISR()
{
    counter++;
}

main()
{ 
    counter = 0;
    motor1StartTime = 0;
    motor2StartTime = 0;
    motor1Running = false;
    motor2Running = false;

    // Setup the timer to trigger the Timer1_ISR every 100ms
    ...

    while(1)
    {
        if( Rd0 == 0 )
        { // If the button is pressed start the motor and save the counter
            RunMotor1(); 
            motor1StartTime = counter;
            motor1Running = true;
        }

        if ((motor1Running) && ((counter - motor1StartTime) >= 60))
        { // If the motor is running from more than 6 seconds, shut it down
            StopMotor1();
            motor1Running = false;
        }


        if( Rd1 == 0 )
        { // If the button is pressed start the motor and save the counter
            RunMotor2(); 
            motor2StartTime = counter;
            motor2Running = true;
        }

        if ((motor2Running) && ((counter - motor2StartTime) >= 20))
        { // If the motor is running from more than 2 seconds, shut it down
            StopMotor2();
            motor2Running = false;
        }
    }
}
char counter;
char motor1StartTime;
char motor2StartTime;
bool motor1Running;
bool motor2Running;

Timer1_ISR()
{
    counter++;
}

main()
{ 
    counter = 0;
    motor1StartTime = 0;
    motor2StartTime = 0;
    motor1Running = false;
    motor2Running = false;

    // Setup the timer to trigger the Timer1_ISR every 100ms
    ...

    while(1)
    {
        if( Rd0 == 0 )
        { // If the button is pressed start the motor and save the counter
            RunMotor1(); 
            motor1StartTime = counter;
            motor1Running = true;
        }

        if ((motor1Running) && ((counter - motor1StartTime) >= 60))
        { // If the motor is running from more than 6 seconds, shut it down
            StopMotor1();
            motor1Running = false;
        }


        if( Rd1 == 0 )
        { // If the button is pressed start the motor and save the counter
            RunMotor2(); 
            motor2StartTime = counter;
            motor2Running = true;
        }

        if ((motor2Running) && ((counter - motor2StartTime) >= 20))
        { // If the motor is running from more than 2 seconds, shut it down
            StopMotor2();
            motor2Running = false;
        }
    }
}