SCHEMATIC 5. This time,
we’re interested in the
multiplexed function of the
I/O pin. For instance, we
will enable Output Capture
module 2 and its output pin
OC2 which disables the I/O
pin’s RD1 functionality.
Compare module (OCMP) and a timer to generate
continuous PWM signals. So, if we are to match up
direction and PWM signals with the J1 connector
shown in Schematic 4, we’ll need to select a Pmod
connector with a general-purpose I/O pin on its pin
1 and an OCMP output on its pin 2. Also, if we plan
ahead to attach a motor with feedback circuitry,
we’ll need general-purpose I/O pins at the selected
Pmod’s pins 3 and 4 to act as inputs. Browsing the
32MX4’s nine Pmod connectors led me to the JD
Pmod connector. You can check my connector
selection work against Schematic 5.
Since the PmodHB5’s Direction input is fed from
a PIC32MX460F512L general-purpose I/O pin, you
already know how to set up the Direction I/O pin on
the PIC side. So, let’s write the code to define and
initialize our Direction I/O pin which (according to
Schematic 5) is RD7:
FIGURE 2. Figures don’t lie and neither does the CleverScope.
This trace was taken from the Enable input pin of the PmodHB5.
#define trisMtrClr TRISDCLR
#define dirMtrFwd
#define dirMtrRev
#define dirMtrPin
PORTDCLR
PORTDSET
7
//clear RD7 TRIS
//bit
//clr a PORTD bit
//set a PORTD bit
trisMtrClr = (1 << dirMtrPin);
//RD7 = Direction output
dirMtrFwd = (1 << dirMtrPin);
//RD7 = 0 = forward
That takes care of the Direction signal from the
PIC32MX460F512L’s point of view. So, let’s get the OCMP
module ready for PWM operation. We do this by setting up
the correct bit pattern in the OS2CON register:
OS2CON = (1 << 2) | (1 << 1);
//load with 0x00000006 = PWM mode
The use of the C bit shift operations reduces the
possibility of unintentionally overwriting other bits that may
have been previously set up in the OS2CON register. Since
we’re initializing the PWM engine, we need not write a
duty cycle value into the OC2R and OC2RS registers at this
time. So, we’ll stuff a zero into both registers which will kill
any PWM activity:
OC2R = 0; //duty cycle register – read only
once the PWM module is enabled
OC2RS = 0; //duty cycle holding register
The OS2CON Output Compare Control Register powers
up with a value of zero. By leaving bit 2 of the OS2CON
clear, we have selected Timer 2 as the time base for our
PWM signal. So, let’s punch a suitable value into the Timer
SERVO 12.2009 51