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.

counter program on led

Status
Not open for further replies.

suga

Member level 2
Joined
Jan 23, 2012
Messages
49
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
chennai
Activity points
1,596
hi
I wrote a program counter on led.in that counter program is running in between i have to stop by pressing a switch for this i wrote this code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
void main()
{
 
//
 
for(p0=0x00;p0<=0xf0;p0++)
{
wait(500);
if(c1=0)
 break;
}
}


Here c1 act as switch,p0 lcd port.but output is not coming
 
Last edited by a moderator:

ya i changed that c1==0 even though output not coming.once I pressed switch na again its started to count.not a stop or pause. this is my code.
Code:
void main()
{
for(p1=0x00;p1<=0xf0;p1++)
{
wait(500);
if(c1==0)

break;


 
}
 

hi
I wrote a program counter on led.in that counter program is running in between i have to stop by pressing a switch for this i wrote this code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
void main()
{
 
//
 
for(p0=0x00;p0<=0xf0;p0++)
{
wait(500);
if(c1=0) //<---------------c=0 is a an assignment statement
 break;
}
}


Here c1 act as switch,p0 lcd port.but output is not coming

Is this a complete code?
check:-
1. Have you made the port configured as output where LEDS are connected?
2. Have you made the pin configured as input where the switch is connecte?
3. How the LEDs and the switch are connected? (using pull -up of pull -down resistors)?

Also,
c=0 is a an assignment statement
 

this is my complete code
Code:
#include<reg51.h>
#define p1 P1
#define p2 P2    
sbit c1=P3^2;
void main()
{
for(p1=0x00;p1<=0xf0;p1++)
{
wait(500);
if(c1==0)

break;


 
}
 

this is my complete code
Code:
#include<reg51.h>
#define p1 P1
#define p2 P2    
sbit c1=P3^2;
void main()
{
for(p1=0x00;p1<=0xf0;p1++)
{
wait(500);
if(c1==0)

break;


 
}

You forgot to give requested H/w details.

Assuming that you are using 89S51, remember all the ports, upon RESET are configured as inputs, ready to be used as input ports. When the first 0 is written to a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to the port.
So after main, P1=0; will make it output port and P2=0; will make P2 output port(but not used in your programme)
You can write P3=0x04; //00000100
will configure Port3 bit 3(P3^2 pin) pin as input.
P3=0xFF; will make every pins of that port input pins.

---------- Post added at 16:45 ---------- Previous post was at 16:25 ----------

**broken link removed**
when the Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned ON.
[Picture showing LED connected to port 2 pin. Same way could be connected to port 1]

input_switch.gif


When the switch is closed pin is low

Example code:-

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
main(){
 
P1=0;
P3=0x04; //P3_2 = 1; Setup P3_2 as input pin
 
    while(1){
        if(P3_2 == 0)
            P1 = 0; //Turn ON the LEDs
        else
            P1 = 0xFF; //Turn OFF the LED
         
    }
}

 
Last edited:

am using 8051 micro controller.in that leds are connected to port p1.
c1 is a switch(push button) which is connected to p3 port 2 pin .
now are you able to understand
 

Check the post #6 above
The exemple code power on all the LEDs when its pressed .
 

I had seen your post.but what am asking nah.I have written a coding for counter program by using led's.its working that's not a matter.after that if I press a switch means(c1==1) according to my program counter program has to stop or pause. this one is not working.
 

I had seen your post.but what am asking nah.I have written a coding for counter program by using led's.its working that's not a matter.after that if I press a switch means(c1==1) according to my program counter program has to stop or pause. this one is not working.
Try this :-

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
main(){
-------------------
-------------------------- 
for(P1=0x00;P1<=0xf0;P1++)
{
wait(500);
if(c1==0)  break;
}
 
while(1)
{
//It is important
}
}

 

thanks.what have I write in while loop
 

No need to write anything. It will work.
If you want to know why that(while loop) is required, I can explain.
 
Last edited:

thanks its working...I wants know why while loop is required
 
Last edited:

Well, When you run a program on Personal Computer, the OS takes over when a program ends. But in microcontroller when main ends , as there is no OS to take control, every thing becomes unstable.[I am avoiding the details]. So the trick is - trap the control so that it stays in main.
 
Last edited:

hi...
your concept I can understood little bit.your telling while loop concept for stop only.now I have to restart where I stopped .
 

Write the program such a way that when You press the pause button before exiting the loop save the current value in a global variable. and later start counting from there.

Try to write the code. If you have any problem I will help you to night after 9pm
 

thanks .how to get current values from the loop
 

Try the following code:-

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
#define pause 0
#define run     1
 
unsigned char mode,i;
 
void main()
{
P0=0x00; //output port 
//remember P0 of 8051 needs pullup resistances. check datasheet. so take into account when LEDs will be on
P3=0x0C; //pin 3 and 4 connected to switch. input 
i=0;
mode=run;
while(1){
            for( ;i<240 &&(mode==run);i++) //i initialized before
                 {
                      P0=i;
                      if(P3_2 == 0)     //pause button pressed
                          mode=pause;
                  }
            while(mode==pause)
                  {
                        if(P3_3==0) 
                               mode=run; // to start the counter resume again
                    }
              if(i==10) i=0; //initialize i to 0 to start counting from 0
        }
}



Also read an article on switch debouncing
 

thanks for reply.even though output is not coming I changed little bit the code.port 1 is connected to leds(output).in that any modification in coding part means u tell

Code:
#include<reg51.H>

#define p0 P0
#define p3 P3
#define p1 P1
#define pause 0
#define run     1
 unsigned char mode,i;    
sbit c1=p3^2;
sbit c2=p3^3;

void wait (int j)  {                      
 long int  d;

  for(d =0;d<j;d++);          
}
void main()
{

P1=0X00;
P3=0X0C;
 i=0;
 mode=run;
while(1)
{

for(;i<256 &&(mode==run);i++) //i initialized before
                 {
                      P1=i;
                      if(c1 == 0)     //pause button pressed
                    mode=pause;
					wait(5000);
					P1=P1-1;
                  }
            while(mode==pause)
                  {
                        if(c2==0) 
                               mode=run; // to start the counter resume again
                    }
              if(i==10) i=0; //initialize i to 0 to start counting from 0
        }
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top