SP.711: Thursday, Day 2 Today in class you will get some more practice with PWM, this time using the PIC's built in PWM generator. You will vary the speed of a small motor using a transistor as a switch (probably the cheapest and easiest way to drive a motor) As always, feel free to combine several of the following "assignments" together or do things differently: 1) The PIC has a "hardware PWM generator" that can automatically generate a PWM signal at a duty cycle you choose, and at one of three frequencies (roughly 1.25 kHz, 5 kHz, and 20 kHz) The C commands to use this PWM generator are: Setup commands to run once, at the beginning of main(): SETUP_CCP1(CCP_PWM); // setup a special pin (pin RC2) // to be a PWM output pin SETUP_TIMER_2(T2_DIV_BY_16,255,1) // set the PWM frequency to 1.25 kHz // (change to T2_DIV_BY_4 for 5 kHz, T2_DIV_BY_1 for 20 kHz) and then, to actually use the PWM generator: set_PWM1_duty(#); // (# = 0 (full off) to 255 (full on)) // 128 = 50 % duty cycle, etc... And the PIC will automatically turn pin RC2 on and off in the background at the duty cycle you specify! Write (or modify old) code to change the brightness of an LED as you adjust a potentiometer. 2) Now instead of changing the brightness of an LED (boring...), we'll change the speed of a small motor. If the motor needed 20 mA or less at 5 V, we could run it directly off the PIC, but the small motors we have probably need 200-1000 mA, so you'll need to use a transistor as a switch. The PIC will turn the transistor on and off with some duty cycles, which will turn the motor circuit on and off at the same duty cycle. Hook up the circuit below and test it with your code from part (1). +V (where +V is a +5V, 1 Amp benchtop supply) | |----+ | _|__ | /\ (optional diode) MOTOR /__\ | | |----+ / R |/ PIC -/\/\--| RC2 |\ \ | GND 3) [tangent, you need to either do this today or over the weekend] We will be using the LMD18200 motor driver chip for much of this seminar. This chip is a great motor driver, though a little pricey ($20, don't lose it). It also has an annoying pin layout that doesn't fit in standard protoboards. Matt made a small circuitboard (preview of the third seminar) and you should solder the LMD18200 and a row of header pins to this circuit board so that you can easily plug the LMD18200 into your protoboard. 4) ("extra"): think about how to make something like in part (2), but where the motor can reverse direction.