Equivalent of millis() in Atmel studio












1














This code It works in atmel studio.But this millis function is starting to run 31-34 miliseconds behind of arduino millis fuction.Why?I am adding picture.



Picture: https://imageshack.com/a/img922/3060/umtNJp.jpg



Update 2



#define F_CPU 8000000UL
#include <avr/io.h>

#include <avr/interrupt.h>

#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

volatile uint8_t count;

ISR (TIMER0_OVF_vect)

{


unsigned long m = timer0_millis;
unsigned char f = timer0_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer0_fract = f;
timer0_millis = m;
timer0_overflow_count++;





}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;

return m;
}



int main (void)

{
DDRB |= (1<<0);
TCNT0 = 0;
count = 0;
TCCR0B |= (0<<CS02) | (1<<CS01) | (1<<CS00); // PRESCALER 64
TIMSK0 = (1<<TOIE0);
sei();

while(1)
{
millis() to uart.

}

}









share|improve this question




















  • 3




    cross post stackoverflow.com/questions/53780848/…
    – Juraj
    Dec 14 '18 at 14:42
















1














This code It works in atmel studio.But this millis function is starting to run 31-34 miliseconds behind of arduino millis fuction.Why?I am adding picture.



Picture: https://imageshack.com/a/img922/3060/umtNJp.jpg



Update 2



#define F_CPU 8000000UL
#include <avr/io.h>

#include <avr/interrupt.h>

#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

volatile uint8_t count;

ISR (TIMER0_OVF_vect)

{


unsigned long m = timer0_millis;
unsigned char f = timer0_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer0_fract = f;
timer0_millis = m;
timer0_overflow_count++;





}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;

return m;
}



int main (void)

{
DDRB |= (1<<0);
TCNT0 = 0;
count = 0;
TCCR0B |= (0<<CS02) | (1<<CS01) | (1<<CS00); // PRESCALER 64
TIMSK0 = (1<<TOIE0);
sei();

while(1)
{
millis() to uart.

}

}









share|improve this question




















  • 3




    cross post stackoverflow.com/questions/53780848/…
    – Juraj
    Dec 14 '18 at 14:42














1












1








1







This code It works in atmel studio.But this millis function is starting to run 31-34 miliseconds behind of arduino millis fuction.Why?I am adding picture.



Picture: https://imageshack.com/a/img922/3060/umtNJp.jpg



Update 2



#define F_CPU 8000000UL
#include <avr/io.h>

#include <avr/interrupt.h>

#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

volatile uint8_t count;

ISR (TIMER0_OVF_vect)

{


unsigned long m = timer0_millis;
unsigned char f = timer0_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer0_fract = f;
timer0_millis = m;
timer0_overflow_count++;





}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;

return m;
}



int main (void)

{
DDRB |= (1<<0);
TCNT0 = 0;
count = 0;
TCCR0B |= (0<<CS02) | (1<<CS01) | (1<<CS00); // PRESCALER 64
TIMSK0 = (1<<TOIE0);
sei();

while(1)
{
millis() to uart.

}

}









share|improve this question















This code It works in atmel studio.But this millis function is starting to run 31-34 miliseconds behind of arduino millis fuction.Why?I am adding picture.



Picture: https://imageshack.com/a/img922/3060/umtNJp.jpg



Update 2



#define F_CPU 8000000UL
#include <avr/io.h>

#include <avr/interrupt.h>

#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)
volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

volatile uint8_t count;

ISR (TIMER0_OVF_vect)

{


unsigned long m = timer0_millis;
unsigned char f = timer0_fract;

m += MILLIS_INC;
f += FRACT_INC;
if (f >= FRACT_MAX) {
f -= FRACT_MAX;
m += 1;
}

timer0_fract = f;
timer0_millis = m;
timer0_overflow_count++;





}

unsigned long millis()
{
unsigned long m;
uint8_t oldSREG = SREG;

// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;

return m;
}



int main (void)

{
DDRB |= (1<<0);
TCNT0 = 0;
count = 0;
TCCR0B |= (0<<CS02) | (1<<CS01) | (1<<CS00); // PRESCALER 64
TIMSK0 = (1<<TOIE0);
sei();

while(1)
{
millis() to uart.

}

}






arduino-uno arduino-ide atmel-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 '18 at 16:45

























asked Dec 14 '18 at 14:34









alex jla

62




62








  • 3




    cross post stackoverflow.com/questions/53780848/…
    – Juraj
    Dec 14 '18 at 14:42














  • 3




    cross post stackoverflow.com/questions/53780848/…
    – Juraj
    Dec 14 '18 at 14:42








3




3




cross post stackoverflow.com/questions/53780848/…
– Juraj
Dec 14 '18 at 14:42




cross post stackoverflow.com/questions/53780848/…
– Juraj
Dec 14 '18 at 14:42










1 Answer
1






active

oldest

votes


















4














The millis() function is defined in the Arduino Core for AVR architecture, specifically the wiring.c file.



You can see that timer0 is setup with various parameters for prescaling and interrupt triggering and handling of timer0 overflow such that a variable called timer0_millis contains the number of milliseconds since the sketch started.



The millis() function basically returns that timer0_millis value.



If you wish to make use of the function in AVR Studio, then you would need to include the same kind of timer0 setup and interrupt/overflow handling that the Arduino core does.






