Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
dc motor speed control
#include<reg51.h>
void delay_us(unsigned int us_count)
{
while((us_count*4)!=0)
{
us_count--;
}
}
#ifndef delay_H
#define delay_H
void delay_us(unsigned int us_count);
#endif
/*
Four quad motor speed controller
8051 and L293D , need LCD as well
Clock Wise
Anti clock wise
forward brake
reverse brake
Speed Control,Speed control is done using PWM
LCD is connected to port 2
Register select pin connected to P2.0
Read Write pin connected to P2.1
Enable pin connected to P2.2
Hiher 4 bits of P2 to the Higher 4 bits of LCD (P2.4 to D4 ...)
Switches - port 1
p1.0 - clock wise
p1.1 - Anti clokc wise
P1.2 - Break Clockwise
P1.3 - Break anti clokc wise
P1.4- Speed increase
P1.5- Speed decrease
L293D
in1 - p0.7
in2 - p0.6
enable -p0.5
TMOD - Timer mode
Gate -0,
mode 1, (M0-0,M1-1)
TCON- timer control
TR0, timer 0 run control bit, set 1 to start the timer,0 to stop the timer
TL0,TH0,TL1,TH1
*/
#include <reg51.h>
#include "lcd.h"
#include "delay.h"
sbit c=P1^0;
sbit ac=P1^1;
sbit bc=P1^2;
sbit bac=P1^3;
sbit si=P1^4;
sbit sd=P1^5;
sbit m1_p=P0^7 ; //motor 1 positive
sbit m1_n=P0^6 ;//motor 1 negative
sbit m1_c=P0^5; //motor 1 control (enable)
float speed=100; //(0 to 100)
float cnst=6000;
int break_delay=1000;
//motor control functions
void m1_stop(){
m1_p=0;
m1_n=0;
}
void m1_clock(){
m1_p=1;
m1_n=0;
}
void m1_antiClock(){
m1_p=0;
m1_n=1;
}
void m1_break_clock(){
int i=0;
m1_antiClock();
for(i=0;i<break_delay;i++);
m1_stop();
}
void m1_break_antiClock(){
int i=0;
m1_clock();
for(i=0;i<break_delay;i++);
m1_stop();
}
void checkKeyBoard(){//by default all keys are high
if(!c){
m1_clock();
LCD_Clrscr();
LCD_DisplayString("Dir: Clock");
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
else if(!ac){
m1_antiClock();
LCD_Clrscr();
LCD_DisplayString("Dir: AntiClock");
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
else if(!bc){
m1_break_clock();
LCD_Clrscr();
LCD_DisplayString("Dir: fwd break");
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
else if(!bac){
m1_break_antiClock();
LCD_Clrscr();
LCD_DisplayString("Dir: Rvr Break");
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
else if(!si){
speed++;
if(speed>100){
speed=100;
}
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
else if(!sd){
if(speed>0){
speed--;
}
LCD_Gotoxy(0,1);
LCD_DisplayString("Speed: ");
LCD_DisplayNumber(speed);
}
}
int main(){
int pwm_1,pwm_0;
//switch of the motor
m1_c=0;
m1_stop();
LCD_Init();
LCD_Clrscr();
LCD_DisplayString("--welcome--");
while(1){
//LCD_Gotoxy(0,1);
//LCD_Clrscr();
pwm_1= (int)(cnst*(speed/100.0));
pwm_0= (int)(cnst*((100-speed)/100.0));
m1_c=1;
//LCD_DisplayNumber(pwm_1);
//LCD_DisplayString(",");
delay_us(pwm_1);
m1_c=0;
//LCD_DisplayNumber(pwm_0);
delay_us(pwm_0);
checkKeyBoard();
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment