millis() function for Atmega8

Discussion in 'Embedded Systems and Microcontrollers' started by aditya369007, Sep 22, 2014.

  1. aditya369007

    Thread Starter New Member

    Jul 24, 2014
    I wanted to know if there was any code for ATmega8 that performs the same function as millis() in arduino ?
    i.e lines of code that will enable me to know the time in milliseconds since the controller was turned on.
  2. Shagas

    Active Member

    May 13, 2013
    Perhaps you can look into how it's implemented in the arduino code.
    Perhaps if you don't need the 16bit timer then you could set it up with a prescalar and make it interrupt on overflow and increment a *volatile* variable. Then you could just calculate the time that passed by
    seeing how many ticks you have. I think arduino does something similar. Also , if you are using arduino functions that use timers then your millis() function will be slightly imprecise which you might or might not mind.

    So if you are clocking at 1Mhz , you could set up your timer with no prescalar to interrupt at OCRXY = 1000; And in the interrupt routine you can increment a *volatile counter
    which means that your interrupt will increment a variable every millisecond
    Last edited: Sep 22, 2014
  3. aditya369007

    Thread Starter New Member

    Jul 24, 2014
    ok is there any example code for it?