/*
FILE: PWM.C
DESC: Control speed and direction of a DC motor using PWM
*/
#include stdio.h
#include stdlib.h
#include dos.h /* outportb, inportb defined here */
#include conio.h /* formatted text functions defined here */
void pwm(int, int, int, int);
void main(void) {
int BASEADDR;
int PORTA, PORTB, PORTC;
int CNTRL;
int DutyCycle;
int SpeedOption;
int Enable, Phase; /* Enable on A.0, Phase on A.1 */
clrscr(); /* clear screen */
window(5,5,75,30); /* set up text window */
gotoxy(1,1); cprintf("Enter Base Address (decimal) e.g. 608 =>\n");
gotoxy(42,1); scanf("%d", &BASEADDR);
PORTA = BASEADDR;
PORTB = BASEADDR + 1;
PORTC = BASEADDR + 2;
CNTRL = BASEADDR+3;
outportb(CNTRL, 128); /* configure all ports for output */
outportb(PORTA, 0); /* motor should be off */
gotoxy(1,3); cprintf("Speed Options");
gotoxy(1,4); cprintf("(f)aster");
gotoxy(1,5); cprintf("(s)lower");
gotoxy(1,6); cprintf("(r)everse direction");
gotoxy(1,7); cprintf("(q)uit");
gotoxy(1,9); cprintf("Selection => ");
gotoxy(14,9);
DutyCycle = 0; /* start off at zero velocity */
Enable = 1;
Phase = 0;
do {
while (!kbhit()) {
pwm(PORTA, DutyCycle, Enable, Phase);
}; /* end of while */
SpeedOption = getch();
switch(SpeedOption) {
case 102 : /* 102 ASCII is f */
/* increase speed by 10% DC */
DutyCycle = DutyCycle + 5;
if(DutyCycle > 100) {
DutyCycle = 100;
gotoxy(1,20); cprintf("Can't go faster");
delay(200);
gotoxy(1,20); cprintf(" ");
};
gotoxy(1,13); cprintf("Going faster...\n");
Enable = 1;
pwm(PORTA, DutyCycle, Enable, Phase);
break;
case 115 : /* 115 ASCII is s */
/* decrease speed by 10% DC */
DutyCycle = DutyCycle - 5;
if(DutyCycle < 0) {
DutyCycle = 0;
gotoxy(1,20); cprintf("Can't go slower");
delay(200);
gotoxy(1,20); cprintf(" ");
};
gotoxy(1,13); cprintf("Going slower...\n");
Enable = 1;
pwm(PORTA, DutyCycle, Enable, Phase);
break;
case 114 : /* 114 ASCII is r */
/* reverse direction at present DC */
Enable = 1;
if(Phase == 0) {
Phase = 2;
} else if(Phase ==2) {
Phase = 0;
};
pwm(PORTA, DutyCycle, Enable, Phase);
gotoxy(1,13); cprintf("Reversing.....\n");
break;
case 113 : /* 113 ASCII is q */
gotoxy(1,22); cprintf("Quitting");
outportb(PORTA, 0);
outportb(PORTB, 0);
outportb(PORTC, 0);
exit(0);
break;
}; /* end of switch */
gotoxy(14,9);
} while(1); /* end of do */
}; /* end of main */
void pwm(int PORTA, int DutyCycle, int Enable, int Phase) {
int OnTime, OffTime, TotalTime;
TotalTime = 100; /* 100 msec */
OnTime = (int)(TotalTime * DutyCycle / 100);
OffTime = (int)(TotalTime - OnTime);
outportb(PORTA, Enable+Phase);
delay(OnTime);
Enable = 0;
outportb(PORTA, Enable+Phase);
delay(OffTime);
gotoxy(1,10); cprintf("Duty Cycle is %3d", DutyCycle);
return;
}; /* end of pwm */
This place is for absolute begineers who are interested in electronics and robotics .Free projects,tutorials,schematics
are available here .You can paricipate here in my forum and
leave messages and comments .
HEADLINES
-
▼
2007
(29)
-
▼
October
(29)
- Guide to a 4-legged walker
- Microcore walker
- Two motor walker robot.
- c program to control speed and direction of DC motor
- Line follower robot circuit schematics
- How To Make One motor walker robot tutorial
- How To Connect Electrical Device to Parallel port
- Remote Control Jammer
- FM -JAMMER
- 5 Band Graphic Equalizer
- Rf serial port link for robot
- Simple three wheel robot with left right direction
- WALKIE /TALKIE CIRCUIT
- 2 channel RF transmitter/reciever
- How to connect motors to lpt port
- C program to control stepper motor.
- How to make eight leg walkerpod.
- H-Bridge circuit.
- PAC-MAN complete project
- Four servo motor control diagram
- 8 RELAY PARALLEL INTERFACE BOARD
- SIMPLE SERVO CONTROL DIAGRAM
- Simplified Output Interface Using parallel port
- The Simplified I/O Interface using parallel port
- WIRELESS AUDIO RECIEVER /TRANSMITTER
- RADIO CONTROL SWITCHES CIRCUIT SCHEMATICS
- CIRCUIT DIAGRAM TO CONNECT A DEVICE TO LPT PORT
- Simple C program to control computer parallel port
- PICTURES OF MY SPIDE-BOT
-
▼
October
(29)
LATEST ENTRY
c program to control speed and direction of DC motor
Participate in my new msg forum and share projects and Tutorials
MESSAGE BOX -- subscribe below to get updates
IF ANYONE WANT TO GET A PERSONAL HELP OR WANT TO BUY A PROGRAM OR ROBOT CAN MAIL ME.
PLEASE POST UR MESSAGES OR REQUESTS IN MESSAGE BOX
OR MAIL ME AT .... abhishekrobotics@gmail.com
OR MAIL ME AT .... abhishekrobotics@gmail.com
Augmented reality onboard the ISS? 14 Oct 2007 17:11, Tech blogs on ZDnet
Massachusetts high schools vying to update old science labs 14 Oct 2007 12:30, The Boston Globe Online
Robots vie in fight of racks, ringers 14 Oct 2007 10:50, Contra Costa Times
Teens build, enter machines in contest 14 Oct 2007 10:11, San Mateo County Times
New NASA software to ease astronauts' tasks in adverse space conditions 14 Oct 2007 08:11, Big News Network