Advanced Microcontrollers: Using chipKIT Pro MX7

Project 0: MPLAB® X Integrated Development Environment

Using chipKIT™ Pro and MPLAB X

This project introduces you to the synthesis and analysis tools for producing microprocessor C code using the MPLAB® X integrated development environment (IDE) on the chipKIT™ Pro MX7 processor board.

8.16K
×
Project 1: chipKIT™ Pro and I/O Control

Digital Input and Output

The purpose of this project is to familiarize you with the methods of reading from and writing to the input and output (I/O) pins of the PIC™32 microcontroller.

6.54K
×
Project 2: chipKIT™ Pro and Delays

Software Timing Delays

The purpose of this project is to investigate methods of creating software time delays to pace processor operations.

4.31K
×
Project 3: Using chipKIT™ Pro with Stepper Motors

Software-Based Finite State Machines

The purpose of this project is to investigate the application of software-based state machines to controlling the speed, direction of rotation, and operational mode of stepper motors.

4.32K
×
Project 4: Using chipKIT™ Pro with Stepper Motors

PIC™32 Timers

The purpose of this project is to understand the operation of PIC™32 timers so that they can be used to implement a synchronized multi-rate periodic control system by polling the timer interrupt flag.

3.76K
×
Project 5: chipKIT™ Pro and Interrupts

Process Speed Controls Using Interrupts

Explore detecting events using interrupts or by using preemption that implements a nested interrupt management scheme.

5.44K
×
Project 6: Using chipKIT™ Pro to Control LCDs

Handshaking and LCD Control

Investigate concepts involving parallel communications and handshaking.

5.21K
×
Project 7: chipKIT™ Pro and Serial Communication

Asynchronous Serial Communications

Learn about asynchronous communications and communicate with a microcontroller using a terminal emulation program to implement a point to point serial link between the chipKIT™ PRO MX7 and a PC.

5.04K
×
Project 8: chipKIT™ Pro and Serial Communications

Synchronous I2C Serial Communications

Investigate concepts involving synchronous communications using a basic master-slave multi-drop network communications and use the I2C protocol to communicate with the 24LC256 I2C™ CMOS Serial EEPROM.

5.17K
×
Project 8a: chipKIT™ Pro and Serial Communications

Synchronous SPI Serial Communications

Investigate synchronous communications with the SPI master-slave serial bus

3.89K
×
Project 9: Controlling a DC Motor

PWM Using Timer Compare

Generate a proportional output using the output compare resource on the PIC®32MX processor to implement digital-to-analog conversion with pulse width modulation (PWM), thus controlling the speed of a DC motor.

6.52K
×
Project 10: Using chipKIT™ Pro and DC Motors

Frequency Measurement Using Input Capture

Use the PIC™ 32 input capture to measure frequency to determine the speed of a DC motor.

4.38K
×

Project 10: Using chipKIT™ Pro and DC Motors

Frequency Measurement Using Input Capture

Using MPLAB® X Integrated Development Environment and chipKIT™Pro

Project 10: Frequency Measurement Using Input Capture

Introduction

The purpose of this project is to use the PIC™32 input capture to measure frequency. Using frequency measurement will allow us to determine the speed of a DC motor. This project uses a period measurement to determine frequency. Project 9 should be completed prior to completing Project 10.

Before you begin, you should have:

Analog-to-Digital Conversion

Project 9 investigated methods of using a microprocessor to generate a variable amplitude output. In Project 10, we will look at one method of using a microprocessor to measure a variable-valued input. The PIC32 processor has 16 10-bit analog inputs channels. However, we will use the DC motor tachometer to measure the analog speed value. Considering that the motor speed is controlled by the average applied voltage, the tachometer represents a voltage to frequency converter. Various silicon manufacturers offer electronic devices that are used to generate a pulse waveform whose frequency is a function of a voltage applied to the sensor input. Using the frequency of a digital pulse wave to represent an analog signal has the advantage of higher signal-to-noise ratio and reduces sensitivity to circuit impedance.

