Featured post

Objectives and content

Thursday, 8 September 2016

Interface a DC motor

DC Motor Interfacing

Expt-1

Titile: Interface a DC motor through L293D

Circuit:
Code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <compat/deprecated.h>
#include <util/delay.h>
void pwm_init()
{
// initialize TCCR0 as per requirement, say as follows
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
}
/*void pwm_init1()
{
// initialize TCCR2 as per requirement, say as follows
TCCR2 |= (1<<WGM20)|(1<<COM21)|(1<<WGM21)|(1<<CS20);
}*/
int main()
{
uint8_t lr=35;
uint16_t hr=282, rpm=35, duty=0;
duty = 30; // duty cycle = 45% of 255 = 114.75 = 115
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB = 0x98;
sbi(PINB,0);
sbi(PINB,1);
sbi(PINB,2);
sbi(PINB,5);
sbi(PINB,6);
DDRD|=(1<<PIND7);
PORTB |=(1<<PB4);//set initial rotation direction;
PORTB &=0x7F;//set initial rotation direction;
// make sure to make OC0 pin (pin PD7 for atmega32) as output pin
_delay_ms(50);
while(1)
{
if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
_delay_ms(500);
duty +=30;
rpm+=35;
}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
_delay_ms(500);
duty -=30;
rpm-=35;
}
else if ((bit_is_clear(PINB,2))&&(bit_is_set(PINB,1)) && (bit_is_clear(PINB,0)))
{
PORTB |=(1<<PB4);//set initial rotation direction;
PORTB &=0x7F;
PORTB|=0x08;// pull up PB3
pwm_init();// initialize timer in PWM mode
OCR0 = duty;
while(1)
{
_delay_ms(50);
if(bit_is_clear(PINB,1))
{
break;
}
else if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
_delay_ms(500);
duty +=30;
rpm+=35;
OCR0 = duty;
}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
_delay_ms(500);
duty -=30;
rpm-=35;
OCR0 = duty;
}
}
}
else if((bit_is_clear(PINB,2))&&(bit_is_clear(PINB,1))&&(bit_is_set(PINB,0)))
{
PORTB |=(1<<PB7);//set initial rotation direction;
PORTB &=0xEF;
PORTB|=0x08;// pull up PB3
pwm_init();// initialize timer in PWM mode
OCR0 = duty;
while(1)
{
_delay_ms(50);
if (bit_is_clear(PINB,0))
{
break;
}
else if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
_delay_ms(500);
duty +=30;
rpm+=35;
OCR2 = duty;
}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
_delay_ms(500);
duty -=30;
rpm-=35;
OCR2 = duty;
}
}
}
else
{
TCCR0&=(0<<WGM00)&(0<<COM01)&(0<<WGM01)&(0<<CS00);
PORTB&=0xF7;// Reset PB3
}
_delay_ms(50);
}
return(0);
}

Expt-2

Titile: Interface a DC motor through L293D with LCD interface

Circuit:



Code:

#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <compat/deprecated.h>
#include <util/delay.h>
#include "lcd.h"
void pwm_init()
{
// initialize TCCR0 as per requirement, say as follows
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
}
/*void pwm_init1()
{
// initialize TCCR2 as per requirement, say as follows
TCCR2 |= (1<<WGM20)|(1<<COM21)|(1<<WGM21)|(1<<CS20);
}*/
int main()
{
uint8_t lr=35;
uint16_t hr=282, d=0, rpm=35, duty=0;
char buffer[10];
duty = 30; // duty cycle = 45% of 255 = 114.75 = 115
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB = 0x98;
sbi(PINB,0);
sbi(PINB,1);
sbi(PINB,2);
sbi(PINB,5);
sbi(PINB,6);
DDRD|=(1<<PIND7);
PORTB |=(1<<PB4);//set initial rotation direction;
PORTB &=0x7F;//set initial rotation direction;
// make sure to make OC0 pin (pin PD7 for atmega32) as output pin
lcd_init(LCD_DISP_ON_CURSOR);
lcd_clrscr();
lcd_gotoxy(0,0);
_delay_ms(50);
while(1)
{
if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
lcd_clrscr();
_delay_ms(500);
duty +=30;
rpm+=35;
lcd_clrscr();
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");

}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
lcd_clrscr();
_delay_ms(500);
duty -=30;
rpm-=35;
lcd_clrscr();
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
}
else if ((bit_is_clear(PINB,2))&&(bit_is_set(PINB,1)) && (bit_is_clear(PINB,0)))
{

PORTB |=(1<<PB4);//set initial rotation direction;
PORTB &=0x7F;
PORTB|=0x08;// pull up PB3
pwm_init();// initialize timer in PWM mode
OCR0 = duty;
while(1)
{
_delay_ms(50);
//lcd_clrscr();
if(bit_is_clear(PINB,1))
{
break;
}
else if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
lcd_clrscr();
_delay_ms(500);
duty +=30;
rpm+=35;
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
OCR0 = duty;
}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
lcd_clrscr();
_delay_ms(500);
duty -=30;
rpm-=35;
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
OCR0 = duty;
}

}
}
else if((bit_is_clear(PINB,2))&&(bit_is_clear(PINB,1))&&(bit_is_set(PINB,0)))
{

PORTB |=(1<<PB7);//set initial rotation direction;
PORTB &=0xEF;
PORTB|=0x08;// pull up PB3
pwm_init();// initialize timer in PWM mode
OCR0 = duty;
while(1)
{
_delay_ms(50);
if (bit_is_clear(PINB,0))
{
break;
}
else if (bit_is_clear(PINB,5) && bit_is_set(PINB,6) && (duty<230))
{
lcd_clrscr();
_delay_ms(500);
duty +=30;
rpm+=35;
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
OCR2 = duty;
}
else if (bit_is_clear(PINB,6) && bit_is_set(PINB,5) && (duty>40))
{
lcd_clrscr();
_delay_ms(500);
duty -=30;
rpm-=35;
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
OCR2 = duty;
}
}
}
else
{

TCCR0&=(0<<WGM00)&(0<<COM01)&(0<<WGM01)&(0<<CS00);
PORTB&=0xF7;// Reset PB3
}
_delay_ms(100);
lcd_gotoxy(0,0);
lcd_puts("RPM=");
itoa(rpm,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts(" Duty=");
d=((duty*100)/255);
itoa(d,buffer,10); //display RPM value
lcd_puts(buffer);
lcd_puts("%");
lcd_gotoxy(0,1);
itoa(hr,buffer,10);
lcd_puts("HR="); //display HRPM
lcd_puts(buffer);
itoa(lr,buffer,10);
lcd_puts(" LR="); //display LRPM
lcd_puts(buffer);
}
return(0);
}

No comments:

Post a Comment