Thursday, December 16, 2010

Basic Theory on AVR Timer0

To start off with, the formulae relating duty cycle, frequency and time period is: f=1/T, D=t/T where D is duty cycle, T is time period, t is on time, f is frequency
Vo = D * Vin, where Vo = average voltage output from AVR pin, D = duty cycle, Vin = Supply Voltage, eg 5v
The output voltage here is in the form of pulses not a complete analog value.
Eg. When frequency is 100Hz, duty cycle is 50% and supply voltage to AVR is 5v, then output from AVR pin is 2.5v. However, this does not mean that the output will be 2.5v DC. The output will be a pulsating output with frequency = 100Hz, 50% on, 50% off, with high state or on state voltage = 5v, low state or off state voltage = 0v. Therefore output = 2.5v, but actually 5v with 50% on and 50% off. This CAN be converted to pure DC by placing an RC or LC filter, which will convert the pulsating DC to a clean fixed DC voltage.
I will refer to ATMEGA8 for example, as this seems to be one of the most popular AVRs.
There are 3 timers – Timer/Counter 0, Timer/Counter 1 and Timer/Counter 2. I will refer to Timer/Counter as TC, so TC0 means Timer/Counter 0.
TC0 and TC2 are 8-bit timers. This means that the timers will count from 0 to (2^8) – 1 = 255. TC2 has one associated compare module. TC1 is 16-bit, so it counts from 0 to (2^16)-1 = 65535. It has two compare modules attached and provides a rich set of PWM functions.
I’ll start off with TC0.
TC0 has 4 registers associated with it that we need to work with. The most important is Timer/Counter 0 Control Register – TCCR0.

 Take a look at the registers below:

The CS2, CS1 and CS0 bits determine the rate at which the value of the TC increments. Say we have a clock of 10MHz. Therefore we have a clock cycle = T = 1/F = 1/10MHz = 100ns. Prescaler extends this time by a specific amount. So, looking at TCCR0, if we write 0 to it, we have no clock source, this means the TC is stopped and will not increment. It retains its current value.
If we set CS[2…0] to 1, we have no prescaler, that means that the TC increments each 100ns, ie each clock cycle. If we assign prescaler 8, by writing 2 to CS[2…0], we extend the time at which TC increments by 8. With no prescaling it would take 1 clock cycle for each increment, therefore with a prescaler of 8, the time would be extended 8 times, ie, it would now take 8 clock cycles for each increment, ie 800 ns.
The same follows for prescaler values of 64, 256 and 1024.
Eg. With a clock cycle of 10MHz, prescaler = 1024, the time taken by TC0 for one increment is:
Time for 1 increment with no prescaling = 1 clock cycle = 1/10MHz = 100ns
Therefore, time for 1 increment with prescaler = 1024 = 1024 clock cycles = 1024*0.1us = 102.4 us
[us = microseconds, ns = nanoseconds]
I will not explain the external clock source as it is not required for PWM.

 Take a look at the registers below:

The second associated register is TCNT0 that holds the current value of TC0.
E.g. What will be the value in TCNT0 when CS[2…0] is cleared (=0) after a time of 10secs? Clock = 1MHz, prescaler = 8.
Each clock cycle = 1/1MHz = 1us [us = microsecond]
TC0 is incremented (if prescaler = 0) each clock cycle, ie each us(microsecond)
Therefore TC0 is incremented (when prescaler = 0) each 8 clock cycles, ie each 8 us(microsecond)
10 seconds = 10,000,000 microseconds
TC0 counts to a maximum of 255. That’s 255 increments. After this it overflows, ie, it rolls over from TOP [TOP means the maximum value to which TC0 can increment], in this case 255, to 0.
Each increment takes 8 us.
Therefore 256 increments (255 increments + 1 increment to overflow to 0) takes 256 * 8us = 2048us = 2.048ms [ms = milliseconds] = 0.002048 seconds
Therefore in 10 seconds, number of overflows = (10/0.002048) = 4882.8125
That’s 4882 overflows. There’s a remainder, 0.8125, which means a remainder of 0.8125*255 = 207.1875
Therefore 207 is stored in TC0.

Take a look at the registers below:

The third associated register is TIMSK – Timer/Counter Interrupt Mask Register. Here bit 0 is what is associated with TC0. TOIE0 – Timer/Counter Overflow Interrupt Enable 0. Setting this bit enables setting TC0 interrupt. I’ll go into details about this later.

Take a look at the registers below:

The last associated register is TIFR – Timer/Counter Interrupt Flag Register. Here bit 0 is what is associated with TC0. TOV0 = Timer/Counter Overflow Flag 0. This flag is set whenever the timer overflows (this has been shown in the above example). Writing one to this bit clears it.

There's more to come about the Timer PWM modes and Timer1 and Timer2 and all PWM modes.
Hope this is helpful to all.

Registers being referred to:


  1. there is no further information regarding timers/counters?


  2. hi dear its very nice and very helpful post for those who are new and thinking to start a blog site.Your posts are very helpful and creative clik here and see how to dc 24 volt to ac 220 volt instalation of inverter

  3. Australia is the largest country in Oceania and one of the major countries in the world. So naturally, Australia is the birthplace of some of the most prestigious colleges and universities globally. Thousands of students come to Australia from all around the world to study various subjects. Australia provides some of the best university assignment help through the excellent faculties and universities.
    Students prefer Australia to pursue their master's degrees or Post Doctorate degrees. Apart from the excellent teaching staff in universities, they have many options to do my homework as well. In addition, the country provides a beautiful landscape and is a gastronomic delight for the foodies. So, that is a lucrative proposition for many students who are serious about study help and making a career in this country.
    Curtin University
    Founded more than five decades ago, in 1966, Curtin University is located in the beautiful suburban setting of Perth. It is a non-profit public higher education university with multiple branch campuses. They have campuses in Northam, Sydney, Kalgoorlie, and even in Singapore. They have been officially recognised by the Department of Education and The University of Western Australia
    This is one of the oldest universities in Australia. Since its inception in 1911, The University of Western Australia is also a non-profit higher education institute located in Crawley. They have some of the best faculties among all the universities in Western Australia and are famous for providing immaculate do my essay to all their students.
    Murdoch University
    Murdoch is a tiny city in the heart of Western Australia. Murdoch University was founded in this very city back in the year 1975. They are also recognised by the Department of Education and Training of Australia and were always a coeducational university. They offer a bunch of courses and programs in several subject areas for instant assignment help. Students never have a problem finding the subject of their choice from the variety of options Murdoch University provides.
    Edith Cowan University
    This university is a relatively newer university and was founded in 1991. Edith Cowan University is situated in the small city of Joondalup in Western Australia. This is also a public university focusing on higher education and works on a non-profit model. Edith Cowan has two branches other than the main campus at Joondalup.
    The University of Notre Dame Australia
    UNDA or The University of Notre Dame Australia is the only private education institute on this list. Established in 1989, this university is situated in the chic urban backdrop of Fremantle in Western Australia. UNDA also has multiple branches in Sydney and Broome.
    We have a few more services: 101586 Assignment Answers| 104801B Assessment Answers| 11408 Task Answers| 11485 Solutions| 11641 Answers

  4. Hi sir,can you please
    give me an article on how to start writing code for microcontroller?