>> The pulse width modulation (PWM) mode in timers of ATmega 16 have the main purpose of controlling the speed of a dc motor.
>> This is a simple program where the PWM is used to gradually increase the speed of the dc motor until reach max speed.
>> Also the speed of the dc motor is decreased gradually until the motor stops.
>> This is a simple program where the PWM is used to gradually increase the speed of the dc motor until reach max speed.
>> Also the speed of the dc motor is decreased gradually until the motor stops.
Program Specification
1- The program use ATmega16 MUC.2- L293D to supply the required current for the motor
3- 4 buttons are uesd to contol dc motor.
>> sw1 : to speed up dc motor speed until max in clock wise direction.
>> sw1 : to speed up dc motor speed until max in clock wise direction.
>> sw2 : to decrease dc motor speed until it stops in clock wise direction.
>> sw3 : to speed up dc motor speed until max in counter clock wise direction.
>> sw4 : to decrease dc motor speed until it stops in counter clock wise direction.
>>>>>>> This code can be used in controlling lift (elevator ) motor.
Code Sample
#include <mega16.h>unsigned int d=4369,x,y;
char flag; // flag =1 left direction , flag =0 right direction
// Timer 1 overflow interrupt service routine
interrupt [9] void timer1_ovf_isr(void)
{
// Place your code here
if (flag ==1)
{
if (x==0)
{
TIMSK &=0b11111011; // disable interrupt
}
else
{
OCR1A=OCR1A-d;
x=OCR1A;
}
}
if(flag==0)
{
if (y==0)
{
TIMSK &=0b11111011; // disable interrupt
}
else
{
OCR1B=OCR1B-d;
y=OCR1B;
}
}
}
void main(void)
{
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x10;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0xFF;
ICR1L=0xFF;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x10;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0xFF;
ICR1L=0xFF;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Global enable interrupts
#asm("sei")
#asm("sei")
while (1)
{
// Place your code here
if (PINA.0==1)
{
OCR1A=0xffff;
x=OCR1A;
TCCR1A |= 0b11000000 ; // inv mode for OC1A
TCCR1B |= 0b00000010 ; // start timer one with 1 mhz
flag =1;
TIMSK |=0b00000100;
while (PINA.1==0)
{}
OCR1A=0xffff;
x=OCR1A;
TCCR1A &= 0b10111111 ; // non inv mode for OC1A
TIMSK |=0b00000100;
}
if (PINA.2==1)
{
OCR1B=0xffff;
y=OCR1B;
TCCR1A |= 0b00110000 ; // inv mode for OC1B
TCCR1B |= 0b00000010 ; // start timer one with 1 mhz
flag =0;
TIMSK |= 0b00000100; // enable interrupt
while (PINA.3==0)
{}
OCR1B=0xffff;
y=OCR1B;
TCCR1A &= 0b11100000 ; // non inv mode for OC1B
TIMSK |= 0b00000100; // enable interrupt
}
};
}
Proteus Simulation
>>>> Here is a link including the project file using codevision avr compiler and proteus simulation
Speed and Direction Control of a DC motor