ather_88
Newbie level 5
- Joined
- May 22, 2014
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 90
#include<16f877a.h>
#fuses HS,NOWDT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#define M1_1 PIN_B0 //left motor 1 Fwd
#define M1_2 PIN_B1 //left motor 1 Rev
#define M2_1 PIN_B2 //left motor 2 Fwd
#define M2_2 PIN_B3 //left motor 2 Rev
#define M3_1 PIN_B4 //Right motor 1 Fwd
#define M3_2 PIN_B5 //Right motor 1 Rev
#define M4_1 PIN_B6 //Right motor 2 Fwd
#define M4_2 PIN_B7 //Right motor 2 Rev
#define trig PIN_D6
//trig = PIN_RD6 //pin 29 OUTPUT
//Echo = PIN_RD7 //pin 30 INPUT
void stop();
void forward();
void reverse();
void right();
void left();
void ultrasonic();
void chk_lmt_swt();
int going_forward;
int going_reverse;
int going_right;
int going_left;
void chk_lmt_swt()
{
if(!input(PIN_D0) || !input(PIN_D1))
{
stop();
}
}
void ultrasonic()
{
int16 width;
int16 range=0.00;
width=0;
range=0;
output_high(trig); //send pulse with a 10us width
delay_us(20);
output_low(trig);
while (!input(PIN_D7)) //wait until an echo is received
{
}
while (input(PIN_D7)) //calculate the width of the echo received
{
delay_us(10);
width++;
}
range=(width*10)/58; //calculate range
range=range/2;
if(range<5)
{
stop();
output_high(PIN_B6);
}
}
void forward()
{
output_high(M1_1);
output_high(M2_1);
output_high(M3_1);
output_high(M4_1);
going_forward =1;
}
void reverse()
{
output_high(M1_2);
output_high(M2_2);
output_high(M3_2);
output_high(M4_2);
going_reverse=1;
chk_lmt_swt();
}
void right()
{
output_high(M1_1);
output_high(M2_1);
output_low(M3_1);
output_low(M4_1);
going_right = 1;
}
void left()
{
output_high(M3_1);
output_high(M4_1);
output_low(M1_1);
output_low(M2_1);
going_left=1;
}
void stop()
{
output_low(M1_1);
output_low(M2_1);
output_low(M3_1);
output_low(M4_1);
output_low(M1_2);
output_low(M2_2);
output_low(M3_2);
output_low(M4_2);
going_forward=0;
going_reverse=0;
going_right=0;
going_left=0;
delay_ms(500);
}
void main()
{
char data;
set_tris_d(0x80);
output_high(PIN_B7);
stop();
while(1)
{
data=getc();
switch(data)
{
case 0x57: // for forward W
if(going_reverse==1||going_right==1||going_left==1)
stop();
forward();
// if(going_forward==1)
// ultrasonic();
break;
case 0x53: // for Reverse S
if(going_forward==1||going_left==1||going_right==1)
{
stop();
reverse();
}
else
if(going_reverse==1)
chk_lmt_swt();
break;
case 0x41: //for Left A
if(going_forward==1||going_reverse==1||going_right==1)
stop();
//if (going_left==1)
left();
break;
case 0x44: //FOR RIGHT D
if(going_forward==1||going_left==1||going_reverse==1)
stop();
right();
break;
case 0x58: //for STOP X
stop();
}
}
}
void chk_lmt_swt()
{
printf("D0 %d D1 %d\n", input(PIN_D0) ,input(PIN_D1)));
if(!input(PIN_D0) || !input(PIN_D1))
{
stop();printf("Stopped\n");
}
}
void chk_lmt_swt()
{
if [B]([/B](!input(PIN_D0) ||[B] ([/B]!input(PIN_D1))
{
stop();
}
}
! has a higher precedence than || therefore if input(PIN_D0) OR input(PIN_D1) is false, i.e. 0, stop() should be calledhello
Problem of parenthesis misplaced
Code:void chk_lmt_swt() { if [B]([/B](!input(PIN_D0) ||[B] ([/B]!input(PIN_D1)) { stop(); } }
void chk_lmt_swt()
{
if(!input(PIN_D0) || !input(PIN_D1))
{
stop();
}
}
In programming, i allways learn that the number of opened parenthesis must be equal to the closed parenthesis
to avoid erratic behavior.
Did the rules changed ?
are they some warning in compilation logging ?
if(!input(PIN_D0) || !input(PIN_D1))
the number of ( balances the number of ) or am I reading it wrong
Yes, you're right..
i was just disturbed by this kind of writting..
.
set_tris_d(0x80);
set_tris_d(0x83);
set_tris_d(0x83);
case 0x53: // for Reverse S
if(going_forward==1||going_left==1||going_right==1)
{
stop();
reverse();
}
else
[B] {[/B]
if(going_reverse==1)
chk_lmt_swt();
}
break;
I tend to use hall effect switches in situations where switch bounce is a problem.i also used limit switch and some the time problem was bcoz of noise only.
so i think you should use 0.1uf capacitor across both end of limit switch.
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?