Waran
Newbie level 5
delaying dc motor
Hi,
This is the coding I did to control a dc motor to run clockwise and anti-clockwise and control the speed of the dc motor.I previously had problem when using 'delay_ms' and I replace it by applying a simple loop to make a delay which is 'wait()' command. The code is error free when I compile using mikroC PRO compiler.But can it work when I burn and apply to a circuit given below?
void wait() {
unsigned int k ;
for(k=0;k<5000;k++);
}
void main (void)
{
TRISA = 0x00; //Initialize these Ports as Outputs
TRISC = 0x00; //This one too
TRISD = 0x00; //This one toooo
PORTA = 0x00; //Initialize PORTA...so the LED is off
PORTC.F0 = 0; //Brake Off
PORTD.F1 = 1; //Direction Forward
PORTD.F1 = 0; //Direction reverse
while(1)
{
if (PORTD.F1 == 1)
{
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001111; // 0x0F Full Speed
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
wait(); //Some delay to see it works with human eye
PORTD.F0 = 0; //Turn Brake Off
}
else if (PORTD.F1 == 0)
{
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001111; // 0x0F (Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
wait();
wait();
wait();
wait(); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
wait(); //Some delay to see it works with human eye
PORTD.F0 = 0; //Turn Brake Off
}
}
}
Hi,
This is the coding I did to control a dc motor to run clockwise and anti-clockwise and control the speed of the dc motor.I previously had problem when using 'delay_ms' and I replace it by applying a simple loop to make a delay which is 'wait()' command. The code is error free when I compile using mikroC PRO compiler.But can it work when I burn and apply to a circuit given below?
void wait() {
unsigned int k ;
for(k=0;k<5000;k++);
}
void main (void)
{
TRISA = 0x00; //Initialize these Ports as Outputs
TRISC = 0x00; //This one too
TRISD = 0x00; //This one toooo
PORTA = 0x00; //Initialize PORTA...so the LED is off
PORTC.F0 = 0; //Brake Off
PORTD.F1 = 1; //Direction Forward
PORTD.F1 = 0; //Direction reverse
while(1)
{
if (PORTD.F1 == 1)
{
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001111; // 0x0F Full Speed
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
wait(); //Some delay to see it works with human eye
PORTD.F0 = 0; //Turn Brake Off
}
else if (PORTD.F1 == 0)
{
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001111; // 0x0F (Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
wait();
wait();
wait();
wait(); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
wait();
wait();
wait();
wait(); //Some delay to see it works with human eye
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
wait(); //Some delay to see it works with human eye
PORTD.F0 = 0; //Turn Brake Off
}
}
}