1.3.6 Polled Input
Modules | Limit High | Resolution |
---|---|---|
NONE | 4.194s | 2.5 μs |
This method can be used by any PIC device and is the most basic in terms of modules required (PORT).
OVERVIEW
The code to measure a single pulse width is similar to the IOC method with the exception that it will not have interrupt-on-change capabilities. Software must constantly loop while polling a single pin.
SETUP
Steps to measure the period:
- Configure one of the pins to be a digital input
- Set up any variable so that it will count
‘
T
’ (Figure 2) without overflowing - Increment a temporary variable
(
period_count
) when the pin transitions from its low-to-high state - When the pin transitions again from low-to-high, stop incrementing the count variable
- Period is
now:
Period = period_count
Polled Input Period Setup and Operation Code
Input Pin Setup (Step 1) TRISBbits.TRISB5 = HIGH; // Set RB5 as a digital input uint24_t period_count = 0; // Set up a variable to count the period (Step 2) while(PORTBbits.RB5 == HIGH){ // Increment period_count when input is high (Step 3) period_count = period_count + 1; } while(PORTBbits.RB5 == LOW){ // Increment period_count when input is low (Step 4) period_count = period_count + 1; } while(PORTBbits.RB5 == HIGH}; // Stop period_count increment(Step 4)
Steps to measure the pulse:
- Configure one of the pins to be a digital input
- Set up any variable so that it will count
‘
W
’ (Figure 2) without overflowing - Increment a temporary variable
(
pulse_count
) when the pin transitions from it low-to-high state - When the pin transitions from high-to-low, stop incrementing the count variable
- Pulse width is
now:
MeasuredPulse = pulse_count
Polled Input Pulse Setup and Operation Code
Input Pin Setup (Step 1) TRISBbits.TRISB5 = HIGH; // Set RB5 as a digital input uint24_t pulse_count = 0; // Set up a variable to count the pulse (Step 2) while(PORTBbits.RB5 == HIGH){ // increment pulse_count when input is high (Step 3) pulse_count = pulse_count + 1; } while(PORTBbits.RB5 == LOW}; // Stop pulse_count increment(Step 4) }
LIMITATIONS
The number of instructions that it takes to increment the register must be taken into account of the final pulse width. The most accurate way to determine this is to look at the disassembly or write the routine in assembly. The first falling edge may be impossible to capture if the pulse is small enough.