【问题标题】:equivalent of arduino millis(); function (that return passed time) in pic microcontroller?相当于 arduino millis(); pic微控制器中的功能(返回经过的时间)?
【发布时间】:2015-10-09 15:58:10
【问题描述】:

我遇到了与此代码相同的情况,它解决了我的问题(此代码在不使用延迟功能的情况下使 LED 闪烁,该功能会阻止执行,直到经过设定的时间)

我的问题是 pic 微控制器中是否有与此功能等效的功能?

const int ledPin =  13;      
int ledState = LOW;            
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
{
    pinMode(ledPin, OUTPUT);
}

void loop()
{
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval)
    {
        previousMillis = currentMillis;  // save the last time you blinked the LED
        // if the LED is off turn it on and vice-versa:

        if (ledState == LOW)
        {
            ledState = HIGH;
        } 
        else 
        {
            ledState = LOW;
        }

        // set the LED with the ledState of the variable:
        digitalWrite(ledPin, ledState);
    }
}

我的问题是 xc8 编译器中是否有一个函数或 lib 可以完成与 millis() 函数相同的工作?

谢谢你。

【问题讨论】:

  • 不,没有内置任何东西。我经常做的是配置一个 1ms 的定时器中断,我可以在那里跟踪经过的时间。
  • 你能给我更多的细节吗,我可以用你说的这个方法,配置一个 1ms 的定时器中断或者如果可能的话给我那个代码,谢谢
  • 您使用哪种编译器来编码 PIC?

标签: arduino microcontroller pic


【解决方案1】:

实现这一点的最佳方法始终是使用定时器中断,这样您的程序就不会在第二个运行时阻塞。在 C 编译器中,您必须执行以下操作:

#define tmr0 53036  //50ms with a 8MHz clock

#INT_TIMER0
void TIMER0()
{       
    static int counter = 20;
    set_timer0(tmr0);   //reload timer register
    if(counter-- == 0)
    {
        //1 sec.
        counter = 20;
        output_bit(PIN_B4,!input(PIN_B4));  
    }
}

void main()
{      
  //configurations here
  set_tris_b(0x00);  //0b00000000 
  setup_timer_0(RTCC_INTERNAL | RTCC_DIV_8);
  set_timer0(tmr0); //50ms : TMR0=65536 - overflow_time/(4*Tosc*prescaler)=53036 [in this case overflow_time=50ms, Tosc=1/8MHz=125ns, prescaler=8]
  //since TMR0=53036 -> overflow_time=(65536-TMR0)*(4*Tosc*prescaler)=(65536-53036)*(4*125ns*8)=50ms
  enable_interrupts(INT_TIMER0);
  enable_interrupts(GLOBAL);
  //********************************************

  while(true){...} //whatever you want
}

【讨论】:

  • 好消息,欢迎您。请记住接受对您有帮助的答案并投票。
【解决方案2】:

没有明确的计时器功能。 但我在考虑Time library,只是因为你的眨眼间隔设置为 1000 ms = 1 s 。

事实上,您可以单独构建一个函数,如果第二个消失,则返回“true”,参数是在调用函数之后编写的第二个。 如果秒数相同,则返回 false,因此不要更改 LED 的状态。 如果秒数不同,则返回 true 并更改 LED 的状态。

例如,你的代码结构是这样的:

void loop()
{
    precSec = second();
    flag = secondIsGone(precSec);

    if (flag) {
      if (ledState == LOW) {
        ledState = HIGH;
        } 
        else {
            ledState = LOW;
        }
        digitalWrite(ledPin, ledState);
    }
} //loop

    boolean secondIsGone (int precSec) {
      if (precSec != second() ) {
         return true;
         }
         else {
           return false;
           }
    }

【讨论】:

  • 好的,我想做的,但我能得到谁。一秒钟确实过去了。您所说的 second() 方法将是什么;知道我在 pic mcu 上,我不想使用延迟。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多