Beruflich Dokumente
Kultur Dokumente
In the last part we saw the basics of DC motor interfacing with AVR microcontroller. In this part
we step further to look at the xAPI routines for motor control which gives easy access to the
motors on our robot.
Connecting Motors.
motor.c
motor.h
myutils.h
You can get these files from the lib folder in the DVD.
To use the motor Library do the following :1. Create a new folder lib in the project using Solution Explorer.
Sample Code
Following sample code makes use of the above functions to demonstrate advance motor
control feature of xAPI. The example will do the following
Change direction of Motor to Counter Clockwise and Again set the speed to MAX.
/*********************************************************************
LFR Board Sample Programs
-----------------------------------Description : Demonstrate the use of DC motor. Teaches you how to
control
the speed and direction of 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
Web
**********************************************************************/
#include <avr/io.h>
#include <util/delay.h>
#include "lib/motor/motor.h"
//Simple Delay Function
void Wait();
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();
}
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);
}
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).
AdvMotorDemo.hex