Project 9 implemented open loop speed control of a DC motor. This control approach uses the assumption that the speed of the motor is proportional to the applied voltage. Project 10 is designed to test the linearity of open loop speed control by providing a tachometer to measure the rotor shaft speed. The hardware requirements for Project 9 are the same as for Project 10. Details concerning the hardware configuration for this project are provided in the related material in the tabs to the right. The DC motor used on the Digilent chipKIT Pro MX7 processor board pictured in the green tab has two Hall effect sensors that provide a pulse each time the DC motor shaft makes a complete revolution. The two sensors are physically oriented around the motor rotor shaft that are rotated by 90 degrees that generate two digital signals that oscillate at the same frequency but are out of phase by 90 degrees. The SA and SB PmodHB5 Hall effect sensor outputs form a quadrature encoder that allows both the speed and direction of rotation to be measured. We are only concerned with measuring the motor speed in Project 10.

The speed of the geared output shaft is reduced by the ratio of 53 to 1 to provide greater mechanical torque. The speed of the motor will be measured by determining the frequency of the digital pulse wave generated by the Hall Effect sensor.

Fundamentals of Frequency Measurement

As with the measurement of any data, we are concerned with three parameters: accuracy, resolution, and precision. The accuracy of a frequency measurement is dictated, to a large extent, by the accuracy of the reference crystal, or oscillator. For this project, we depend on the accuracy of the oscillator populated on the chipKIT Pro MX7 processor board. The measurement precision is a metric applied to the repeatability of a measurement. Resolution is the degree of fineness or, conversely, the granularity of a measurement. Accuracy is inherent in the reference that the measurement is compared to. Measurement precision and resolution are determined by the processing of the measured data. The subject of frequency measurement resolution is of great interest and is addressed in greater detail in the following paragraphs.

There are two methods for measuring the frequency of the tachometer. The number of pulses can be counted per unit of time or the inverse of the period between two successive pulse transitions can be computed. Measuring frequency by counting pulses over short intervals is more accurate for signals when the frequency of the signal being measured is high relative to the observation interval. This results in many hundreds or even thousands of cycles being counted during the measurement interval, thus providing a high frequency measurement resolution.

Measuring the period is generally more accurate for relatively low frequency signals. The high resolution is provided by the number of timer counts between input signal transitions. The measurement period now depends of the frequency of the input signal and, for very low frequency signals, may result in excessively long delays between measurement updates.

There are methods to mitigate the effects of long measurement periods for low frequency signals. One method is to use frequency multiplication with a phased locked loop (PLL) circuit. Most modern microcontrollers and microprocessors use frequency multiplication so that the core speed of the processor that is many times the processor crystal frequency. The PIC32 processor family uses a phase locked loop multiplier to set the core frequency by a parameter in the config_bits.h file. Another method for handling the measurement of signals with long periods is to use clocks that run at very low count rates.

For Project 10, you will measure the speed of the motor rotor shaft by computing the period between two successive pulses of the motor tachometer by capturing a timer count at each transition, which are not necessarily equally spaced. In order to achieve the measurement with relatively good accuracy, the period must be measured with a high resolution. This requires that the timer be counting at a much higher rate than the period of the of the tachometer signal. The accuracy of the frequency is preserved by inverting the period using floating point variables or fixed point math divide algorithms.

The challenge is to select the timer input clock rate that will provide adequate period accuracy across the range of possible tachometer frequencies. Initially, assume that the capture is set up to capture each positive or negative transition. Doing so will eliminate issues associated with the tachometer not producing a perfect square wave. This will, however, reduce the period sampling rate by approximately 50%, as described above. Starting with a peripheral bus clock (PBCLK) rate of 10 MHz, a period register to divide by 65536 by setting the PR register to 0xFFFF, and a timer pre-scale value of 256 will result in a 1 Hz/timer count granularity for a tachometer frequency of 200 Hz. Since the maximum period is 65536 timer counts, the minimum measurable frequency is 0.596 Hz but will require 1.68 seconds to get a measurement update.

Granularity is a relative term that relates to the coarseness of a measurement. Higher granularity means lower resolution. From the derivation shown in the Period Measurment tab at the top of the page, for a given measurement period, the granularity of any input frequency can be determined using the equation:

\[\Delta F = \frac{{T{3_{CLK\_PD}}}}{{{T_{SIGNAL}}}} \bullet {F_{SIGNAL}} = T{3_{CLK\_PD}}{F_{SIGNAL}}^2\]

Where $F_{\mathrm{SIGNAL}} = $the inverse of the period of the signal being measured$ = \frac{1}{T_{\mathrm{SIGNAL}}}$ and $T{3_{CLK\_PD}}\ = $the Timer 3 clock period$ = \frac{T3_{\mathrm{PRESCALE}}}{\mathrm{PBCLK}}$.

Eq. 1 above shows that, for a fixed timer clock frequency, the granularity of the frequency measurement, ΔF, increases as the square of the input frequency and has dimension of Hz. As Fig. 1 illustrates, a higher input frequency will result in a measurement with higher granularity for a given measurement interval. Better resolution (finer granularity) is achieved by increasing the clock frequency or reducing input frequency.

Figure 1. Plot of frequency measurement resolution for a fixed clock frequency.

Measuring a signal frequency by computing the inverse of the signal period will generate a new measurement update at the signal frequency. Provided that the input signal has a 50% duty cycle, we can get two frequency measurements per cycle of the input signal by determining the period when the input is high and the period when it is low. Measurement noise can be reduced by averaging multiple period measurements.

The speed of the motor is proportional to the frequency of the signal generated by the motor tachometer. The motor that we are using has a reduction gear with a ratio of 53:1. Using a 10V motor supply, the maximum motor speed will be approximately 525 revolutions per second (RPS), or 594 RPM on the output of the gear head. Due to motor power limitations and gear head friction, the minimum motor speed is approximately 79.5 RPS, which is equivalent to 90 RPM as measured on the gear-head output shaft.

Timer Setup for PWM and Input Capture

For this project, we will use two PIC32 timers: one for generating the PWM output that controls the motor speed (Timer 2), and one that will be used to capture the time when the tachometer signal transitions from low to high or high to low (Timer 3). The reason for using two timers is that the period register of the two timers are set to different values in order to achieve different design objectives. The period register of the timer used for generating the PWM signal is set to give a particular PWM cycle frequency. The period register of the timer used for measuring the period of the tachometer signal is set to the maximum value to allow the period measurement of an input signal over a wider range of frequencies. The lower the frequency on the input signal, the more timer counts that will be measured. The period measurement will be incorrect if the timer resets after reaching the terminal count more than once between input signal transitions. Hence, the period register for the timer that is used for timer capture will be set to its maximum value, 65535.

Using unsigned short int (16-bit) data types for variables used to capture the timer counts and to compute the period measurement results in correct period measurements even when the two successive timer captures are made when the timer rolls over from 0xFFFF (65535 base 10) to 0. Consider the case when the first captured timer value is 0XFE3E or the decimal equivalent of 65086. Let the second captured timer value is 0x0096 or the equivalent decimal value of 150. The result of 0x96—0xFE3E is 0xFFFF0258. Since we are using 16-bit data types, the higher 4-byte word is dropped leaving only 0x0258 which is equivalent to 600 – the true period in timer counts. A more thorough discussion of time interval issues is provided in the section titled, “Handling Timer Rollover for Hardware Assisted Delays,” in Project 2.

PIC32 Software for Timer 2 Setup for PWM and Timer Interrupt

Timer 2 will serve two functions: it will be used to generate the PWM signal and it will generate an interrupt once each millisecond. The one millisecond interrupt will be used for hardware timing. Configure. Timer 2 for operations as follows:

  1. Configure Timer 2 from a 1 ms period.
    1. OpenTimer2(T2config_bits, PR2_value); where (PR2_value + 1) is the period register value computed for the PWM cycle frequency.
      1. T2config_bits = (T2_ON | T2_Ps_1_1 | T2SOURCE_INT)|(T2_ON | T2_Ps_1_1 | T2SOURCE_INT)|(T2_ON | T2_Ps_1_1 | T2SOURCE_INT)
      2. PR2 = (10000-1) for PBCLK = 10MHz
  2. Configure Output Compare for 1 ms PWM cycle period.
    1. OpenOCx(OC_configure_bits, nOCxRS, nOCxR); where nOCxRS is the initial value written to the OCxRS register and nOCxR is the initial value written to the OCxR register. “x” designates the specific OC register being configured in the range between 1 and 5.
      1. OC_configure_bits (b1 | b2 | b3 | b4)|OC_configure_bits (b1 | b2 | b3 | b4)|OC_configure_bits (b1 | b2 | b3 | b4)|OC_configure_bits (b1 | b2 | b3 | b4)
        1. b1 = OC_ON //Enables the Output compare processor resource
        2. b2 = OC_TIMER_MODE16 // Timer uses 16 bit mode
        3. b3 = OC_TIMER2_SRC // Selects Timer 2 as input timer
        4. b4 = OC_PWM_FAULT_PIN_DISABEL // Don’t use fault pin
      2. Output Compare Registers
        1. nOCxRS — This is a constant or a variable that specifies the initial PWM compare value
        2. nOCxR — This is a constant or a variable that specifies the next PWM compare value
    2. Example: OpenOC3(OC_configure_bits, nOC3RS, nOC3R);.
  3. Timer 2 interrupt using peripheral library macro functions.
    1. mT2SetIntPriority( 2); // Set Timer 2 interrupt group priority level 1
    2. mT2SetIntSubPriority( 1); // Set Timer 2 interrupt subgroup priority 1
    3. mT2IntEnable( 1); // Enable T2 interrupts
    4. Alternate using the C32 Peripheral Library function:

      ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2 | T2_INT_SUB_PRIOR_1);|ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2 | T2_INT_SUB_PRIOR_1);|ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2 | T2_INT_SUB_PRIOR_1);

  4. Timer 2 ISR code.
    void __ISR( _TIMER_2_VECTOR, ipl2) T2Interrupt(void);
    {
    LATBINV = LEDA;	// Toggle  LED A – instrumentation
    mT2ClearIntFlag();	// Clear Timer 2 interrupt flag
    // INTClearFlag(INT_T2);	// Alternate peripheral library function
    }
    

PIC32 Software Timer Setup for Input Capture

Timer 3 Interrupt Setup

Timer 3 will be used exclusively for a 16-bit input timer capture.

  1. Configure Timer 3 to use the PBCLK and a pre-scale value of 256. Set Timer 3 PR3 register to 0xFFFF for maximum interval.
    1. OpenTimer3(T3_ON | T3_PS_1_256 | T3_SOURCE_INT, 0xFFFF);|OpenTimer3(T3_ON | T3_PS_1_256 | T3_SOURCE_INT, 0xFFFF);|OpenTimer3(T3_ON | T3_PS_1_256 | T3_SOURCE_INT, 0xFFFF);
    2. mT3SetIntPriority( 2); // Set Timer 3 interrupt group priority 2
    3. mT3SetIntSubPriority( 2); // Set Timer 3 interrupt subgroup priority 2
    4. mT3IntEnable( 1); // Enable Timer 3 interrupts
    5. Alternate to steps b through d using the C32 Peripheral Library function:

      ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_2 | T3_INT_SUB_PRIOR_2);|ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_2 | T3_INT_SUB_PRIOR_2);|ConfigIntTimer3(T3_INT_ON | T3_INT_PRIOR_2 | T3_INT_SUB_PRIOR_2);

  2. Timer 3 ISR code.
    void __ISR( _TIMER_3_VECTOR, ipl2) T3Interrupt(void)
    {
    LATBINV = LEDC;	// Toggle  LEDC  - Timing instrumentation
    mT3ClearIntFlag();	// Clear Timer 3 interrupt flag – 
    // INTClearFlag(INT_T3);	// Alternate peripheral library function
    }
    

Timer Capture Peripheral

Input capture is a processor peripheral that is used to associate at relative time with the occurrence of an external event by capturing the value of a processor timer. Relating the difference between successive captured timer values to the rate at which the timer increments allows the determination of the period between successive events or, inversely, the rate of event occurrences. Common applications of the input capture peripheral are for tachometers and frequency meters. This project measures the speed of the DC motor rotation by measuring the period of a rectangular wave produced by the Hall effect devices installed in the DC motor. Since the Hall effect outputs do not produce a perfect square wave, the period of the signal will be determined by the time between two successive signal rising edges.

The PIC32 uses up to 5 dedicated pins (IC1 through IC5) to trigger a timer capture operation. Either Timer 2 or Timer 3 can serve as a 16-bit timer or Timer2 and Timer 3 can be used together to operate as a 32-bit timer. Section 15.5 of PIC32 user’s manual for the input capture lists the options for configuring the event capture modes. The modes that allow single events to initiate a timer capture can be set for rising, falling, or both rising and falling edges of the input signal. Timer capture operations can also be initiated after a succession of either 4 or 16 events.

Interrupts can be generated after 1, 2, 3 or 4 capture events. A four-deep first-in first-out (FIFO) buffer allows the captured times associated with the succession of events. A capture event is defined by the writing of a timer value into the FIFO. As events are recorded into the FIFO buffer, a counter records the number of entrees. The number of capture events required to set an IC interrupt flag that triggers an interrupt event is set by the OpenCapture configuration. When all of the events have been read from the FIFO buffer, the capture count is cleared. Input capture interrupts persist so long as the condition that caused them persists. In addition, the interrupt will occur again immediately if the condition is not cleared by reading all of the captured events out of the FIFO buffer. It is highly recommended that the interrupt capture flag be reset at the conclusion of the input capture ISR.

Timer Capture Initialization

The following steps will allow the input capture to be setup to use Timer 3 and generate an interrupt when the timer value is captured by a rising edge of the IC5 input pin.

  1. Set motor Hall effect outputs SA (RD4) and SB (RD12IC5) as inputs. MTR_SA is set as an input but is not used for this application because RD4 is not a designated capture input.
    1. mPORTDDirection(MTR_SA_bit | MTR_SB_bit);|mPORTDDirection(MTR_SA_bit | MTR_SB_bit);
  2. Clear pending input capture interrupts.
    1. mIC5ClearIntFlag();
  3. Use input capture channel 5 for the following options. An interrupt will be generated on each positive transition transition of the input signal. void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);|void OpenCapture5(c1 | c2 | c3 | c4 | c5 | c6 | c7);
    1. c1 = IC_ON // Enable input capture
    2. c2 = IC_CAP_16BIT // Capture a 16 bit clock
    3. c3 = IC_IDLE_STOP // Stop input capture during debug
    4. c4 = IC_FEDGE_FALL // Initial capture on negative transition
    5. c5 = IC_TIMER3_SRC //Use Timer 3 as time to capture
    6. c6 = IC_INT_1CAPTURE // Generate interrupt on each capture
    7. c7 = IC_EVERY_ RISE_EDGE // Capture time on every edge
  4. c7 = IC_EVERY_ RISE_EDGE // Capture time on every edge
    1. ConfigIntCapture5(ic1 | ic2 | ic3);|ConfigIntCapture5(ic1 | ic2 | ic3);|ConfigIntCapture5(ic1 | ic2 | ic3);
      1. ic1 = IC_INT_ON // Enable input capture interrupt )
      2. ic2 = IC_INT_PRIOR_3 // Set priority for level 3
      3. ic3 = IC_INT_SUB_PRIOR_0 // Set sub priority for level 0
    2. Example:

      ConfigIntCapture5(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_0);|ConfigIntCapture5(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_0);|ConfigIntCapture5(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_0);

Input Capture ISR Example

   
"void __ISR( _INPUT_CAPTURE_5_VECTOR, ipl3) Capture5(void)"
{
static unsigned int con_buf[32];	// Declare an input capture buffer
// Declare three time capture variables: 
static unsigned short int time1;	// Most recent captured time
static unsigned short int time2 = 0;	// Previous time captured
static unsigned short int time_diff = 0; // Time between captures

LATVINV = LEDD	//Toggles LEDD on each input capture interrupt - instrumentation
ReadCapture5(con_buf);		// Read captures into buffer
time1 = con_buf[0];			// Save time of event
time_diff = time1-time2;		// Subtracts previous capture time to
					// determine period.
time 2 = time1;			// Save time1 as time 2
// Compute motor speed in RPS (revolutions per second). Equation to be
// worked out by reader
mIC5ClearIntFlag();			// Clears interrupt flag
// INTClearFLag(INT_IC5);		// Alternate peripheral library function

}

Project Tasks

The objective for this project is to implement a motor speed tachometer by measuring the frequency of the motor shaft sensor.

  1. Develop code that generates the PWM signal.
    1. Initialized the PWM duty cycle to 33%
    2. Toggle LEDA each Timer 2 interrupt
  2. Develop code that detects button operations.
    1. Set LEDB at the start of a CN ISR and clear LEDB at the end of the CN ISR
  3. Develop code that sets up Timer 3 for timer capture.
    1. Toggle LEDC each Timer 3 interrupt
    2. Toggle LEDD each input capture interrupt
  4. Write and verify the C code to complete the button control and LCD display design specified below.
  5. Connect the logic analyzer probes as follows to demonstrate that the PWM output and Timer 2 interrupts continue to function while the CN interrupt is being served.
    1. Channel 0: LEDA — Measures Timer 2 interrupt timing for PWM cycle period
    2. Channel 1: LEDB — Measures the ISR duration that detects a button press
    3. Channel 2: LEDC — Measures Timer 3 rollover period timing
    4. Channel 3: LEDD — Measures the Input capture event timing
    5. Channel 4: PmodHB5_EN — PWM output signal
    6. Channel 5: PmodHB5_SA — Motor tachometer Phase A
    7. Channel 6: PmodHB5_SB — Motor tachometer Phase B
  6. Capture the logic analyzer screen for the four PWM duty cycle settings specified in Table 1. (See item three under project testing.)

Table 1. Button controlled PWM duty cycle.

BTN2 BTN1 PWM
OFF OFF 40%
OFF ON 65%
ON OFF 80%
ON ON 95%

Project Specifications

Specifications in red italic font are additions to the Project 9 specifications.

  1. Operating Requirements:
    • Foreground operations (operations implemented from ISR):
      • Toggles LEDA in the Timer 2 ISR at the 500 Hz rate (LEDA is toggled at one half of the PWM cycle frequency)
      • Detect button uses the change-notice interrupt similar to the Project 5 implementation.
        1. The CN interrupt is to use a 20 ms software delay for switch debounce
        2. LEDB is turned on for the duration of the CN ISR.
        3. Sets the PWM output as a function of the states of buttons BTN1 and BTN2 as specified in Table 1
      • Toggles LEDC for each Timer 3 interrupt (1.19/2 Hz) (the maximum time before a Timer 3 roll-over)
      • Toggles LEDD each time the input capture ISR is serviced (the speed of the DC motor)
      • Writes the PWM percent duty cycle to line 1 of the LCD whenever the buttons change the %PWM (Hint: Generate a function the positions the LCD cursor to a fixed position. Then write a fixed length character string to update the element of the display.)
    • Background operation:
      • Updates the motor speed measurement (RPM) on line 2 of the LCD display at the rate of once each 100 ms, as determined by the software delay function DelayMs that can be preempted
      • Clear only line 2 of the LCD and display the measured revolutions per second as RPS = xxx.xx
      • The LCD updates must be protected from CN interrupts

  2. Software Organization:
    • main
      • Calls application initialization function that completes the following:
        1. chipKIT Pro MX7 processor board configured inputs for BTN1 and BTN2
        2. chipKIT Pro MX7 processor board configured outputs for PmodSTEP LEDA through LEDD
        3. Timer 2 to generate an interrupt each millisecond.
        4. The PWM output channel as follows:
          • Use output compare 3 (OC3)
          • Uses Timer 2 for the time base for the output compare
          • PWM cycle frequency of 1000 Hz
        5. Timer 3 for divide by 256 prescale (T3PS = 255 or 0xFF0) and a period of 65536 (PR3 = 65535 or 0xFFFF).
        6. Input Capture (See Input Capture ISR Example)
          • Use Input Capture 5
          • Trigger on each transition of the input pin IC5 (RD12)
          • Use a 16-bit timer
          • Use Timer 3 as time reference
        7. Initialize a CN interrupt for button status detection at priority 1 and the sub priority level 0.
        8. LCD initialization with a function added to position the LCD cursor at any position. (Refer to Project 6 for LCD programming.)
      • Executes a while(1) loop (background operations)
        1. Updates the motor speed measurement (RPM) on line 2 of the LCD display at the rate of once each 100 ms, as determined by the software delay function DelayMs that can be preempted
        2. Clear only line 2 of the LCD and display the measured revolutions per second as RPS = xxx.xx
        3. The LCD updates must be protected from CN interrupts
    • Button Detect ISR (refer to Project 5).
      • Sets LEDB on entry and clears LEDB on exit.
      • Removes button contact bounce with 20 ms software delay.
      • Reads button state.
      • Decodes buttons and sets PWM in accordance with Table 1.
      • Sets the motor PWM.
      • Updates only line 1 of the LCD reporting the percent PWM duty cylce using the format: PWM = ##%.
      • Clears CN interrupt flag.
    • Timer2 ISR.
      • Toggles LEDA.
      • Clears T2 interrupt flag.
    • Timer 3 interrupt
      • Set timer 3 for a 0.59 Hz interrupt rate. (Source = PBCLK, T3PS = 256, PR3 = 0xFFFF)
      • Toggle LEDC
    • Input capture ISR
      • Clears input compare interrupt flag
      • Toggles LEDD
      • Computes the amount of time since the last input capture ISR
      • Computes motor speed—RPS (global variable)

Project Testing

The DC motor control block diagram is shown in orange tab on the right, The Hall Effect device output labeled SB is connected to the IC5 / RD12 pin. Consequently, we will be configuring the PIC32 to use input capture channel 5. Refer to the OpenCapture topic in the C32 peripheral library for the setting of the input capture module.

  1. Connect the test equipment to the chipKIT Pro MX7 processor board using a logic analyzer as follows:
    • Connect the logic analyzer CH 0 through CH 3 probes to the test points for LEDA through LEDD on the PmodSTEP.
    • Connect logic analyzer CH 4 probe to the test point for the PWM EN pin on the PmodHB5.
    • Connect logic analyzer probes for CH 5 and 6 to the test points for the SA and SB pins on the PmodHB5.
  2. Run the Project 10 program to record the measurements indicated in Table 2 for the case when the PWM is set to 65%.

    Table 2. PWM measurements.

    Instrumentation TP Function Measurement — Hz
    PmodSTEP LEDA PWM cycle frequency (Timer2) 500 Hz
    PmodSTEP LEDB Period of CN interrupt 1.67 ms
    PmodSTEP LEDC Input capture timer frequency (Timer3) 0.2980 Hz
    PmodSTEP LEDD Input capture frequency 20 ms
    PmodH5 EN PWM waveform 500 Hz
    PmodH5 SA Tachometer frequency 439 Hz
    PmodH5 SB Tachometer frequency 439 Hz
  3. Provide a screen capture of the seven signals instrumented to complete Table 2. An example plot is shown in Fig. 2.

    Figure 2. Screen capture of the seven monitored signals when BTN is pressed to change the duty cycle from 33% to 65%.

    Screenshot of Digilent WaveForms running on Microsoft Windows 7.

  4. Complete Table 3 and graph the frequency measured at the PmodHB5 SB pin versus the percent PWM duty cycle specified in Table 1. A sample plot of this data is provided in Fig. 3.

    Table 3. Motor tachometer frequency for button controlled PWM duty cycle.

    BTN2 BTN1 PWM SB—Hz
    OFF OFF 33% 249
    OFF ON 65% 426
    ON OFF 80% 467
    ON ON 95% 499
    Figure 3. Sample Plot of DC motor speed as a function of PWM duty cycle.

Try It on Your Own!

  1. Explain why the SB signal is at the same frequency as the LEDD signal.
    • ANSWER: As shown in Fig. 2, LEDC toggles for each transition of the SA signal.
  2. What are the limitations to using the input capture for measuring frequency?
    • ANSWER: The range of measured frequencies is constrained by the timer clock frequency. This method is best for low frequency signals—most likely less than 10 kHz.
  3. Referring to the bottom tab on the right, what is the effect of changing the timer pre-scale value on the frequency measuring resolution in COUNTS/Hz at a given signal frequency assuming that the PBCLK remains constant?
    • ANSWER: The higher the prescale value, the lower the T3 clock frequency resulting in higher granularity. Lower prescale values result in better resolution but limit the low range of period measurements.
  4. Describe a method that would allow you to adjust the prescale value that would allow you to adjust the prescale or switch from period to pulse, counting to optimize the resolution of the measured frequency.
    • ANSWER: A table which maps frequency ranges to procedures can be made. The computed frequency is compared against this table. If the frequency is in a different optimization range, the improved resolution measurement would not be used until the next single sample.
  5. From the graph generated in step 4 of the Project Testing section, what conclusions can be made concerning the linearity of open-loop speed control of a DC motor?
    • ANSWER: The motor speed as a function of PWM duty cycle is proportional but is not linear. Given that the load is constant, a table could be generated that would compensate for the nonlinearity. A better approach is to use feedback and operate as a closed-loop controller.
  6. For a given timer clock rate, what value of input signal frequency will have the same resolution for both the period measurement and the counts per unit time that also gives the same measurement update rate?
    • ANSWER: Unknown.

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.