Vous êtes sur la page 1sur 5

Final Project

Input Capture on Motor Speed

Eric Terry

April 26, 2012

Introduction
The speed of a motor can be measured by using input capture on a PIC 32 microcontroller. In this project a motor was controlled by PWM by using output capture and a potentiometer. An optical tachometer attached to the end of the motor sends 192 pulses per revolution. The input capture will read the time in between each pulse. Then the speed of the motor is found by multiplying the time by 192. The goal was to be able to adjust the speed of the motor with the potentiometer by having the program compare the actual speed with the desired speed set by the potentiometer. Then the duty cycle would be increased or decrease until the speed of the motor matches the desired speed with the potentiometer.

Procedures
The clock was configured to have a clock frequency of 40 Mhz. Output Compare 3 was used to generate the PWM. To make the PWM 1ms long the OC3RS register was set to 40000 or 0x9c40 in hexadecimal. The duty ratio of the PWM is adjust by adjusting OC3RS value. At a duty ratio of one OC3RS is 0x9c40 and at 0 OC3RS is 0. The Input Capture 1 was configured with the register IC1CON set equal to 0x8203. This configuration turns on the input capture at the first rising edge and will capture every rising edge thereafter. To make the Input Capture 1 pin an input _TRISD8 = 8 since RD8 share the same pin as IC1. An interrupt was constructed for the input capture by enabling the multivectored interrupt function with the code: INTEnableSystemMultiVectoredInt(); The priority of the interrupt to read the voltage from the ADC was set to 6 and the interrupt used for the input capture was set to 7. The interrupt service routine for the input capture is shown in Figure 1 below. The service routine will be activated when the rising edge of the square wave from the tachometer comes. First a while statement waits for the capture to be ready. Then in the first if statement when the capture is ready the time of the first rising edge is captured and set equaled to CaptureTime. In the second if statement when the second rising edge occurs CaptureTime1 will be set equaled to this time. The period between each rising edge will be found by subtracting CaptureTime from CaptureTimer1.
void __ISR(_INPUT_CAPTURE_1_VECTOR, ipl7) IC1Interrupt(void) { //period = TMR3; while(!mIC1CaptureReady()); if(mIC1CaptureReady()) { CaptureTime = mIC1ReadCapture(); } //while(!mIC1CaptureReady()); if(mIC1CaptureReady()) { CaptureTime1 = mIC1ReadCapture(); }

period = CaptureTime1 - CaptureTime; mIC1ClearIntFlag();

Figure 1: Interrupt Service Routine for the Input Capture

Another interrupt service routine was used to read the voltage from the potentiometer this is shown in Figure 2 below.
void __ISR(_TIMER_2_VECTOR, ipl6) T2Interrupt(void) { a = readADC(POT); temp = (a*0x9c40)/1023; OC3RS = temp; mT2ClearIntFlag(); //clear flag and exit } //end T2 interrupt

Figure 2: Interrrupt Service Routine for the Potentiometer reading The analog voltage from pin AN5 and is converted to a digital number. The variable a is the voltage in digital form. a has a 10 bit resolution so when it is at full capacity it is 1023 bits. a has to be adjust so that it matches the same proportion of the OC3RS register, so it is multiplied by 0x9c40 and divided by 1023.

Results
In the execution of this program some breakpoints were set in the interrupt service routine to test how long it was interrupting. When motor was set for a constant speed the interrupt for the input compare interrupted sporadically. Sometimes a few minutes would pass before it would interrupt. A watch window (as shown in Figure 3) was also used in the simulation to see if the period would remain constant after each interrupt. The period was not consisted but it mostly remained in the range of 3 to 11 ticks.

A possible reason for the inconsistency of the period is that my clock rate for Timer3 was not fast enough to keep up with the speed of the motor. Another possibility is that there was a lot of noise in the output of the tachometer to obtain a crisp rising edge in the square wave. This could explain why sometimes the input capture interrupt would sometimes take a 3 minutes to interrupt, because it would take a will for it to find a well-defined rising edge. Figure 4 shows the voltage wave form of the output of the tachometer displayed on an oscilloscope.

Figure 4: Voltage Waveform of the Tachometer.

If the input capture on this project was working the speed of the motor could be obtained by dividing the period by 40 Mhz and multiplying by 192. And with this speed the program could be modified to change the duty ratio to match a desired speed that could be adjusted by the potentiometer. But, without the input capture working a speed of the motor was unable to be captured. And without the capturing of the speed, a speed control was not achieved.

Conclusion
Input capture can be very useful in controlling the speed of motors if one can figure it out. There is a lot of careful thought that needs to go into the timing and the configuration of the input capture. One must understand how the input capture works to use it effectively.

Vous aimerez peut-être aussi