Comments (4)
Even though the data sheet does not explicitly state that starting the timer with 0x0000 in TCNT1 will set the overflow flag if does state the overflow flag will be set when TCNT1 = 0 while counting. Apparently the overflow flag will be set ANY time TCNT1 contains zero and the counter is running, i.e. any of the clock source control bits (CS10, CS11, CS12) are set and mode 8 is used. It would be nice if the datasheet stated this.
I can see no way other than to start TCNT1 at 0x0001. The first timer interval will be short 1 clock cycle, all after will be correct. The overflow flag should also be cleared by writing a 1 into it (I know this sounds funny, blame Atmel).
Other mode count up only. This would half the maximum possible interval and quite possibly screw up the PWM functions.
from timerone.
Setting TCNT1 to 1 will not fix the problem. If the timer was previously counting down, setting the TCNT1 to 1 will count DOWN to 0 after 1 count - causing the TOV1 to be set after 1 timer tick. The data sheet does not specify how to guarantee the TCNT1 counts up when the timer is started. Simply setting the prescaler bits (thus starting the timer) doesn't do it.
from timerone.
This was a pretty big issue for me today. I wanted to get a simple one-shot timer interrupt working without digging too deep, but was stuck for a while because the first (and only) interrupt always happens immediately.
I came up with the same "TCNT1 = 1" solution as Swap-File, but yes, that has its own issues. Maybe a different counting configuration is needed.
from timerone.
I too am trying to "extend" the length of a timer by calling start()
from within the ISR. This issue is a real pain in the rear to deal with, especially for those (like me) who are just learning how to use timers. I've gone as far as manipulating the registers for timer operation directly and can repro the issue as I'm eventually setting TCNT1 = 0
from my "start" routine.
For reference, I looked at the implementation of the MsTimer2 library, which has a similar start/stop/init interface. This library will set the TCNT vector to the following expression in its start() method, which is generally never zero. Is it sensible to use the same technique for TCNT1 for Timer1?
tcnt2 = 256 - (int)((float)F_CPU * 0.001 / prescaler);
In other words:
TCNT1 = 65536 - (int)((float)F_CPU * 0.001 / prescaler);
The Timer
libraries do a great job abstracting the meticulous register and bit manipulation required for timer use and it would be great if we can fix this.
from timerone.
Related Issues (20)
- External input for the timer1 to generate an interrupt when the counter reaches the maximum HOT 4
- Errors when using TimerOne with new Arduino Nano Every HOT 3
- Inverted PWM HOT 1
- [Error:] Not Working for the Digispark with Attiny85 HOT 1
- is it possible to attach overload metheds to the timer
- TimerOne issue in combination with BMP085 Adafruit driver
- Lacking support for pwm pins 0 and 1 on ATTiny85
- probably flaw of start() HOT 2
- 100% Duty cycle is not completely 100% (see picture) HOT 6
- Timer will not delay for long periods
- Timer1 hangs delay() function. HOT 1
- First cycle after start() is half the specified interval on Teensy 3.6
- Two glitches at start of timer on Teensy 2.0++ HOT 1
- Is the TimerOne library not compatible with the GPL license?
- Support for ATtiny84/44/24
- Support for ATtiny13 HOT 1
- Compile Error with Updated A-Star 328PB Board Info HOT 6
- Feature Request
- New release? HOT 1
- Missing support for Atmega128 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from timerone.