Using timers TMR0, TMR1 and TMR2. Using interrupts, declaring new function...


If you have read the previous example, you probably have noticed a disadvantage of providing delays using loops.  In all those cases,  the microcontroller  is ‘captive’  and does nothing.  It simply waits for some time to pass. Such waste of time is an unacceptable  luxury and some other method should be applied therefore.

Do you remember the story about timers? Interrupts? This example makes links between them in a practical way. The schematic is still the same as well as the challenge. It is necessary to provide a delay long enough to notice changes on a port. Timer TMR0 with assigned prescaler is used for this purpose. An interrupt is generated on every timer register overflow and every interrupt  routine  automatically  increments  the cnt variable  by 1. When  it’s value  reaches
400, the port B is incremented by 1. The whole procedure is performed ‘behind the scenes’,

which enables the microcontroller to do something else.


Interrupt occurs on every timer register TMR0 overflow.


In this case, an interrupt is enabled after the timer register TMR1 (TMR1H, TMR1L) overflow

occurs. Combination of bits changing on port B is different from that in the previous example.


This    time,    an    interrupt    occurs    after    timer    register    TMR2    overflow    occurs. The Replace function, which normally doesn’t belong to C, is used in this example to invert port pins state.



0 komentar: