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
/*
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);
}
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
while(1);
{
if(PORTB.F0 == 1) //If the switch is pressed .................................
......................................
......................................
Delay_ms(500); // Delay 100 ms
}
}
}
/*
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([COLOR="#FF0000"]PORTB.F0 == 0[/COLOR]) //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 ([COLOR="#FF0000"]PORTB.F1 == 0[/COLOR]) //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);
}
ADCON1=0X07; // Configure ADCON1, or ADCON1=0X06;
while(1);
{
if([COLOR="#FF0000"]PORTB.F0 == 0[/COLOR]) //If the switch is pressed .................................
......................................
......................................
Delay_ms(500); // Delay 100 ms
}
}
}
Two ways to go about correcting this:
1. Change the connection to idle LOW for the PORTB pins (You'll have to come up with a different connection)
2. (Recommended) Change your code to idle HIGH while you retain the existing connection
/*
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 == 0) //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(500); // Delay 500 ms
}
else
if (PORTB.F1 == 0) //If the switch is pressed
{
if(PORTB.F1 == 0) //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(500); // Delay 500 ms
}
else
{
PORTD.F0 = 1; // Motor stop
PORTD.F1 = 1;
PORTD.F2 = 1; // Motor stop
PORTD.F3 = 1;
Delay_ms(500); // Delay 500 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 500 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 500 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 500 ms
}
}
while (1);
}
With changes mentioned by Ibkay add 50 ms debounce delays for the switches.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #define SENSORPORT PORTA #define SWITCHPORT PORTB #define MOTORPORT PORTD unsigned short toggle = 0; 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; while(1){ if((SENSORPORT & 0x07) == 0x00){ if(((SWITCHPORT & 0x07) == 0x06) ){ //SW1 pressed Delay_ms(50); if((SWITCHPORT & 0x07) == 0x06){ MOTORPORT = (MOTORPORT & 0xF0) | 0x0A; //Motor 1 & 2 CW } } else if(((SWITCHPORT & 0x07) == 0x05)){ //SW2 pressed Delay_ms(50); if((SWITCHPORT & 0x07) == 0x05){ MOTORPORT = (MOTORPORT & 0xF0) | 0x05; //Motor 1 & 2 CCW } } else if(((SWITCHPORT & 0x07) == 0x03)){ //SW3 pressed Delay_ms(50); if((SWITCHPORT & 0x07) == 0x03){ MOTORPORT = (MOTORPORT & 0xF0); //Motor 1 & 2 Stop } } } else if(((SENSORPORT & 0x07) == 0x01) || ((SENSORPORT & 0x07) == 0x02) || ((SENSORPORT & 0x07) == 0x04))){ MOTORPORT = (MOTORPORT & 0xF0); } } }
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.
//define motor1 ports
sbit motor1a at RD0_bit;
sbit motor1b at RD1_bit;
//define motor2 ports
sbit motor2a at RD2_bit;
sbit motor2b at RD3_bit;
//define sensor ports
#define SENSOR PORTA
//define switches ports
#define PB1 PORTB.F0
#define PB2 PORTB.F1
#define PB3 PORTB.F2
#define PB4 PORTB.F3
unsigned short toggle = 0;
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;
while(1){
if((SENSOR & 0x07) == 0x00)
{
if(PB1 == 1) //If the push button 1 is pressed
{
Delay_ms(50); // Delay 100 ms
if(PB1 == 1)
{
motor1a = 1; // Motor 1 clockwise
motor1b = 0;
motor2a = 1; // Motor 2 clockwise
motor2b = 0;
}
}
else
if (PB2 == 1) //If the push button 2 is pressed
{
Delay_ms(50); // Delay 100 ms
if(PB2 == 1)
{
if(PB2 == 1)
motor1a = 0; // Motor 1 counter clockwise
motor1b = 1;
motor2a = 0; // Motor 2 counter clockwise
motor2b = 1;
}
}
else
if (PB3 == 1) //If the push button 3 is pressed
{
Delay_ms(50); // Delay 100 ms
if(PB3 == 1)
{
motor1a = 1; // Motor 1 clockwise
motor1b = 0;
motor2a = 1; // Motor 2 clockwise
motor2b = 0;
}
}
else
if (PB4 == 1) //If the push button 4 is pressed
{
Delay_ms(50); // Delay 100 ms
if(PB4 == 1)
{
motor1a = 0; // Motor 1 counter clockwise
motor1b = 1;
motor2a = 0; // Motor 2 counter clockwise
motor2b = 1;
}
}
}
else if(((SENSOR & 0x07) == 0x01) || ((SENSOR & 0x07) == 0x02) || ((SENSOR & 0x07) == 0x04))
{
motor1a = 0; // Motor 1 stop
motor1b = 0;
motor2a = 0; // Motor 2 stop
motor2b = 0;
}
}
}
Replace s1, s2, s3, connected to R4, R5, R6 in your circuit with output pins. You have used input pins for them. You have to use one input pin and one output pin which make a pair for connection.
Your image and expanation was not helpful. PBx are buttons in image? Just explain the working of 3 buttons ins your circuit. Earlier you said
You want 3 buttons or 2 buttons?
If three buttons - 1st button starts or stops the motors, 2nd button will turn motors CW, 3rd button turn the motors CCW.
If 2 buttons - 1st button starts or stops motors, 2nd button change direction to CW or CCW .
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?