Bilal
Bilal

Reputation: 1382

equivalent of arduino millis(); function (that return passed time) in pic microcontroller?

I have the same situation of this code it resolve my problem (this code blinks leds without using delay function that block the execution until a set time has passed)

my question is there eny equevalent to this function in pic microcontrollers ?

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);
    }
}

my question is there a function or lib in xc8 compiler to do the same work , of millis() function ?

and thank you .

Upvotes: 0

Views: 3601

Answers (2)

rxmxn
rxmxn

Reputation: 499

The best way to achieve this is always to use a Timer interruption, so your program wont block while the second is running. In C-Compiler you would have to do something like this:

#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
}

Upvotes: 1

Iledran
Iledran

Reputation: 133

There is no explicit timer function. But I'm thinking about the Time library, just because your blink interval is set to 1000 ms = 1 s .

In fact you can build a function alone that return "true" if a second gone, with argument the second written after the function is called. If the seconds are the same, return false so don't change the state of the led. If the seconds are different, return true and change the state of the led.

For example,the structure of your code will be something like this:

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;
           }
    }

Upvotes: 0

Related Questions