www.eXtremeElectronics.co.in

xAPI For Advance Motor Control.

xBoard v2.0

Easy to Use learning and development tool for Atmel AVR family of MCUs.

 

<< Return To Help Index

DC Motor Control is a basic requirement for making any robotics based project. xBoard v2.0 is optimized for small robotics application. Where it is used as main controller board. That is why xBoard v2.0 has four high current capacity motor drivers that can run 4 DC Motors. It can supply upto 1 Amp current per motors. It uses two L298N ICs for the purpose. Freewheeling Diodes required to protect the ICs is also built in. Motors can be connected and disconnected from the board very easily.

To further simplify your job xAPI™ has support for easily controlling the DC Motors. In the previous tutorial we have seen a very simple program to control DC motor. In that we only controlled the direction of the motors by software. But sometime we need more control. For complete control we must also be able to adjust the speed of the motor too. So in this tutorial we will explore the MOTOR Control Part of the xAPI. Previously xAPI helped you to easily use the LCD Module and the IR Remote control.

Connecting Motors.

motor connector
Motor Connectors On xBoard v2.0
dc motor connector xboard
Motors Connected with xBoard v2.0

 

dc motor
A Geared DC Motor.

 

motors with xboard
The Whole Setup

 

Electrical Connection.

Detailed electrical connection of Motor driver with the MCU can be found in the xBoard v2.0 Schematic. A simplified block diagram is given below.

motors with xboard
Primary Motor Control Unit (for MOTOR A and B)

From the above diagram it is clear that PC0 and PC1 control the direction of Motor A while PC2 and PC3 control the direction of Motor B.

The Jumper JP2 allows the user to select from one of the two modes.

First mode is selected when Jumper JP2 is in ON state while second mode is selected when jumper is in OFF state.

USART, JP4, JP2 and JP3 Location

Similarly JP3 is used for Motor B mode setup.

xAPI for Motor Control

The DC Motor Core of the xAPI is contained in two files "motor.c" and "motor.h"

It is made up of only three functions.

void MotorInit(void)

This function must be called before any other Motor Control Function is used. It sets up the I/O Ports and Initializes the PWM Module of AVR.

********

void MotorA(uint8_t dir,uint8_t speed)

This function controls the Motor-A. The first argument is the direction of rotation required. It can be one of the following constants.

MOTOR_STOP - Stops the motor.

MOTOR_CW - Rotates the Motor Clockwise (CW).

MOTOR_CCW - Rotates the Motor Counter Clockwise(CCW).

The Second Argument is the speed required. It can be any value between 0 and 255.

Examples

MotorA(MOTOR_CW,128); //Set Rotation as Clockwise with approx half the max speed.

The above example set the direction of rotation of motor-a as Clockwise with approximately half the maximum rated speed. If the motor was a 200 RPM motor then it will rotate approx 100 revolutions per minute (RPM).

********

void MotorB(uint8_t dir,uint8_t speed)

This function controls the Motor-B. The first argument is the direction of rotation required. It can be one of the following constants.

MOTOR_STOP - Stops the motor.

MOTOR_CW - Rotates the Motor Clockwise (CW).

MOTOR_CCW - Rotates the Motor Counter Clockwise(CCW).

The Second Argument is the speed required. It can be any value between 0 and 255.

Examples

MotorB(MOTOR_CCW,25); //Set Rotation as Anti Clockwise with approx 10% of the max speed.

The above example set the direction of rotation of motor-b as Counter Clockwise with approximately 10% of the maximum rated speed. If the motor was a 200 RPM motor then it will rotate approximately 20 revolutions per minute (RPM).

********

Sample Code

Following sample code makes use of the above functions to demonstrate advance motor control feature of xBoard v2.0. The example will do the following

Before running the demo make sure that jumper JP2 is in ON state or the speed control won't work.


/*********************************************************************

                 xBoard(TM) v2.0 Sample Programs

               ------------------------------------


Description :  Demonstrate the use of DC motor. Teaches you how to control
            the speed and direction od DC motors.

            The program starts the MOTOR - A in CW direction with
            with Full Speed. Then it changes speed to half.

            After that the direction is reversed and speed is again
            set to MAX. After some time speed is reduced to 50%

            This whole process is repeated again and again.


Author      : Avinash Gupta 2008
Web         : www.eXtremeElectronics.co.in

                   
**********************************************************************/

#include <avr/io.h>
#include <util/delay.h>

#include "motor.h"

//Simple Delay Function

void Wait()
{
   uint8_t i;

   for(i=0;i<250;i++)
      _delay_loop_2(0);

   for(i=0;i<250;i++)
      _delay_loop_2(0);
}


void main()
{
   //Initialize motor subsystem
   MotorInit();

   while(1)
   {
      //Start Motor A in Clock Wise (CW) direction with full speed (255)
      MotorA(MOTOR_CW,255);

      //Wait

      Wait();

      //Set speed to half
      MotorA(MOTOR_CW,127);

      //Wait
      Wait();

      //Now change direction to Counter Clock Wise (CCW)
      MotorA(MOTOR_CCW,255);

      //Wait

      Wait();

      //Now change direction to Counter Clock Wise (CCW)
      MotorA(MOTOR_CCW,127);

      //Wait
      Wait();
   }

}

Compile the project to get the HEX file. Now burn the HEX file to xBoard and connect a Motor in connector labeled MOTOR-A. After that make sure the jumper JP2 in in ON position. You are now ready to power up the xBoard and watch the MOTOR in Action.

NOTE: If the motors rotates opposite of the expected direction then you must have wired the motor in wrong polarity. The left wire in the connector should be RED and the right one should be BLACK as shown in above image.

NOTE: You need a powerful Battery or Adaptor (current supply of 1A or More). The project would not run properly with small 9V battery or small adaptor(500ma).

Secondary Motors

Their are two secondary motors named MOTOR-C and MOTOR-D. They are optional and are not available with some models. You can only control their direction or stop them. You cannot control their speed. They are connected with the MCU in the following way. More detail in the xBoard v2.0 Schematic.

Secondary Motor Driver Block (for Motor C and D)

Signal Details

Control Truth Table for Motor-C

 
PC4
PC5
Clockwise Rotation
HIGH
LOW
Counter Clockwise Rotation
LOW
HIGH
Stop
LOW
LOW
Stop
HIGH
HIGH

Control Truth Table for Motor-D

 
PC6
PC7
Clockwise Rotation
HIGH
LOW
Counter Clockwise Rotation
LOW
HIGH
Stop
LOW
LOW
Stop
HIGH
HIGH

 

<< Return To Help Index