Components Required
- ATmega32 microcontroller
- AVR Programmer Board
- Crystal
- Capacitors
- Motors
- L293D IC
- +5V supply
- Battery equivalent to voltage rating of motor
- Breadboard
- Connecting Wires
Motor Controller
L293D - Dual D.C. Motor Controller
- Enable-1 controls the output of output1 and output2.
- Enable-2 controls the output of output3 and output4.
- Vss is connected to +5V.
- Vs is connected to a voltage equal to the voltage rating of the motor.
- Enable 1-2 and Input 1-4 comes from the microcontroller.
- If speed control using PWM is not needed, the Enable Pins can be directly connected to +5V.
- All 4 grounds are shorted to a common ground.
- Output 1-4 goes to the motor.
Circuit Diagram
Description
- PC0 and PC3 are connected to Enable 1 and Enable 2 respectively.
- PC1,PC2,PC4 and PC5 are connected to Input 1-4.
- OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.
- A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
- Reset is connected to +5V.
Source Code
/*The following code will make the motor to run forward for 2 seconds
and then backward for 2 seconds for infinite time*/
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL
int main()
{
DDRC = 0xFF; // PORTC as output port
PORTC = 0x00; //Initially all pins as output low
PORTC|=(1<<PC0)|(1<<PC3); //C0 and C3 are connected to enable
while (1) // Infinite Loop
{
/*Forward Motion*/
PORTC|=(1<<PC1)|(1<<PC4); //#1
PORTC&=~((1<<PC2)|(1<<PC5)); //#2
_delay_ms(2000); //#3
/*Backward Motion*/
PORTC&=~((1<<PC1)|(1<<PC4)); //#4
PORTC|=(1<<PC2)|(1<<PC5); //#5
_delay_ms(2000); //#6
}
return 0;
}
# 1 . PORTC|=(1<<PC1)|(1<<PC4);
1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
Taking OR of this values will generate 00010010. Now OR of this value and the initial condition of PORTC will make the Bits 1 and 4 as high independent of all other pins of PORTC.
# 2 . PORTC&=~((1<<PC2)|(1<<PC5));
1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
Taking OR of this values will generate 00100100 and NOT of this value will generate 11011011. Now AND of this value and the initial condition of PORTC will make the Bits 2 and 5 as low independent of all other pins of PORTC.
# 3. _delay_ms(2000);
A delay of 2 seconds after which the motor will change its direction.
# 4 . PORTC&=~((1<<PC1)|(1<<PC4));
1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
Taking OR of this values will generate 00010010 and NOT of this value will generate 11101101. Now AND of this value and the initial condition of PORTC will make the Bits 1 and 4 as low independent of all other pins of PORTC.
# 5 . PORTC|=(1<<PC2)|(1<<PC5);
1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
Taking OR of this values will generate 00100100. Now OR of this value and the initial condition of PORTC will make the Bits 2 and 5 as high independent of all other pins of PORTC.
# 6. _delay_ms(2000);
A delay of 2 seconds after which the motor will change its direction.
Source Code for Normal Run of DC motor
#include <avr/io.h>
#define F_CPU 1000000UL
int main()
{
DDRC = 0xFF;
PORTC = 0x00;
PORTC|=(1<<PC0)|(1<<PC3); //C0 and C3 are connected to enable
PORTC|=(1<<PC1)|(1<<PC4); //C1 and C4 as output high
PORTC&=~((1<<PC2)|(1<<PC5)); //C2 and C5 as output low
while(1) // Infinite Loop
;
return 0;
}
ThankYou for Reading.
No comments:
Post a Comment