Saturday, November 23, 2013

All actions Micro - controller does Bardoaino including, scheduled according Far from dulcet


Let's put it on the table: the Hardoaino timers, designed to allow precise timing of actions, are an ugly business, and no wonder that many start trying to work with them (beyond, of course, copy the code from the network and a prayer that it works) discouraged very quickly. The timers are not built intuitively, and explanations you usually find on the net - not to mention the official technical specifications - are shocking esther boutique really. Tremendous amount of registers names strange bitwise operators, formulas ... what the hell is going on there?
This short series esther boutique of posts I will try to explain in an understandable way - it will not be easy, believe me - the subject timers Bardoaino (more precisely, micro - controllers ATmega328 type) and how to start using them. Therefore, the first question I will refer to it, and perhaps most important, is: Who needs it?
Let's talk a moment about the Hardoaino basic output through esther boutique the pins. There are two ways to "enable" esther boutique pin output Ardoaino first is to use the built-in functions and recognized pinMode and digitalWrite, and the other is directly modify registers (Registers) micro - controller. In terms of software code, registers behave almost exactly like a simple predefined variables, but in practice they do not just reside in memory micro - controller as ordinary variables, but are connected directly to the hardware, influence and are influenced her - her.
The first way to activate the output pin, with functions, much more simple and understandable esther boutique novice user - and much slower, about ten times at best. That is, for simple applications we prefer the easy solution and read, but when we will have something that works really esther boutique fast, accurate, smart or cost effective, we will have no choice but to start digging in registers. The same is true for timers. Simple applications we can schedule operations with functions like millis or delayMicroseconds and some tricks code, but when accuracy and efficiency critical, we'll manipulate the timers ourselves.
All actions Micro - controller does Bardoaino including, scheduled according Far from dulcet ", or rather a source of electric fluctuations speed. esther boutique This watch can internal or external: Bardoaino, it is customary to use an external crystal oscillator with a frequency of 16 MHz, ie 16 million vibrations per second. Basically, esther boutique with such oscillation, micro - controller performs a single atomic action - not in the sense of the atoms of the material, but simply the act of calculating the most basic kind: for example, esther boutique an addition or comparison.
Parallel with the calculation belonging to our code, micro - controller adds, with every movement of the clock, one register (one or more) called Timer. We can set an upper value of this register (up to the maximum limit is dictated by the number of bits in register), and every time the value reaches the upper register we defined, is reset and starts counting again. That is, suppose we have an 8-bit timer, we can in theory set the value of his top any number from 0 to 255. One can also define interrupt (interrupt) to the timer, so that whenever the timer is reset, the ruling will be activated and the code which will run automatically.
For example, say I set up my code a variable named A initialized to 0, the timer interrupt I create coding increases the A 1, and I set the upper limit of the timer is 255. Therefore, the timer is reset once every 256 clock oscillations, and assuming clock frequency is exactly 16MHz, a second later the value of A will reach -62 500.
Now, suppose I want the ruling runs less frequently - for example, instead of every 256 swings of the clock, I'd stopped run once every 2048 fluctuations, the number of which is of course too big for the limit eight bits of the timer. Can write basic code to count eight rulings only then will run the real thing, but it's a waste of resources and a better solution, called prescaler. The prescaler performs a kind of division of the original clock frequency, and the timer is actually based on that partition, instead of directly on the clock. If the prescaler is 1, then of course there is no difference, because one division does not change the frequency. However, if we define the prescaler esther boutique 8, then in the eyes of the timer, the frequency of the clock is divided by 8 and turn to 2 MHz. Timer is reset 7812.5 times a second (which is 62,500 parts 8) - just one 2048 original clock oscillations.
Interim conclusion, we have three layers: the base clock for crude off sitting the prescaler its Department "the frequency of the clock and reduces it, and the prescaler sitting timer comes and rises up to the limit defined for it, then reset and start the stopped (or running The ruling then reset - will not go into those details now).
Micro - controllers ATmega328 type, those who are most Uno models, Nano, Mini Pro and Duemilanove old, has three timers (numbered 0-2), and one (Watch dog) who works in a slightly different method and not talk about it right now. The timers 0 and 2 are actually changing the size of 8-bit each, as in the example above. Timer 1, however, is a 16-bit variable, which allows to determine the upper limit to the value of his 65,535.
Each of these timers have its own separate prescaler, but unfortunately, we can not give to prescaler what value we want: Actually, we are limited esther boutique in this passage incredibly small number of choices dictated by the severity of the micro - controller. Of course, we can also define separate award for each of timers.
Hardoaino's standard libraries esther boutique make extensive use of these three timers. Functions like delay, millis, tone, analogWrite or refused to activate functions rely on different timers, so once you start to work directly with timers, you can no longer rely on the familiar esther boutique functions. This is another reason for a headache for the beginner, but as mentioned, when the requirements of the project dictate precisely, this is a relatively small sacrifice compared to the benefits. esther boutique
HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b>

No comments:

Post a Comment