+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 31
  1. #1
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Glow LED on specified counter values 0-500 in 89c52 uC

    Please let me know I have 89c52 and I want to program in this manner that counter counts by external interrupt by push button 0 to 500.
    When it reaches 10 glow LED1 and at 15 glow LED2 at 25 glow LED1 again at 30 glow LED2 and so on till 500.

    Code:
    unsigned int Counter = 0;
    void ext0_isr(void) interrupt 0
    {. 
    Counter++;
     }
    
    main()
    {. 
    if(Counter == 10)
    LED1 = 1;
    else if(Counter == 15)
    LED2 = 1;
    else if(Counter == 25)
    LED1 = 1;
    
    and so on till 500
    }
    In above for 0 to 500 LED1 and LED2 glow alternatively with gap of 10 and 5.

    I want to know is there any possibility for shorter code size.
    IMRAN

  2. #2
    Super Moderator
    Points: 54,180, Level: 56

    Join Date
    Apr 2011
    Location
    Minneapolis, Minnesota, USA
    Posts
    13,206
    Helped
    2629 / 2629
    Points
    54,180
    Level
    56

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    It looks as though you light led1 every 15 counts starting at 10.
    Likewise led2 lights every 15 counts starting at 15.

    This can be done by a simple formula, for each led.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 3
    Points: 3,642, Level: 14
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    728
    Helped
    54 / 54
    Points
    3,642
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Try this.

    Code:
    unsigned int Counter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;
    }
    
    main()
    { 
    if(Counter % 10)LED1 = 1;
    else 	LED2 = 1;
    }
    
    or
    
    main()
    { 
    if(Counter % 10)LED1 = 1;
    elseif(!(Counter % 10))	LED2 = 1;
    }


    1 members found this post helpful.

  4. #4
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Yes exactly.Please hint

    - - - Updated - - -

    I will try it, you gave in previous thread great answer this answer also looks good I will try and let you know.
    IMRAN



  5. #5
    Super Moderator
    Points: 82,131, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,666
    Helped
    3778 / 3778
    Points
    82,131
    Level
    69

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Hi,

    In the given code I miss how the LEDs are set to "0".

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

  6. #6
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Quote Originally Posted by KlausST View Post
    Hi,

    In the given code I miss how the LEDs are set to "0".

    Klaus
    Code:
    unsigned int Counter = 0;
    unsigned char flag = 0;
    void ext0_isr(void) interrupt 0
    {. 
    Counter++;
     }
    
    main()
    {. 
    if(Counter == 10 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    else if(Counter == 15 && flag == 1)
    {
    LED2 = 1;
    delay(1000);
    LED2 = 0;
    flag = 0;
    }
    else if(Counter == 25 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    else if(Counter == 30 && flag == 1)
    {
    LED2 = 1;
    delay(1000);
    LED2 = 0;
    flag  = 0
    }
    else if(Counter == 40 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    
    and so on till 500
    }
    I want to make it shorter by using formula or FOR LOOP or other LOOP, any hint?
    IMRAN



  7. #7
    Super Moderator
    Points: 82,131, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,666
    Helped
    3778 / 3778
    Points
    82,131
    Level
    69

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Hi,

    As "counter" is used both in main and isr, it should be declared "volatile".

    If "counter" needs multiple access to be processed (i.e. 16 bit variable on a 8 bit microcontroller) you should use "atomic" access.
    For hobbyists this often is no problem, but for reliable industrial solutions it's very important to avoid malfunction.

    If you want repeated function of LED glow every x events,
    * consider to reset (or better: subtract x from) the counter
    * or use the modulo operator

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  8. #8
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Quote Originally Posted by KlausST View Post
    Hi,

    As "counter" is used both in main and isr, it should be declared "volatile".

    If "counter" needs multiple access to be processed (i.e. 16 bit variable on a 8 bit microcontroller) you should use "atomic" access.
    For hobbyists this often is no problem, but for reliable industrial solutions it's very important to avoid malfunction.

    If you want repeated function of LED glow every x events,
    * consider to reset (or better: subtract x from) the counter
    * or use the modulo operator

    Klaus


    I also used while loop for forever operation.I will try your suggestions and will let you know.

    Code:
    unsigned int Counter = 0;
    unsigned char flag = 0;
    void ext0_isr(void) interrupt 0
    { 
    Counter++;
     }
    
    main()
    {
    
    while(1){
    if(Counter == 10 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    else if(Counter == 15 && flag == 1)
    {
    LED2 = 1;
    delay(1000);
    LED2 = 0;
    flag = 0;
    }
    else if(Counter == 25 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    else if(Counter == 30 && flag == 1)
    {
    LED2 = 1;
    delay(1000);
    LED2 = 0;
    flag  = 0
    }
    else if(Counter == 40 && flag == 0)
    {
    LED1 = 1;
    delay(1000);
    LED1 = 0;
    flag = 1;
    }
    
    and so on till 500
    }}
    IMRAN



  9. #9
    Super Moderator
    Points: 84,086, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,835
    Helped
    4593 / 4593
    Points
    84,086
    Level
    70

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Subtract the base values 10 and 15 from the Counter value and if the result modulo 10 and 15 is zero, turn the LED on, otherwise turn it off.

    Make sure 'Counter' is declared volatile.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  10. #10
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Ok doing it.
    IMRAN



  11. #11
    Advanced Member level 3
    Points: 3,642, Level: 14
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    728
    Helped
    54 / 54
    Points
    3,642
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Try this.

    Code:
    volatile unsigned int Counter = 0;
    volaile unsigned int oldCounter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;
    	
    	if(oldCounter != Counter) {
    		oldCounter = Counter;
    		LED1 = 0;
    		LED2 = 0;
    	}
    }
    
    main()
    { 
    if(Counter % 10)LED1 = ~LED1;
    elseif(!(Counter % 10))	LED2 = ~LED2;
    delay(1000);
    }



  12. #12
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Quote Originally Posted by baileychic View Post
    Try this.

    Code:
    volatile unsigned int Counter = 0;
    volaile unsigned int oldCounter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;
    	
    	if(oldCounter != Counter) {
    		oldCounter = Counter;
    		LED1 = 0;
    		LED2 = 0;
    	}
    }
    
    main()
    { 
    if(Counter % 10)LED1 = ~LED1;
    elseif(!(Counter % 10))	LED2 = ~LED2;
    delay(1000);
    }
    LED1 should start from 10 and with gap 15 it will lit-up, means 10,25,40,55,70....... till 500.

    and

    LED2 should start from 15 and with gap 15 it will lit-up, means 15,30,45,60,75........till 500.

    - - - Updated - - -

    I tried this but LED was glowing at start of Counter means at Counter value 1.

    - - - Updated - - -

    Quote Originally Posted by betwixt View Post
    Subtract the base values 10 and 15 from the Counter value and if the result modulo 10 and 15 is zero, turn the LED on, otherwise turn it off.

    Make sure 'Counter' is declared volatile.

    Brian.
    I tried this, but only at value 10 of counter LED was litting-up but I also want to lit-up to whole sequence 0-500 first start at 10 then 25 then 40 then 55 then 70.....so on 500.
    Same as applies for 15 but how to resolve please more hints needed.

    Code:
    	if((!(10 - Counter) % 10) && flag == 0)
                {						
    		LED1 = 1;
                    delay(1000);
                    LED = 0;
                    flag = 1;					
    	   }
    IMRAN



  13. #13
    Advanced Member level 3
    Points: 3,642, Level: 14
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    728
    Helped
    54 / 54
    Points
    3,642
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Very simple. Try this.

    Code:
    volatile unsigned int Counter = 0;
    volaile unsigned int oldCounter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;	
    }
    
    main()
    { 
    if(Counter >= 10) && ((Counter % 15) == 10) { LED2 = 0; LED1 = ~LED1; }
    elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
    delay(1000);
    }



  14. #14
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Quote Originally Posted by baileychic View Post
    Very simple. Try this.

    Code:
    volatile unsigned int Counter = 0;
    volaile unsigned int oldCounter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;	
    }
    
    main()
    { 
    if(Counter >= 10) && ((Counter % 15) == 10) { LED2 = 0; LED1 = ~LED1; }
    elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
    delay(1000);
    }

    It is working only Counter value of 10 only after 10 onwards it is not working.
    I tried only for if(Counter >= 10) && ((Counter % 15) == 10) this condition for LED1 only.
    IMRAN



  15. #15
    Advanced Member level 3
    Points: 3,642, Level: 14
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    728
    Helped
    54 / 54
    Points
    3,642
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Missing brackets.

    Code:
    volatile unsigned int Counter = 0;
    volaile unsigned int oldCounter = 0;
    
    void ext0_isr(void) interrupt 0
    { 
    	Counter++;	
    }
    
    main()
    { 
    if((Counter >= 10) && ((Counter % 15) == 10)) { LED2 = 0; LED1 = ~LED1; }
    elseif((Counter >= 15) && ((Counter % 10) == 0)){ LED1 = 0; LED2 = ~LED2;}
    delay(1000);
    }



  16. #16
    Super Moderator
    Points: 84,086, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,835
    Helped
    4593 / 4593
    Points
    84,086
    Level
    70

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    from my post #9:

    Subtract the increment from the counter then check the modulo for zero.
    You did it the other way around! Subtracting Counter from 10 results in a 'negative' rollover of the unsigned int!

    if(((Counter - 10) % 10) == 0) LED1 = 1; else LED1 = 0;
    if(((Counter - 15) % 15) == 0) LED2 = 1; else LED2 = 0;

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  17. #17
    Super Moderator
    Points: 82,131, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,666
    Helped
    3778 / 3778
    Points
    82,131
    Level
    69

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Hi,

    (Counter - 10) % 10)
    (Counter - 10) % 10) is the same as (Counter % 10)
    (Counter - 15) % 15) is the same as (Counter % 15)

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  18. #18
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Problem Solved

    I used simple formula not huge coding needed but unfortunately no one in thread could provide proper answer, however problem solved.

    Code:
    volatile unsigned int Counter=0, h=10, v=15;
    
    isr(void) interrupt 0
    {
    Counter++;
    }
    
    
    main()
    {
    while(1)
    if(Counter == h)         // for LED1
    {
    LED1=1;
    delay(1000);			
    LED1=0;
    h+=15;  
    } 
    
    if(Counter == v)         // for LED2
    {
    LED2=1;
    delay(1000);			
    LED2=0;
    v+=15;  
    } 
    }
    }               											}
    IMRAN



  19. #19
    Super Moderator
    Points: 30,285, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    9,061
    Helped
    1143 / 1143
    Points
    30,285
    Level
    42
    Blog Entries
    9

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    I used simple formula not huge coding needed but unfortunately no one in thread could provide proper answer, however problem solved
    Problem 'solved' with a bad programming style, consider next time using state machine handling delays with flags instead of closed loop delays. I would not be surprised if you reopen the discussion reporting that with the above working code you are unable to do well other tasks besides this LED blinky itself. By the way, with the code above, you perform the required task as expected only in the first iteration.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  20. #20
    Advanced Member level 2
    Points: 4,016, Level: 14
    Achievements:
    7 years registered
    imranahmed's Avatar
    Join Date
    Dec 2011
    Location
    Karachi,Pakistan
    Posts
    650
    Helped
    3 / 3
    Points
    4,016
    Level
    14

    Re: Glow LED on specified counter values 0-500 in 89c52 uC

    Quote Originally Posted by andre_teprom View Post
    Problem 'solved' with a bad programming style, consider next time using state machine handling delays with flags instead of closed loop delays. I would not be surprised if you reopen the discussion reporting that with the above working code you are unable to do well other tasks besides this LED blinky itself. By the way, with the code above, you perform the required task as expected only in the first iteration.
    Yes you saying alright but please let me know how to do in best way.How to use state machine handling delays with flags?

    Please let me know.


    Are you taking about only delays (bad programming style)?
    IMRAN



    •   AltAdvertisement

        
       

--[[ ]]--