#include<reg51.h>
sbit sw1=P2^0;
sbit sw2=P2^1;
sbit L293D_A = P1^0; //Make positive of motor
sbit L293D_B = P1^1; //Make negative of motor 0
sbit L293D_E = P1^2; //Enable L293D
void rotate_f(void); //Forward run funtion
void rotate_b(void); //Backward run function
void breaks(void); //Motor stop function
void delay(void); //Some delay
void main(){
P2=0xFF;
P1=0x00;
//Our main function
while(1){ //Infinite loop
if(sw1==1 && sw2==0)
{
rotate_f(); //Run forward
delay(); //Some delay
breaks(); //Stop
delay(); //Some delay
}
if(sw2==1 && sw1==0)
{
rotate_b(); //Run Backwards
delay(); //Some delay
breaks(); //Stop
delay(); //Some delay
}
}
}
void rotate_f() //rotate forward
{
L293D_A = 1; //Make positive of motor 1
L293D_B = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
}
void rotate_b() //rotate backward
{
L293D_A = 0; //Make positive of motor 0
L293D_B = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
}
void breaks() //stop motor
{
L293D_A = 0; //Make positive of motor 0
L293D_B = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
void delay() //Some delay...
{
unsigned char i,j,k;
for(i=0;i<0x20;i++)
for(j=0;j<255;j++)
for(k=0;k<255;k++);
}
you can use these DC motors
but if you want to do such easy task with your microcontroller then you are wasting your resources and money so do take this program as an example only. if you want to drive motor directly with switch you can refer my earlier post.
very clean and neat explanation
ReplyDeletegreat write up! thank you