PDA

View Full Version : Help in PWM!!



robodam
03-19-2010, 06:47 PM
Hello experts,
I am stuck in a big problem. I am making a line follower robot and the algorithm which I have employed reduces speed of one motor and increases another motor speed by same amount.
This I have done by changing values of OCR1A and OCR1B registers. I am using Atmega 128 with PWM on timer 1 on channels A and B.
The PROBLEM is I am not able to make motors run at different speeds i.e. both of my motors are going at same speed.My logic to line follow is possibly correct as the speed of motors are changing with sensors input. I cannot catch the bug .Please help!
here is my code which I have used to initialise PWM:

OCR1B=_BV(CS11)|_BV(WGM13);
TCCR1A=_BV(COM1B1)|_BV(COM1A1);
ICR1=1000;

robodam
03-20-2010, 12:04 AM
No I am not using it. I am just trying to follow line using closed loop Propotional control.

avinash_g
03-20-2010, 08:28 AM
Any special reason for using ATmega128 ???

The xBoard design is Open and all schematic and source code are available for download. You may download the source code for the motor part and use it. For tuning the hardware see the schematics.

avinash_g
03-20-2010, 08:31 AM
See source code for PWM Motor speed control here


Listing For "motor.c"



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

eXtreme Electronics xAPI(TM)
----------------------------
xAPI is a Powerful but easy to use C library to program the xBoard(TM)
series of AVR development board. The library has support for commonly use tasks
like:-

*LCD interfacing
*MUXED 7 segment displays.
*Remote Control
*Serial Communication
*DC Motor Controls with Speed Control
*Analog Measurement for Sensor Interface.
*Temperature Measurement.
*I2C Communication.
*EEPROM Interface
*Real Time Clock (RTC Interface)

The APIs are highly documented and easy to use even by a beginner.

For More Info Log On to
www.eXtremeElectronics.co.in

Copyright 2008-2009 eXtreme Electronics India

DC Motor Core
---------------------
This module is used for controlling DC motors via the on board L298 driver IC
The library has functions to control both the direction and speed of DC motors.
The PWM speed controlled is done by the library internally.


Please give credit to www.eXtremeElectronics.co.in if you use
it in your projects and find it useful

NOTICE
--------
NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.


WRITTEN BY:
AVINASH GUPTA
me@avinashgupta.com


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

#include <avr/io.h>
#include <avr/interrupt.h>

#include "motor.h"

void MotorInit()
{

//set up pwm for speed control

TCCR1A=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM10);

//clk=fcpu/64

TCCR1B=(1<<CS11)|(1<<CS10);

//Set the corresponding port pin to output

DDR(OC1A_PORT)|=(1<<OC1A_PIN);
DDR(OC1B_PORT)|=(1<<OC1B_PIN);

//Set the direction control PINs to OUT
DDRC|=0X0F; //PC0 to PC3 as output

}

/************************************************** ********
Descriptipn:
Set the rotation of motor A in given direction and speed

dir = 0 for stop
dir = 1 for CW rotation
dir = 2 for CCW rotation
dir = 3 for brake

speed is any value between 0 and 255

Example:
MotorA(MOTOR_CW,120);

Note:
Other Constants for direction are
MOTOR_CCW and MOTOR_STOP

************************************************** *********/
void MotorA(uint8_t dir,uint8_t speed)
{
//Direction
if(dir == MOTOR_STOP)
{
PORTC&=(~(1<<PC0));
PORTC&=(~(1<<PC1));
}

else if(dir == MOTOR_CCW)
{
PORTC&=(~(1<<PC0));
PORTC|=(1<<PC1);
}
else if(dir == MOTOR_CW)
{
PORTC&=(~(1<<PC1));
PORTC|=(1<<PC0);
}

//Speed
uint8_t sreg=SREG;

cli();

OCR1A=speed;

SREG=sreg;
}

/************************************************** ********
Descriptipn:
Set the rotation of motor B in given direction and speed

dir = 0 for stop
dir = 1 for CW rotation
dir = 2 for CCW rotation
dir = 3 for brake

speed is any value between 0 and 255

Example:
MotorB(MOTOR_CW,120);

Note:
Other Constants for direction are
MOTOR_CCW and MOTOR_STOP

************************************************** *********/
void MotorB(uint8_t dir,uint8_t speed)
{
//Direction
if(dir == MOTOR_STOP)
{
PORTC&=(~(1<<PC2));
PORTC&=(~(1<<PC3));
}

else if(dir == MOTOR_CCW)
{
PORTC&=(~(1<<PC2));
PORTC|=(1<<PC3);
}
else if(dir == MOTOR_CW)
{
PORTC&=(~(1<<PC3));
PORTC|=(1<<PC2);
}

//Speed
uint8_t sreg=SREG;

cli();

OCR1B=speed;

SREG=sreg;
}





Listing For "motor.h"



#include <avr/io.h>

#ifndef MOTOR_H
#define MOTOR_H

#define OC1A_PORT D
#define OC1A_PIN PD5

#define OC1B_PORT D
#define OC1B_PIN PD4


#define DDR(a) __CONCAT(DDR,a)
#define PORT(a) __CONCAT(PORT,a)
#define PIN(a) __CONCAT(PIN,a)


#define MOTOR_STOP 0
#define MOTOR_CW 1
#define MOTOR_CCW 2

void MotorInit();
void MotorA(uint8_t dir,uint8_t speed);
void MotorB(uint8_t dir,uint8_t speed);

#endif

robodam
03-20-2010, 06:48 PM
Thanks avinash!
Rest I'll tell you after using it.

avinash_g
03-20-2010, 07:44 PM
@robodam

You are most welcomed !