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.

single key using two condition at 89s52

Status
Not open for further replies.

vijayaraja

Newbie level 6
Joined
Sep 1, 2011
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,399
Hello,
I have to do a key is pressed once led will start to glow and continuously running the codition,when a key is pressed again led will off condition, how to write a program for 89c52 no experience with 8051 need help.
 

easy.
1.
Check if key is pressed.
loop until key is pressed.

2.
when the key is pressed, toggle the pin connected to the led.
go to 1

also, you should specify what code you are working with
(i.e. assembly or C)
 

This is a sample C code, but you should have delay_ms() function in your library or else you have to write the delay routine.

Code:
sbit SW = P1^1
sbit LED = P1^2

void main() {

	if(SW == 1) {
		delay_ms(150);
		if(SW == 1) {
			LED = ~LED;
		}
	}
}
 

This is a sample C code, but you should have delay_ms() function in your library or else you have to write the delay routine.

Code:
sbit SW = P1^1
sbit LED = P1^2

void main() {

	if(SW == 1) {
		delay_ms(150);
		if(SW == 1) {
			LED = ~LED;
		}
	}
}

thats nice... but i want a program for input side only one switch is connected(i want input is given hex code format), output side 8 led connected if key is pressed 8 led will glow keep on continuous, when key is pressed again 8 led will turn off.. for example


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<reg52.h>
#define input P0
#define output P1
void main()
{
if(input==0x01)
{
output=0xff;
if(input==0x01)
{
output=0x00; 
}
}
}



the above code is automatically will go to off state pls solve the problem

- - - Updated - - -

easy.
1.
Check if key is pressed.
loop until key is pressed.

2.
when the key is pressed, toggle the pin connected to the led.
go to 1

also, you should specify what code you are working with
(i.e. assembly or C)


thats nice... but i want a program for input is given hex code format, output side 8 led connected if key is pressed 8 led will glow keep on continuous, when key is pressed again 8 led will turn off.. for example


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<reg52.h>
#define input P0
#define output P1
void main()
{
if(input==0x01)
{
output=0xff;
if(input==0x01)
{
output=0x00; 
}
}
}



the above code is automatically will go to off state pls solve the problem
 
Last edited by a moderator:

Try this code

Code:
#define LEDPORT P2
sbit SW = P1^0


void main() {
  while(1) {
			if(SW == 0x01) {
			delay_ms(150);
					if(SW == 0x01) {
					LEDPORT = ~LEDPORT;
					}
			}
}
}
 
Last edited:
thank you sir, 8 led(port 1) controlled by 8 switches(port0) respectively if key one pressed means first led will glow, pressed again led off when second key is pressed second led only will off.. help me
 

Do you mean to say, if sw1 pressed led1 will turn ON, if sw1 is pressed again led1 will turn OFF.
Similarly, if sw2 is pressed led2 will turn ON, if sw2 is pressed gain led2 will turn OFF. Is it what you want? same thing for 8 sw and 8 leds?
Can't you use P1 for Switches and P2 or P3 for LEDs?

Code:
#include<reg52.h>

void delay_ms(unsigned int time);

sbit SW1 = P1^0
sbit SW2 = P1^1
sbit SW3 = P1^2
sbit SW4 = P1^3
sbit SW5 = P1^4
sbit SW6 = P1^5
sbit SW7 = P1^6
sbit SW8 = P1^7

sbit LED1 = P2^0
sbit LED2 = P2^1
sbit LED3 = P2^2
sbit LED4 = P2^3
sbit LED5 = P2^4
sbit LED6 = P2^5
sbit LED7 = P2^6
sbit LED8 = P2^7

void delay_ms(unsigned int msec) {

	  unsigned int x,y;
                for(x=0; x<=msec;x++) {
                         for(y=0;y<=110;y++);
                }

}

void main() {

  	while(1) {
			
		if(SW1 == 0x01) {
		    delay_ms(150);
	            if(SW1 == 0x01) {
			  LED1 = ~LED1;
		    }
		}
		else if(SW2 == 0x01) {
		    delay_ms(150);
	            if(SW2 == 0x01) {
			  LED2 = ~LED2;
		    }
		}
		else if(SW3 == 0x01) {
		    delay_ms(150);
	            if(SW3 == 0x01) {
			  LED3 = ~LED3;
		    }
		}
		else if(SW4 == 0x01) {
		    delay_ms(150);
	            if(SW4 == 0x01) {
			  LED4 = ~LED4;
		    }
		}
		else if(SW5 == 0x01) {
		    delay_ms(150);
	            if(SW5 == 0x01) {
			  LED5 = ~LED5;
		    }
		}
		else if(SW6 == 0x01) {
		    delay_ms(150);
	            if(SW6 == 0x01) {
			  LED6 = ~LED6;
		    }
		}
		else if(SW7 == 0x01) {
		    delay_ms(150);
	            if(SW7 == 0x01) {
			  LED7 = ~LED7;
		    }
		}
		else if(SW8 == 0x01) {
		    delay_ms(150);
	            if(SW8 == 0x01) {
			  LED8 = ~LED8;
		    }
		}

	}
}
 
