I would like to write code that completely uses Timer0 (Atmega328) the way I want (i.e. specify my own ISRs and timer setup) but the Arduino environment uses this timer to implement various procedures: millis(), micros(), delay(), delayMicroseconds(), analogWrite(), tone(), and noTone(). Furthermore, the Arduino environment includes wiring.h and wiring.c in all sketches that sets up the timer ISRs, defines some of the procedures listed above, as well as a procedure, init(), which must be called before a user setup() routine is called (this call is hidden from the sketch but the environment handles it). So, is there a way I can tell the Arduino environment not to load any of timer0 routines.? Do I need to manually edit the Arduino.h file to comment out the include of wiring.h? Although the Arduino environment can make using these Atmega chips fairly easy, it can make things real hard for a user when they want to stray a bit outside of the endorsed environment when they really want full control of an on chip peripheral.
If you want full control of the hardware then perhaps you would be better off moving away from the Arduino runtime entirely, and just use the underlying AVR runtime. The alternative is to reverse engineer the Arduino runtime, understand how it's using those resources and then selectively disable or re-implement those features - that is potentially a lot of effort. Arduino makes it easy to get started, but it's by no means the only way to program a microcontroller and perhaps you've reached the point where you want to take the stabilisers off.
Just to add to PeterH's reply, check out WinAVR, http://winavr.sourceforge.net/ and also Atmel Studio, http://www.atmel.com/design-support/software-tools/default.aspx
Thanks Peter and Jack for your reply.
I don't think that moving away to a different environment should be necessary for what I what to do. When I learned the C language MANY years ago a library was implemented by means of #include files. E.g. if I was writing a program that performed a lot of string manipulation then I could '#include <string.h> ' in my code and it would compile with several functions pertaining to string manipulation. If I didn't want that functionality then I simply did not include that library and no extra code would be generated and I would not be able to access those functions -- pretty simple! The Arduino TIMER0 code and its timer related functions are contained in an include file called wiring.h and wiring.c so things are set up in a consistent C like manner -- Great! The problem is that the Arduino environment takes away my choice of whether I want to include these files or not. Apparently, in the spirit of 'friendliness' this imposition makes it much harder to NOT want that functionality. In principal, it should be fairly easy to restore a user choice in not only TIMER0 functionality but all on-chip peripheral functionality that the Arduino environment may touch as long it is done with #include files.
So I really want my question to be addressed to those people who may have more knowledge of the inner workings of the Arduino environment that may be able to suggest ways I can tell the environment to not automatically include certain files.
I can always try to trace the path of includes starting with Arduino.h and add directives in the files(s) that include wiring.h and skip over the include but I would do that only in last resort.
The problem is that the Arduino environment takes away my choice of whether I want to include these files or not.
Yes, it is.
Apparently, in the spirit of 'friendliness' this imposition makes it much harder to NOT want that functionality.
Because 99.9% of users do want millis() and delay() and Serial and other things to work.
In principal, it should be fairly easy to restore a user choice in not only TIMER0 functionality but all on-chip peripheral functionality that the Arduino environment may touch as long it is done with #include files.
In principal, maybe. In practice, not hardly.
So I really want my question to be addressed to those people who may have more knowledge of the inner workings of the Arduino environment that may be able to suggest ways I can tell the environment to not automatically include certain files.
You've already rejected the only reasonable alternative - not using the IDE.
I used Timer0 w/o problem, just not include any of the function you mention in orig. post. Than you define your settings calling hardware registers in the setup.
Any reason not to use Timer2, they pretty match same in functionality?
Thank you PaulS and Magician for your responses.
PaulS, I think you're right in that 99.9% of the people are probably satisfied with the Arduino environment the way it is and that I am part of the disgruntled 0.1%. However, I think it could be incredibly easy to satisfy just about everyone -- newbie through experts without moving to a different programming environment. When one opens a new sketch either by the drop down menu or Cntl-N in the IDE, one is presented with a new sketch template with empty setup() and loop() functions and we can simply start there and add code. To satisfy just about everyone, the Arduino developers could add #include lines to all the libraries that define the 'standard' programming environment in the sketch template as well (instead of hiding their inclusion somewhere else). Then, as you say, 99.9% of all users add their code just as they always had and they are just as happy -- they really don't see any difference in how they use the IDE (other than actually seeing what needs to be explicit for the code to compile instead of info that is hidden from them). For the remaining 0.1% (me included) are also just as happy -- if there is any functionality I DON'T want I can either delete or comment out the corresponding #include file. Am I missing something here? It seems like such an incredibly simple solution.
Magician, for my application I am using all the timers so I do not have the option of simply using a different one. Furthermore, although I can write code to reconfigure TIMER0 control registers, I get compiler errors if I try to change any of TIMER0 interrupt vectors(which I need to do). Even if I could, my compiled code size is bigger than necessay with Arduino code that I don't need. This might not matter if I am programming an AVR processor with a lot of flash but for programming an ATTiny it is of great concern.
@bobo, you are correct, what you propose is an incredibly simple solution, makes perfect sense to me and I would welcome it. The thing you are missing is that part of the demographic that Arduino is aimed at are rank beginners that don't necessarily understand computers or programming and likely haven't written a single line of code in their life. To them, at least initially, a bunch of #includes, although they can easily just be ignored, do add visual noise and I imagine that what the design team was going for was a very straightforward uncluttered environment. I can see the point, but I don't completely agree either. Another approach to the same end would be to have the IDE run in two modes, novice or expert, where the latter would behave as you describe. But that would represent added development and maintenance costs, and the argument would be that the experts can use the existing tools oriented towards experts.
Seriously, if you've appropriated all the timers to your own ends, it sounds more and more like a project that would be just as happy in WinAVR or Atmel Studio.
I've used Atmel Studio some, but not what I would call extensively. I am aware that there is an Arduino extension for it. I have not tried it, but I wonder if it wouldn't give some of the flexibility you seek.
Jack,
I understand and agree with your point up to a point. I do realize the demographics of the Arduino community consists of a lot of novices so the desire to make the IDE easy is an admirable one. However, it appears that it violates one of my cardinal rules: making something easy for beginners should never permanently thwart an advanced or expert user. As a beginner gains more experience and expertise they should be able to access and use advanced programming that accesses the full power of an AVR processor. Learning a programming language is not necessarily an easy task and it requires learning syntax and symantics. C language is simple
in one sense but can be quite complex in another with it's many nuances. So if the Arduino team thinks that learning C/C++ is appropriate for rank beginners then they should not cripple advanced programming techniques.
Of course, my cardinal rule may not be shared by either you or the rest of the Arduino community.
Don't want the "overhead" of the Arduino core? This is your starting sketch...
int main( void )
{
// Your code goes here
return( 0 );
}
Coding Badly,
I am so happy to see this code since I re-discovered it yesterday by myself. I've been wondering whether I should post this coding nugget but decided to wait until I experimented with other ideas. GREAT CONTRIBUTION!!
The Arduino runtime is not designed to allow features to be turned on and off and to share its runtime resources with non-Arduino code. Enhancing the Arduino runtime to allow that for arbitrary resources would be a lot of work. If you want to use resources that the Arduino runtime also uses, the only sensible option IMO is to stop using the Arduino runtime.
Defining main() in your sketch will bypass the arduino initialization, but I don't think it will let you define your own ISR for Timer0 and the other ISRs implemented by the core.
You can also simply re-initialize the timers after the arduino init() code is done with them, at which point they'll stop doing arduino things; there are several libraries for using the other timers. You still have the ISR problem.
The timer0 tick is pretty much the center of the Arduino runtime. If you don't want that, I don't understand why you want ANY of the Arduino core. (I guess I'm agreeing with PeterH)
PeterH:
The Arduino runtime is not designed to allow features to be turned on and off and to share its runtime resources with non-Arduino code. Enhancing the Arduino runtime to allow that for arbitrary resources would be a lot of work. If you want to use resources that the Arduino runtime also uses, the only sensible option IMO is to stop using the Arduino runtime.
+1
There are many things the Arduino team might do which would be more useful to the 99.1% than spending time on something that is outside the scope of the Arduino concept.
...R
westfw:
...I don't think it will let you define your own ISR for Timer0 and the other ISRs implemented by the core.
As long as there are no references to the functions in wiring.c it should work. Be back in a minute... Yup. Works fine. Yup. Calling millis causes a duplicate symbol.
+1 for PeterH (and westfw and Robin2).
'Coding Badly' is correct has has supplied the key to getting complete control of the AVR processor -- just start with the sketch template:
int main() {}
Starting here requires you to take on more responsibility to fill in all details that the Arduino environment takes care of for you but the extra complexity is actually quite minimal. As long as you don't reference anything in wiring.c (e.g. delay(), milis(), micros(), delayMicroseconds(), ... ) you can take over use of TIMER0 (and ALL other on chip peripherals) any way you want. You can even define your own ISR. In fact, the following is valid:
ISR( TIMER0_OVF_vect ) {}
int main() { }
Hi All,
I know this post is a little old, but I wanted to add that including the #includes should be the way this is done. I understand that the Arduino concept is all about getting beginners going as soon as possible, but this is pretty standard in C and it should be something that a beginner should start to get used to as soon as possible. In the beginning, the beginner will just ignore the "pesky" #includes, but as they get more proficient, they will understand what they do. Although in this case there is a workaround, making it so that the beginner is able to see what is under the covers, even if they don't at first (or ever), is part of the learning process, which is what the Arduino environment is all about. I certainly do not see the IDE as a "springboard" to AVR studio or anything like that. You can create many complex projects with the Arduino environment. It's such a simple fix for maintaining the #include build "philosophy" of C that it is a little perplexing it is not done this way.
By the way, I found this post because I am also using an attiny85 and needed both counters. And I find my Uno is excellent for prototyping before programming the tiny for the final product. I don't use any of the timing functions in my project so I assumed I could use timer0, but got the compile error. Thank goodness I found this post.
Regards,
Jose
I think they are taking out font tags.
And I have used them many many times. Makes my old posts look dorky.
I don't find the IDE gets in my way. You can always turn the timers off, and the suggested fix of using a main() of your own also works perfectly well.
See How to avoid the quirks of the IDE sketch file pre-preprocessing if you want to write in a more standard C / C++ way.