Beruflich Dokumente
Kultur Dokumente
ATmega32 microcontroller
Crystal
Capacitors
Motors
L293D IC
+5V supply
Breadboard
Connecting Wires
Motor Controller
If speed control using PWM is not needed, the Enable Pins can be directly connected
to +5V.
Circuit Diagram
Description
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.
Mikrokontroler ATmega32
Kristal
Kapasitor
Motor
L293D IC
+ 5V supply
Papan breadboard
Menghubungkan Kabel
Motor Pengendali
Jika kontrol kecepatan menggunakan PWM tidak diperlukan, Aktifkan Pins dapat
langsung terhubung ke + 5V.
Circuit Diagram
Deskripsi
OUT1 dan OUT2 dihubungkan ke terminal motor pertama dan OUT3 dan OUT4
yang terhubung ke terminal motor kedua.
Sebuah kristal terhubung ke XTAL1 dan XTAL2 pin untuk memberikan pulsa clock.
Reset terhubung ke + 5V.
Kode sumber
/ * Kode berikut akan membuat motor untuk berjalan ke depan selama 2 detik
dan kemudian mundur selama 2 detik untuk waktu yang tak terbatas * /
#include <avr / io.h>
#include <util / delay.h>
#define F_CPU 1000000UL
int main ()
{
DDRC = 0xFF; // PORTC sebagai port output
PORTC = 0x00; // Awalnya semua pin sebagai output yang rendah
PORTC | = (1 << PC0) | (1 << PC3); // C0 dan C3 terhubung untuk mengaktifkan
while (1) // Infinite Loop
{
/ * Teruskan Gerak * /
PORTC | = (1 << PC1) | (1 << PC4); // # 1
PORTC & = ~ ((1 << PC2) | (1 << PC5)); // # 2
_delay_ms (2000); // # 3
/ * Gerak Mundur * /