Last edited:

    V

    Points: 2
    Helpful Answer Positive Rating
@ vijayaraja you did not indicate your preference in terms of language.... do you prefer a C-code, Basic or Assembly? because you will end up having so many response that may confuse you....
 

sir i want program in embedded c only.. need a program for input data hex code format instead of key pressing,
if we given input dat hex code format the corresponding led will glow..
0x01 means first led will glow keep on continuous and again given the same input led will off,
0x02 means second led so and so 0x08 means eight led ll glow,again given led will off
 

Do you mean to say, if 0x01 is given led1 will glow and is 0x01 is again given led1 will turn OFF. If 0x02 is given led1 and led2 will turn ON and if 0x02 is given again led1 and led2 will turn OFF?

Are you using momentary buttons are spst sw? You cannot give 0x02 if you are using momentary switches. If you press Sw1 or sw2 or any sw for a moment then that pin will go high for a moment. If you are pressing sw3 then sw1 and sw2 will be OFF, so P1^0 and P1^1 will be 0 and P1^2 will be 1 then the value will be 0x4 and not 0x03. 0x03 means 11, so doyou want to toggle led3 when sw1 and sw2 are pressed together or do you want to toggle led3 when P1 is 0x04?

your method is like this

0x00 0b00000000
0x01 0b00000001
0x02 0b00000010
0x03 0b00000011
0x04 0b00000100
0x05 0b00000101
0x06 0b00000110
0x07 0b00000111
0x08 0b00001000

what you want is if you are using momentary switches is

0x01 0b00000001
0x02 0b00000010
0x04 0b00000100
0x08 0b00001000
0x10 0b00010000
0x20 0b00100000
0x40 0b01000000
0x80 0b10000000
 
Last edited:

sir i am doing inter telecom project using 89c52, in that 100 station individual call,
group call,all call and random call. i was designed 100 inputs given using
matrix keypad(P1 and P3) to master microcontroller,
if we pressed key 1 means uc will generate 0x00 hex code in port0,
key 2 will generate 0x01,key 3 will generate 0x02,
key 4 will generate 0x03, like this going if we pressed key 100 means 0x7f will generate in port 0.
output design i was designed one slave uc(89c52)
master uc P0 data given to the slave uc input(P0), P1,P2& P3 using output purpose
so key 1 pressed means corresponding hex code(0x00) given input data to slave uc, i need program for slave uc below the coditions
Code:
#include<reg52.h>
#define input P0 //from Master Uc
#define parallel P1 
#define leone P2 //control upto 1-8 latch
#define letwo P3 //control upto 8-16 latch
void main()
{
parallel=0x00;// intial low
leone=0x00;  // intial low
letwo=0x00;  // intial low
while(1)
{
if(input==0x00) // if key pressed once
{
parallel=0x01;
leone=0x01;
letwo=0x00;
}
if(input=0x00) //again the same input is given(the same key pressed again)
{
parallel=0x00;
}
}
}
i was tried using switch case but i cant get any idea, i wrote a program for master uc,
i need a program for slave uc above the concept. pls help me
 
Last edited:

are you familiar with assembly coding?....if yes i can post the code here for your look up
 

ya i know assemble language also,,, please post that code, if any other possible to wright c code?
 

Are there hundred keys in your circuit? Is it a 10x10 matrix?
 

first of all you said you want keys from (0 to 9) on a port
secondly you want it to output the binary value if pressed say from (0 to 100) on a port.
should there be a facility for enter or clear?...because it will make it more flexible
 

did you got my concept? i dont have circuit in orcad design, i have design in paper only
 

I didn't get your concept, but what I am asking is are you using a 10X10 matrix keypad?

Can you explain to which ports the switches and leds are connected in master circuit and slave circuit?

I think I can do it in C, but if you explain about your circuit.
 
Last edited:

if you have your design on paper...please for clarity you can snap with your phone or camera.....then upload it here so that one will not be confused
 

ya.. thats not a problem. master uc given input data(hex code format) instead of key, in slave uc get the input from master uc, if get (0x00) means port 1 will generate 0x01,at the same time P2=0x01 and P3=0x00.when slave uc again get the data(0x00) means uc will generate P1=0x00 only

- - - Updated - - -

can i send the circuit in paint?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top