In the preceding recipe, we learned how to create a simple ISR that only does a counter increment. Let's make the interrupt routine do something more useful. In this recipe, we will learn how to program the 8051 microcontroller so that it generates a signal with a given frequency.
The 8051 microcontroller has two timers – Timer 0 and Timer 1 – both of which are configured using two special function registers: Timer Mode (TMOD) and Timer Control (TCON). The timer's values are stored in the TH0 and TL0 timer registers for Timer 0 and the TH1 and TL1 timer registers for Timer 1.
The TMOD and TCON bits have special meanings. The bits of the TMOD registers are defined as follows:
Bit | Timer | Name | Purpose |
0 | 0 | M0 | Timer mode selector – lower bit. |
1 | 0 | M1 | Timer mode selector... |