1.4.1 Timer1 Gate
Modules | Limit High | Resolution |
---|---|---|
Timer1 Gate | 4.1 ms | 125 ns |
Timer1 |
Microcontrollers that have the gate feature of Timer1 can also measure the duty cycle of a waveform.
OVERVIEW
To calculate the duty cycle both the pulse width and period width need to be measured. The
pulse width measurement can be seen in detail in section Timer1 Gate. To calculate the period length, two measurements of the waveform must
occur. These measurements can be achieved by setting the TxGCON.GE
,
TxGCON.POL
, TxGCON.GSPM
, TxGCON.GTM
, and
TxGCON.GGO/DONE
bits as seen in Figure 1:
If interrupts are enabled, the end of the period will set the interrupt flag. After this measurement is saved, the procedure to measure a single pulse as seen earlier (see section Timer1 Gate) can be repeated. The duty cycle is then a ratio of the pulse width over period width.
SETUP
- Set up Timer1 Gate for Single-Pulse and toggle mode on rising/falling edge
- Choose an appropriate Timer1 clock source and prescale for the period
- Clear TMR1H and TMR1L
- Enable the module and set T1GGO bit
- Wait for two consecutive rising/falling edges
- TMR1GIF is set
- Save period width from TMR1H:L
- Clear TMR1GIF
- Set up Timer1 Gate for Single-Pulse (see section Timer1 Gate)
- Save pulse width measurement
- Divide the pulse width by the period to determine the duty cycle ratio
Timer1 Gate Period Setup and Operation Code
Timer1 Setup T1GCON = 0x10; // Enable Single-Pulse Mode (Step 1) T1CLK = 0x2; // Timer1 Clock is Fosc with 1:1 prescale (Step 2) T1GCONbits.GE = 1; // Enable Timer1 Gate T1GCONbits.GPOL = 1; // Enable Timer1 Gate Polarity T1GCONbits.GTM = 1; // Enable Timer1 Gate Toggle Mode Operation Code TMR1H = TMR1L = 0; // Clear TMR1H and TMR1H (Step 3) Timer1_StartSinglePulseAcquisition(); // (Step 4) while(!TMR1GIF); // Wait for TMR1GIF to set (Step 5 and Step 6) uint8_t period_width_low = TMR1L; // Save period low byte data (Step 7) uint16_t period_width_high = TMR1H; // Save period high byte data (Step 7) TMR1GIF = 0; // Clear Gate Flag (Step 8)
LIMITATIONS
This method requires two measurements to be made: the waveform's pulse and period. Both of these are done entirely inside of the module after software initiation. The uncertainty for each measurement is plus or minus one clock period.