nsw1216
Member level 3
Help in C programming
I want to create a programmed which take 4 inputs from a PS2 controller and produce 4 outputs to the motors which is forward, backward, left and right.
I used the "if-else" function to set the output for each input but my MCU (AT89S52) does not produce any output for the left and right "if-else" loop. However, it produced outputs for forward and backward "if-else".
Anyone know what is the problem of my program attached below ?
Your opinion will be welcome and appreciated.
I want to create a programmed which take 4 inputs from a PS2 controller and produce 4 outputs to the motors which is forward, backward, left and right.
I used the "if-else" function to set the output for each input but my MCU (AT89S52) does not produce any output for the left and right "if-else" loop. However, it produced outputs for forward and backward "if-else".
Anyone know what is the problem of my program attached below ?
Your opinion will be welcome and appreciated.
Code:
#include<reg51.h>
sbit p14 = P2^4; //ps2-move-f
sbit p15 = P2^3; //ps2-move-b
sbit p16 = P2^1; //ps2-move-r
sbit p17 = P2^2; //ps2-move-l
sbit p30 = P3^0; //move-f
sbit p31 = P1^5; //move-b
sbit p32 = P1^6; //move-r
sbit p33 = P1^7; //move-l
bit on=0;
bit off=1;
void main()
{
while(1)
{
if(p14==on&&p15==off&&p16==off&&p17==off)//forward
{
//Gear();
p30=on;
p31=off;
p32=off;
p33=off;
}
if(p14==off&&p15==on&&p16==off&&p17==off)//backward
{
//Gear();
p30=off;
p31=on;
p32=off;
p33=off;
}
if(p14==off&&p15==off&&p16==on&&p17==off)//right
{
//Gear();
p30=off;
p31=off;
p32=on;
p33=off;
}
if(p14==off&&p15==off&&p16==off&&p17==on)//left
{
//Gear();
p30=off;
p31=off;
p32=off;
p33=on;
}
if(p14==off&&p15==off&&p16==off&&p17==off)
{
//Gear();
p30=off;
p31=off;
p32=off;
p33=off;
}
}
}