In this second step from scratch we modify the LED blinking so that it has a not regular timing, in practice I want a rapid series of two blink and a longer period of time off; the effect must be that of a light that indicates a presence. A behavior like the one in the figure.
There is an easy way to get this result, we need to go back to the configuration files, again using the MCC tool, and change the time period of the overflow of the timer0 from 1s downto 100ms. In this way we will get 5 turns each second. It will be straightforward to create a global variable that takes into account the number of blinks so that the LED switches only when the variable has a value between zero and three.
By pressing the "Generate" button you get the modification of the tmr0.c file that was previously generated when we set timer0 to 1s. Let's see what the changes are.
Portion of the tmr0.c file before the change:
Portion of the tmr0.c file after the change:
You can see that the two TMR0H and TMR0L variables associated with the TMR0 registry have changed. The latter is a 16-bit register whose value is the current value of the timer0 which is incremented at each rising edge of the selected clock source. The value of the TMR0 register is indirectly set by the "Requested period" field of the MCC tool seen above. How is it?
The program (MCC) calculates how many clock pulses are used to set the timer overflow over the required time and sets the registry accordingly; so if we set 1s, as we did in the first post, knowing that each rising edge of the clock arrives in 16us, we have to write in the registry a value of
65535 - 1s/16us = 65535 - 62500 = 3035 (0x0BDB)
that, unless a small approximation is the value entered in the old version of the file. I try to represent what we have done in the figure below.
Now you just have to modify the main loop in main.c:
The blinkCount variable is created and initialized to zero; every time the timer goes into overflow state, the value of the variable is incremented by one unit. The LED changes status only when the variable is between zero and three, in fact, it switches on twice, then shuts off until the variable arrived at nine, than the variable is resetted and the sequence start again.