Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help in C programming for AT89S52

Status
Not open for further replies.

nsw1216

Member level 3
Joined
May 7, 2010
Messages
60
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,667
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.
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;
		}
	}
}
 

I know that '==' has higher precedence than '&&' but often the result is not what you might expect.
You could try to use more brackets to clarify the expression.


Code:
if((p14 == on) && (p15 == off) && (p16 == off) && (p17 == off))  /* forward */ 
      { 
         //Gear(); 
         p30=on; 
         p31=off; 
         p32= off; 
         p33=off; 
      } 
 else if((p14 == off) && (p15 == on) && (p16 == off) && (p17 == off))  /* backward */ 
      { 
         //Gear(); 
         p30=off; 
         p31=on; 
         p32=off; 
         p33=off; 
      } 
 else if(etc.)
 

    nsw1216

    Points: 2
    Helpful Answer Positive Rating
or better u can use SWITCH CASE...
write down case according P2 status...:D
 

What do u meant with write down case according P2 status?
Can you give me a simple example of how to use the switch function ?
 

nsw1216 said:
What do u meant with write down case according P2 status?
Can you give me a simple example of how to use the switch function ?

I think that BurnOut_tesla means something like this:
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){
		switch(P2 & 0x1E){
			case 0x10:
				 //Gear();
				 p30=on;
				 p31=off;
				 p32=off;
				 p33=off;
			break;
			
			case 0x08:
				//Gear();
				p30=off;
				p31=on;
				p32=off;
				p33=off;
			break;
			
			case 0x02:
				 //Gear();
				 p30=off;
				 p31=off;
				 p32=on;
				 p33=off;
			break;
			
			case 0x04:
				//Gear();
				p30=off;
				p31=off;
				p32=off;
				p33=on;
			break;
			
			case 0x00h:
				//Gear();
				p30=off;
				p31=off;
				p32=off;
				p33=off;
			break;
		}
	}
}

The port 2 is:
Code:
P2 = x x x p14 p15 p17 p16 x (So the mask to work just with the bits you need to check is: 00011110b = 0x1Eh)

The if's statements comes to:
Code:
(p14==on) && (p15==off) && (p16==off) && (p17==off) = 00010000b = 0x10h
(p14==off) && (p15==on) && (p16==off) && (p17==off) = 00001000b = 0x08h
(p14==off) && (p15==off) && (p16==on) && (p17==off) = 00000010b = 0x02h
(p14==off) && (p15==off) && (p16==off) && (p17==on) = 00000100b = 0x04h
(p14==off) && (p15==off) && (p16==off) && (p17==off) = 00000000b = 0x00h

Regards.
 

diegobb said:
nsw1216 said:
What do u meant with write down case according P2 status?
Can you give me a simple example of how to use the switch function ?

I think that BurnOut_tesla means something like this:
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){
		switch(P2 & 0x1E){
			case 0x10:
				 //Gear();
				 p30=on;
				 p31=off;
				 p32=off;
				 p33=off;
			break;
			
			case 0x08:
				//Gear();
				p30=off;
				p31=on;
				p32=off;
				p33=off;
			break;
			
			case 0x02:
				 //Gear();
				 p30=off;
				 p31=off;
				 p32=on;
				 p33=off;
			break;
			
			case 0x04:
				//Gear();
				p30=off;
				p31=off;
				p32=off;
				p33=on;
			break;
			
			case 0x00h:
				//Gear();
				p30=off;
				p31=off;
				p32=off;
				p33=off;
			break;
		}
	}
}

The port 2 is:
Code:
P2 = x x x p14 p15 p17 p16 x (So the mask to work just with the bits you need to check is: 00011110b = 0x1Eh)

The if's statements comes to:
Code:
(p14==on) && (p15==off) && (p16==off) && (p17==off) = 00010000b = 0x10h
(p14==off) && (p15==on) && (p16==off) && (p17==off) = 00001000b = 0x08h
(p14==off) && (p15==off) && (p16==on) && (p17==off) = 00000010b = 0x02h
(p14==off) && (p15==off) && (p16==off) && (p17==on) = 00000100b = 0x04h
(p14==off) && (p15==off) && (p16==off) && (p17==off) = 00000000b = 0x00h

Regards.

With the codes above, my dc motor can't even move forward and backward at all.
Any problem with the code above ?
 

Dear diegobb,
I exactly mean the same what u have written...
I thought nsw1216 may able to write down switch statement after that suggestion...
I dont think directly giving code is good idea...
He should atleast try for it..
after that if he is getting stuck then we will guide him in proper way..

Dear nsw1216...
I dont ubderstand why ur mapping ur I/O in such fashion,..
it creates a confusion...
I know it doesnt matter, but it reduces readability of code..
& if ir IF ELSE loop is workinh then this switch statement should also work...
logically there is no difference both code will produce same result...

for moving ur motor u should continually press ur resspective key....
 

Thanks for the comments. Actually i have try out using switch case before asking for the sample code but it still cant work. That's why i ask for the code.

There is another questions about the Port 2 of AT89S52, how's it function ?
If i add one buffer at Port 2, how to control the input and output for port 2 ?
Is it the same manner controlling the Port 0,1,3 ?
 

Yes, its is same as rest ports.

For reading any port u should write '1' to that pin then only read the status of the pin...
It is mentioned in the datasheet...

Added after 2 minutes:

If possible use 4K7Ω resistor as pullup for port 2...
 

    nsw1216

    Points: 2
    Helpful Answer Positive Rating
4K7 ohm ? It is 4.7ohm or 4.7kiloohm ?
 

4K7 means 4.7 kilo ohms and not 4.7 ohms.

4.7ohms will be like this 4E7

Nandhu
 

    nsw1216

    Points: 2
    Helpful Answer Positive Rating
BurnOut_tesla said:
Dear diegobb,
I thought nsw1216 may able to write down switch statement after that suggestion...
I dont think directly giving code is good idea...
I agree with you, but sometimes if somebody gives you a piece of code, with a good explanation, you learn a lot more (and is alot less frustating that search over internet for a code that barely applies in your situation), don't you think?

Best Regards.
 

i totally agree with u....
i just wanted to say that if anybody is suggesting u the idea..
u should try to implement it....
i think nsw has already tried that....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top