Ishah
Member level 1
- Joined
- Mar 13, 2013
- Messages
- 33
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,502
Hi everyone,
I need your help to fix my code. I want to make project security sliding door. When the user push one button the motor will run,at the same time when the user push the button again,the motor will stop. Another case, while the motor is running,the sensor triggered and the motor will stop.
Here is my Proteus circuit and my code. I am lack of programming, but I try to learn it seem it look messy. Can anyone give some example?
My problem is the motor automatically run,stop,run without selecting any button.
Then if I make some changes
The motor static.
Here are the result:
For simulation, I used the switches as sensor.
I need your help to fix my code. I want to make project security sliding door. When the user push one button the motor will run,at the same time when the user push the button again,the motor will stop. Another case, while the motor is running,the sensor triggered and the motor will stop.
Here is my Proteus circuit and my code. I am lack of programming, but I try to learn it seem it look messy. Can anyone give some example?
My problem is the motor automatically run,stop,run without selecting any button.
Code:
/*
Project Title: Controlled Motor by switches and sensor
*/
void main()
{
TRISA=0XFF; //Configure all bit of PORTA as input
TRISB=0XFF; //Configure all bit of PORTB as input
TRISD=0X00; //Configure all bit of PORTD as output
PORTA=0X00; // Clear bits to zero
PORTB=0X00; // Clear bits to zero
PORTD=0X00; // Clear bits to zero
CMCON=0X07; // or CMCON register = 0X00000111
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
do
{
if(PORTB.F0 == 1) //If the switch is pressed
{
PORTD.F0 = 0; // Motor 1 clockwise
PORTD.F1 = 1;
PORTD.F2 = 0; // Motor 2 clockwise
PORTD.F3 = 1;
Delay_ms(1000); // Delay 100 ms
}
else
if (PORTB.F1 == 1) //If the switch is pressed
{
if(PORTB.F1 == 1) //If the switch is pressed
PORTD.F0 = 1; // Motor 1 anti-clockwise
PORTD.F1 = 0;
PORTD.F2 = 1; // Motor 2 anti-clockwise
PORTD.F3 = 0;
Delay_ms(1000); // Delay 100 ms
}
else
{
PORTD.F0 = 1; // Motor stop
PORTD.F1 = 1;
PORTD.F2 = 1; // Motor stop
PORTD.F3 = 1;
Delay_ms(500); // Delay 1000 ms
}
if(PORTA.F0 == 1) //If the sensor triggered
{
PORTD.F0 = 0; // Motor 1 stop
PORTD.F1 = 0;
PORTD.F2 = 0; // Motor 2 stop
PORTD.F3 = 0;
Delay_ms(500); // Delay 100 ms
}
else
if(PORTA.F1 == 1) //If the sensor triggered
{
PORTD.F0 = 0; // Motor 1 stop
PORTD.F1 = 0;
PORTD.F2 = 0; // Motor 2 stop
PORTD.F3 = 0;
Delay_ms(500); // Delay 100 ms
}
else
{
PORTD.F0 = 1; // Motor 1 keep run
PORTD.F1 = 1;
PORTD.F2 = 1; // Motor 2 keep run
PORTD.F3 = 1;
Delay_ms(500); // Delay 100 ms
}
}
while(1);
}
Code:
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
while(1);
{
if(PORTB.F0 == 1) //If the switch is pressed .................................
......................................
......................................
Delay_ms(500); // Delay 100 ms
}
}
}
Here are the result:
For simulation, I used the switches as sensor.