Problems with timing

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello

 

My system - ATMega168pa, 16MHz, Arduino IDE 1.8.10, WIN 7 32 Bit

 

I have a (small) programme which toggles 3 leds (1 sek each) and sends data via USART (every 70ms). To compile it I use the Arduino IDE 1.8.10 (could use my AVRStudio4.18 too, of course). In my programme I use TIMER2_OV_vect and self defined USART routines - see attached Klaus_03.c (with the "int main()" routine and Klaus_02.c only the "void setup" and "void loop" routine.

 

Now there's the problem:

When that programme is written with an "int main()" - loop (like it'd be written for compilation with my AVRStudio4 and NOT for compilation within that Arduino IDE) and I compile it within the Arduino IDE then all is ok - the leds are toggling with 1 sek and the data are transmitted every 70 ms as defined.

 

When that programme is written with "void setup()" and "void loop()" instead of "int main()" (=> Arduino style) and it's compiled then within that Arduino IDE the leds toggle with a time delay of 3 (!) seks and the data are transmitted every 218ms - so it looks like there'd be either a modifcation of the Timer or the frequency or whatever.

And for the life of me - I can't see where that error might be.
The fuses are set EXT 0xF9, HIGH 0xDD, LOW 0xFF, LOCK 0xFF.

 

Your kind advice / help would be much apreciated as I'm completely clueless 

 

Question - could it be problem caused by TIMER2?

 

Thanks in advance

 

Hero_123

Attachment(s): 

This topic has a solution.
Last Edited: Mon. Feb 10, 2020 - 10:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,   Most of the code in the Arduino version (Klaus_02.c)  is missing.  

 

Does "sek" refer to 1.0 second, or is a programmer's abbreviation for 'seek'?   Your code labels make me feel like I'm working on 1958-era computer with 64 bytes of RAM memory.

 

Does this little test program work OK if you use the standardized, tested, and debugged Arduino libraries like Serial.h and one of the common UNO/Nano libraries used with Timer2?

-and-

If your little test program does work with standardized, tested, and debugged Arduino libraries like Serial.h, then why aren't you using standardized, tested, and debugged Arduino libraries like Serial.h to get your application up and running?

 

Last Edited: Sat. Feb 8, 2020 - 06:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Simonetta

 

Thank you for your reply although it didn't help me at all.

 

I do not want to use those libs like Serial.h etc.

In my post I clearly stated that I want to use my own USART and TIMER sw, which is already approved (tested with AVRStudio 4.18 and it also is working correct, when I use the "int main()" when compiling with the Arduino IDE.

And my question was why that odd timing behaviour.

Of course I know I could use these UNO libraries

 

I modified my small programme so that I can switch between "void setup()" & "void loop()" AND "int main()"; and here again:

if I use that Arduino typical "void setup()" and "void loop" I experience that stange timing behavior (see #1)

if I do NOT use that typical Arduino "void setup()" and "void loop()" BUT INSTEAD "int main()"  the software behaves like expected.

 

So attached is only one file where one can select between Arduino "setup()" & "loop()" OR "int main()" depending on the value of "#define AVRStud" and #define Ard_IDE"

 

I also tried to modify my comments to fit english.

 

Thanks again

 

regards 

 

Hero_123

 

Attachment(s): 

Last Edited: Sat. Feb 8, 2020 - 09:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My guess you have atomicity issues as you have shared variables between the timer isr and mainline code. Remember - the timer interrupt can hit at any point in your code.
I found the code very hard to follow.
As well, change the timer to CTC mode - you’ll get more consistent timing. Your method hails back to the 8051 days.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Kartman

 

Thank you for reply although it didn't help me at all

 

1.) Atomicity - ALL variables used in the ISR are

- uint8_t

- volatile

so that shouldn't be the cause of the issue - => when compiled AVRStudio style "int main()" - it works as intended.

 

2.) Changing TIMER2 to CTC mode - that also won't solve the issue

 

3.) with all due respect - " Your method hails back to the 8051 days. " you don't want to vilify me, do you?

 

The main point is

- when compiled with "int main()" (=> AVRStudio style) all is ok, the LEDs toggle with 1 second as intended

- when compiled with "void setup()" and "void loop()" the LEDs toggle with 3 seconds (which is wrong).

 

I've attached a new file "Klaus_04.c" with only TIMER2 and the LEDs, the behaviour is still the same - perhaps it's not that hard to follow the code (if you'll have a look at the code at all).

 

Another odd thing is - when compiled with "int main" the code size is smaller than when compiled with "void setup()" and "void loop()".

 

Hero_123

Attachment(s): 

Last Edited: Sun. Feb 9, 2020 - 10:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hero_123 wrote:

When that programme is written with an "int main()"...then all is ok...When that programme is written with "void setup()" and "void loop()" ...(=> Arduino style) and it's compiled then within that Arduino IDE the leds toggle with a time delay of 3 (!) seks and the data are transmitted every 218ms - so it looks like there'd be either a modifcation of the Timer or the frequency or whatever.

 

So let's just be clear...

 

'Arduino' style works OK, 'raw' C style doesn't?

 

How are you uploading your code to the chip? Bootloader? If a bootloader then how do you 'reset' the chip to a known state after the bootloader has run?

 

I would be inclined to rewrite your init_TIMER2 routine to not use '|=' so that you start with a known state. If you want to use '|=' then write 0x00 to the registers to start with.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

I would be inclined to rewrite your init_TIMER2 routine to not use '|=' so that you start with a known state. If you want to use '|=' then write 0x00 to the registers to start with.

 

Absolutely.

When using 'Arduino style', the hidden init function is probably configuring some of the Timer 2 registers for its own purpose, so you can't rely on these registers being at their power on reset value.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Brian Fairchild, hello MrKendo

 

Thanks a lot for your replies.

 

@Brian Fairchild:

Unfortunately it's the "Arduino style" which doesn't work as intended (toggling LEDs takes appr. 3 times longer than intended); "raw" (= "int main()" style) works as expected.

I always use the Arduino IDE to compile the sw - I use the switch "AVRStud" or "Ard_IDE" to compile either with "int main()" or "void setup()" and "void loop()".

I compile the sketch with the option "export compiled binary file" and I use the compiled file without bootloader, as I use an ISP programmer to load my programme onto my ATMega168pa.

So there's no bootloader involved.

 

And I did as you suggested - I write first 0x00 to the timer register TCCR2B, then "TCCR2B = 0x05" (which corresponds to a prescaler of 128 - will be 1 ms then) - but to no avail, if I compile the file with the "Ard_IDE" set (meaning compiling with Arduino options) - still the leds take appr 3 seconds to toggle (should be 1 second only).

If I compile it with the "AVRStud" option set (= "int main()") all is well, the leds toggle with 1 second as intended.

 

When I however set TCCR2B = 0x03 ( equals a prescaler of 32) and compile my sw with "Ard_IDE" set ("void setup" and "void loop() THEN the leds toggle with appr 1 second, but then the leds toggle 3 times faster when compiled with "AVRStud" option set (quite obvious as the prescaler is 32).

 

So what I did - as a temporary solution - when I compile with "Ard_IDE" option set I set TCCR2B = 0x03 only once in the "void loop()" and then it's - well - nearly ok.

Nevertheless I have no clue why that behaviour, and unfortunately I do not know what happens, when inside of "void setup()"  and "void loop()" likewise.

 

@MrKendo

What exactly happens INSIDE that "void setup()" function and inside "void loop()"? How and where can I find out which TIMER, USART etc are already used hidden inside the Arduino core?

 

Attached is again my file "Klaus_04.c" with that modification of setting TCCR2B in the "void loop()" only once.

 

My next tests will be implementing the USART - now let's see what happens then when compiled with"Ard_IDE" style set and when compiled with "AVRStud" style set - and I 'll not use that Serial - stuff but my own USART definitions which I've already successfully used in different other projects (but at that time  compiled with the AVRStudio 4.18)

 

regards

 

Hero_123

 

 

Attachment(s): 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hero_123 wrote:
And I did as you suggested - I write first 0x00 to the timer register TCCR2B

OK, but don't forget about TCCR2A.

Arduino init might change that as well.

I would explcitly  initialise ALL timer 2 registers to be safe.

 

I don't have arduino installation, but I have been looking at the code on github.

hidden main here

https://github.com/arduino/Ardui...

 

the init within the hidden main here

https://github.com/arduino/Ardui...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello MrKendo, hello Brian Fairchild

Thank you very much for your help yes!

 

That advice with initialising ALL timer2 registers did the trick! I now set the TCCR2A = 0x00 and TCCR2B = 0x00 in my TIMER2_init routine and additional  (only once) at the beginning of the "void loop()" (which is "Ard_IDE" style).

 

So now the LEDs toggle with 1 second no matter if compiled either "AVRSTudio" style (=> with "int main(void)" OR "Ard_IDE" style (=> with "void setup()" and "void loop()").

 

@MrKendo

 

Also many thanks for those two links (# 9) which are a great help (though I'm not familiar with c++; I normally programme in C with AVRStudio 4.18 which is sufficient enough for me right now).

the init within the hidden main here

https://github.com/arduino/Ardui...

 

I don't have arduino installation, but I have been looking at the code on github.

hidden main here

https://github.com/arduino/Ardui...

 

 

regards

 

Hero_123