|
PIC to Motor Mind B
This library provides you with a set of functions to interface a PIC
16C84 to a Motor Mind B module from Solutions Cubed. I wanted to
provide a simple and clean way to interface a number of MMB modules to
a PIC for my PIC Robot. Instead of writing one-off code, I decided to
build a reuseable library of code.
One of the interesting features of this library is that it can support
any number of MMB modules. In my LCD library
you have to define the port and pin the LCD is connected too. It will
only support one LCD module, which is a limitation I was happy
with. However, it is quite likely that you will have more than one MMB
connected to your PIC - at least two are needed for a 2 wheeled robot!
The library needed some extra work to generalise the code so that it
could send serial commands over any pin and port.
Features of a Motor Mind B
Before we go interfacing to this module, let's examine what its
functionality is:
- Interfaces with a PIC using a 2-wire serial protocol. One wire
transmits data to the MM from the PIC, and a second returns status
information. The current version of the library only uses the send
wire, so a one-wire serial interface is possible. The serial link
operates at 2400 baud.
- It can drive motors up to 30V at 3.5 amps. It is packaged with a
large heat sink to disipate the heat involved in driving larger
motors.
- It provides speed control of motors. Speed control is achieved
using Pulse Width Modulation (PWM). PWM allows you to control the
speed of a motor by sending short pulses of current to it, rather than
simply turning it on and off.
- Motor direction control is provided, and a motor can be reversed.
- You can drive the motors from a seperate power supply than that
powering the logic side of the module. This is a very useful feature
for reducing noise and brown outs on the logic power supply rails.
- If the motor has a tachometer attached, it can return the current
RPM of the motor.
- In combination with the PWM feature, you can specify a speed you
want the motor to remain at, and it will continually monitor the
motor's speed and adjust the pulses to the motor to maintain the
preset speed.
Phew! That's quite a feature set, and more than most people will need
for initial experimentation.
Library functions provided
The following functions are provided by the library:
- void mm_putc(char port, char pin, char c) Send the byte c
to a Motor Mind B module connected to port and pin. The port parameter
corresponds to the PORT location in the PIC's register file: PORTA is
location 5, and PORTB is location 6. The pin parameter defines which
bit of the specified port the MMB is attached to.
- void mm_start(char port, char pin, char dutycycle) Start
the motor attached to port and pin. Set the dutycycle (speed) of the
motor.
- void mm_stop(char port, char pin) Stop the motor attached
to port and pin.
- void mm_reverse(char port, char pin) Reverse the
direction of the motor attached to the specified port and pin.
It's a pretty limited set of commands right now, but I will add more
over time.
Library Source Code
You can download the library, and the example source code I describe
further on below.
- motor.c C source code for the library.
- motor.lib library file.
- mmtest.jpg schematic of wiring the Motor Mind B
to the PIC.
- mmtest1.c test source code to turn motor on and
off only (also available as a hex file to
download into the PIC).
- mmtest2.c test source code to reverse motor
direction(also available as a hex file to
download into the PIC).
- mmtest3.c test source code to run motor
continualy, but slow it down over time (also available as a hex file to download into the PIC).
Example 1 - Turn the motor on and off
In this first test, we will just turn the motor on and off
continually. This verifies that the serial link is working correctly,
and the motor is receiving power.
/*
* mmtest1.c
*
* Motor Mind Driver library Test
*
* Test one: turn the motor on and off
*
* Motor Mind module is conected to PORTA
* FM pin on MM is connected to RA1 pin 18
* LED on RB4 via 1K resistor
*
* Mark Crosbie 10/31/98 mark@mastincrosbie.com
*/
#define MOTOR 0x5
#define MMPIN 1
#define LED 4
void main(void) {
set_bit(STATUS, RP0); /* select the register bank 1 */
clear_bit(TRISA, MMPIN); /* Port A.1 is output to motor */
clear_bit(TRISB, LED); /* Port RB.4 is output LED*/
clear_bit(STATUS, RP0);
while(1) {
/* start the motor at half speed */
mm_start(MOTOR, MMPIN, 128);
delay_s(2);
/* stop it */
mm_stop(MOTOR, MMPIN);
/* flash the LED once */
set_bit(PORTB, LED);
delay_s(1);
clear_bit(PORTB, LED);
delay_s(1);
}
}
An LED is connected to PORTB pin 4 which is flashed each time through
the loop. The speed of the motor is set to half: the parameter
dutycycle to mm_start() can range from 0 to 255,
with 0 effectively turning off the motor and 255 running it full speed
ahead. A setting of 128 chooses a half-speed setting.
Example 2 - Test reversing the motor direction
Building upon the previous example, we will test the reverse
command. It is safer to STOP the motor before reversing it: if you
reverse a running motor a large back-emf current is generated. This
current spike could damage the Motor Mind B module!
/*
* mmtest2.c
*
* Test two: turn on the motor, let it run for 2 seconds,
* reverse direction, flash LED and then turn the
* motor on again
*
* NOTE: It is safer to stop the motor before reversing its
* direction. If you don't then a potentially large back-EMF current
* from the motor could destroy the PIC or the Motor Mind B module
*
* Motor Mind module is conected to PORTA
* FM pin on MM is connected to RA1 pin 18
* LED on RB4 via 1K resistor
*
* Mark Crosbie 10/31/98
*/
#define MOTOR 0x5
#define MMPIN 1
#define LED 4
void main(void) {
set_bit(STATUS, RP0); /* select the register bank 1 */
clear_bit(TRISA, MMPIN); /* Port A.1 is output */
clear_bit(TRISB, LED); /* Port RB.4 is output */
clear_bit(STATUS, RP0);
while(1) {
/* start the motor at half speed */
mm_start(MOTOR, MMPIN, 128);
delay_s(2);
/* stop the motor */
mm_stop(MOTOR, MMPIN);
/* reverse direction */
mm_reverse(MOTOR, MMPIN);
/* flash LED */
set_bit(PORTB, LED);
delay_s(1);
clear_bit(PORTB, LED);
delay_s(1);
}
}
|
PIC Microcontroller Project Book
Lot's of great PIC project ideas!
Programming and Customizing the Pic Microcontroller
If you are learning to program microcontrollers then Myke's book is good start.
The Art of Electronics
A classic in the field. Teaches you the art and science of linear and digital electronic design.
If you want to learn why your circuit is not working, read this book and you'll know why.
Mobile Robots: Inspiration to Implementation
A very readable introduction to the art of robotic design and implementation from the best
practioners in the field: the MIT Artificial Intelligence Lab!
Microcontroller Cookbook
A set of cookbook style designs for the 8051 and PIC microcontrollers.
A handy reference to have if you need a quick solution to a problem.
|