share|improve this answer





















  • I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
    – alex jla
    Dec 14 '18 at 14:58










  • I will not write it for you. It's already written in wiring.c
    – jose can u c
    Dec 14 '18 at 15:00










  • oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
    – alex jla
    Dec 14 '18 at 15:28








  • 3




    You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
    – jose can u c
    Dec 14 '18 at 15:33










  • Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
    – alex jla
    Dec 14 '18 at 17:35











Your Answer






StackExchange.ifUsing("editor", function () {
return StackExchange.using("schematics", function () {
StackExchange.schematics.init();
});
}, "cicuitlab");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "540"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58757%2fequivalent-of-millis-in-atmel-studio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














The millis() function is defined in the Arduino Core for AVR architecture, specifically the wiring.c file.



You can see that timer0 is setup with various parameters for prescaling and interrupt triggering and handling of timer0 overflow such that a variable called timer0_millis contains the number of milliseconds since the sketch started.



The millis() function basically returns that timer0_millis value.



If you wish to make use of the function in AVR Studio, then you would need to include the same kind of timer0 setup and interrupt/overflow handling that the Arduino core does.






share|improve this answer





















  • I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
    – alex jla
    Dec 14 '18 at 14:58










  • I will not write it for you. It's already written in wiring.c
    – jose can u c
    Dec 14 '18 at 15:00










  • oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
    – alex jla
    Dec 14 '18 at 15:28








  • 3




    You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
    – jose can u c
    Dec 14 '18 at 15:33










  • Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
    – alex jla
    Dec 14 '18 at 17:35
















4














The millis() function is defined in the Arduino Core for AVR architecture, specifically the wiring.c file.



You can see that timer0 is setup with various parameters for prescaling and interrupt triggering and handling of timer0 overflow such that a variable called timer0_millis contains the number of milliseconds since the sketch started.



The millis() function basically returns that timer0_millis value.



If you wish to make use of the function in AVR Studio, then you would need to include the same kind of timer0 setup and interrupt/overflow handling that the Arduino core does.






share|improve this answer





















  • I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
    – alex jla
    Dec 14 '18 at 14:58










  • I will not write it for you. It's already written in wiring.c
    – jose can u c
    Dec 14 '18 at 15:00










  • oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
    – alex jla
    Dec 14 '18 at 15:28








  • 3




    You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
    – jose can u c
    Dec 14 '18 at 15:33










  • Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
    – alex jla
    Dec 14 '18 at 17:35














4












4








4






The millis() function is defined in the Arduino Core for AVR architecture, specifically the wiring.c file.



You can see that timer0 is setup with various parameters for prescaling and interrupt triggering and handling of timer0 overflow such that a variable called timer0_millis contains the number of milliseconds since the sketch started.



The millis() function basically returns that timer0_millis value.



If you wish to make use of the function in AVR Studio, then you would need to include the same kind of timer0 setup and interrupt/overflow handling that the Arduino core does.






share|improve this answer












The millis() function is defined in the Arduino Core for AVR architecture, specifically the wiring.c file.



You can see that timer0 is setup with various parameters for prescaling and interrupt triggering and handling of timer0 overflow such that a variable called timer0_millis contains the number of milliseconds since the sketch started.



The millis() function basically returns that timer0_millis value.



If you wish to make use of the function in AVR Studio, then you would need to include the same kind of timer0 setup and interrupt/overflow handling that the Arduino core does.







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 14 '18 at 14:42









jose can u c

5,6022724




5,6022724












  • I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
    – alex jla
    Dec 14 '18 at 14:58










  • I will not write it for you. It's already written in wiring.c
    – jose can u c
    Dec 14 '18 at 15:00










  • oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
    – alex jla
    Dec 14 '18 at 15:28








  • 3




    You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
    – jose can u c
    Dec 14 '18 at 15:33










  • Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
    – alex jla
    Dec 14 '18 at 17:35


















  • I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
    – alex jla
    Dec 14 '18 at 14:58










  • I will not write it for you. It's already written in wiring.c
    – jose can u c
    Dec 14 '18 at 15:00










  • oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
    – alex jla
    Dec 14 '18 at 15:28








  • 3




    You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
    – jose can u c
    Dec 14 '18 at 15:33










  • Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
    – alex jla
    Dec 14 '18 at 17:35
















I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
– alex jla
Dec 14 '18 at 14:58




I have found example code,but isn't same.Can you write equivalent of code in above?for being example and my learning.Of course,If is this possible?Thanks.
– alex jla
Dec 14 '18 at 14:58












I will not write it for you. It's already written in wiring.c
– jose can u c
Dec 14 '18 at 15:00




I will not write it for you. It's already written in wiring.c
– jose can u c
Dec 14 '18 at 15:00












oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
– alex jla
Dec 14 '18 at 15:28






oh sorry,I hadn't seen wiring.c file.I have added to my project.But I am not using timer0_millis.Error: 'timer0_millis' undeclared (first use in this function). What can I add wiring.c to my project?
– alex jla
Dec 14 '18 at 15:28






3




3




You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
– jose can u c
Dec 14 '18 at 15:33




You will need to read all of wiring.c, understand it, and then implement it in your own project. Arduino makes very simple the things which are complex on "pain" AVR programming. What is the reason you choose to use AVR Studio?
– jose can u c
Dec 14 '18 at 15:33












Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
– alex jla
Dec 14 '18 at 17:35




Because I am learning atmel studio.But now,I need to this function.No need to make it so difficult.Just,I want to counter like millis() which has forward direction.I need solution.
– alex jla
Dec 14 '18 at 17:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Arduino Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f58757%2fequivalent-of-millis-in-atmel-studio%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy