Skip to main content

Subsection 13.2.5 Part 5: Design Exercise, Verifying the Pulse on the Oscilloscope

Weโ€™ll verify the pulse on the oscilloscope before connecting the servo: a wrong pulse would drive the servo against its mechanical stops, so the pulse is checked on the screen first. The potentiometer wiring is the ADC chapterโ€™s test circuit (Figureย 6.2.6), and the two captures below (Figureย 13.2.23, Figureย 13.2.24) are what a good capture looks like. For the stretch, milliseconds() is explained in Subsectionย 11.5.4.

Activity 13.2.8. A potentiometer-controlled pulse on PA7.

Weโ€™ll verify the pulse on the oscilloscope before connecting the servo: a wrong pulse would drive the servo against its mechanical stops, so the pulse is checked on the screen first. Work through these in order. The servo stays unplugged until your pulse is verified.

(a)

Copy your TemplateProject to a new project named Servo. Download Day15_servo_template.c, tim.c and tim.h from the bookโ€™s starter files; the two .c files go into Src and tim.h into Inc. Youโ€™ll also need your ADC code. If you have previously added your adc.c and adc.h from Lab 5 to the mylib folder you are good to go; otherwise copy adc.c into Src and adc.h into Inc.

(b)

Complete the five #define lines with the numbers you chose earlier today (the prescaler factor, the counterโ€™s top value, and the three compare values for a 1 ms, a 1.5 ms and a 2 ms pulse) and the map line. If your ADC functions have different names from pa0_adc_init() and adc_read(), adjust those two calls to your own names.

(c)

Wire the potentiometer to A0 (PA0) exactly as in the ADC chapterโ€™s test circuit: the outer terminals to 3.3 V and GND, the wiper to A0. Connect the AD2โ€™s channel 1, the orange lead, to PA7 (D11), and channel 1โ€™s ground lead, the one with the white stripe, to a GND pin. Nothing else is connected to PA7.

Activity 13.2.9. Predict the pulse.

Before you capture anything, think about three things: the time base that shows one full 20 ms period on the screen; the trigger you will use; and the pwm_value your program will print with the knob at one end, in the middle, and at the other end.

Activity 13.2.10. Measure the pulse.

(a)

Build, run, and open CoolTerm. Turn the knob from one end to the other: pot_value should run from 0 to 4095, and pwm_value between the numbers you predicted.

(b)

On the oscilloscope, trigger on the rising edge and measure the pulse width and the period at each end of the knob, and verify that the pulse width sweeps smoothly from 1 ms to 2 ms as you rotate the potentiometer. Check what you measure against the prediction you made. The two captures below show what a good capture looks like: note their time base and their trigger setting.

(c)

Find the knob position that gives a 1.5 ms pulse. Remove and reattach the knob, if necessary, so that it is at the midway position. Then show your sweeping pulse to an instructor.
Figure 13.2.23. The pulse on PA7 with the knob at one end, captured on the AD2 at 5 ms/div with a rising-edge trigger on channel 1 (the empty lower half of the screen is cropped out). The cursors measure the pulse width at 1.0056 ms and the period at 19.91 ms, which the cursor table reports as 50.2 Hz.
Figure 13.2.24. The same capture with the knob at the other end. The pulse is now 2.008 ms wide, and the period has not moved: 19.93 ms against 19.91 ms, 20 ms to within the cursorsโ€™ placement.
Nothing on the screen, or not what you predicted? Work down this list, since each symptom points somewhere different.
  1. The project will not build. Look at the #define lines first: a blank one puts nothing where the compiler expects a number, and the error is reported at the line that uses the name rather than at the blank itself. Then check that tim.c and your adc.c are in the project, and that there is exactly one main() in it.
  2. No pulse at all. Look at the terminal first. If pot_value is printing, the program is running and the fault is on the scope side: channel 1 on PA7 (D11), and its striped ground lead on a GND pin. If nothing prints, the build never reached the board, which is the first itemโ€™s checks.
  3. A pulse of the wrong width or period. One of the #define values is off. PWM_PSC_FACTOR and PWM_TIMER_MAX set the period; SERVO_MIN and SERVO_MAX set the width at the two ends of the knob.
  4. A pulse that does not move with the knob. The fault is in the ADC or the map. Look at pot_value in the printout: if it is not changing, the fault is before the map; if it is changing and pwm_value is not, the fault is in the map.

Activity 13.2.11. Going further.

These are optional, for when your pulse is verified.

(a)

Replace the TIM16 interrupt with milliseconds() from Day 12, the way the motor chapterโ€™s main loop uses it: rewrite the loop so that the sampling interval is a #define and no interrupt of your own is needed. Nothing else about the program changes. In Lab 8 you are asked to keep track of the sampling interval this way.

(b)

The map turns 4096 potentiometer readings into 200 timer counts. How many readings share one timer count, and what does that mean for how the knob feels as you turn it? Then: if you wanted one timer count per reading, what would PWM_TIMER_MAX have to be, and what would go wrong with the pulse period?