+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    how to set and reset pin in microcontroller

    I enabled timer and i want to set and reset pin in microcontroller every two hours, how to do that? i mean i want the pin on for 2 hours and off for another 2 hours.
    Last edited by mshh; 30th April 2019 at 10:52.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 79,050, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,046
    Helped
    3634 / 3634
    Points
    79,050
    Level
    68

    Re: how to set and reset pin in microcontroller

    Hi,

    what a question...

    Neither clear whether the timing is the problem or set/clear a pin
    nor does it give a clue about microcontroler, language or compiler.

    so: How can we help?

    Hint: it is a very basic task (or two tasks) which has been discussed numerous times in the internet and the forum...
    What have you done so far to find a solution on your own.

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



  3. #3
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    Re: how to set and reset pin in microcontroller

    the problem is i want to set and reset it every 2 hours and every state hold its value for 2 hours
    for example
    Code:
    if( hour=1&minute=59)/// this will set it every 2 hours but it will reset after 1 minute when the condition is false.
    {
    set
    }
    Last edited by mshh; 30th April 2019 at 12:11.



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 79,050, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,046
    Helped
    3634 / 3634
    Points
    79,050
    Level
    68

    Re: how to set and reset pin in microcontroller

    Hi,

    Still unclear what the problem is.

    if( hour=1&minute=59)/// this will set it every 2 hours but it will reset after 1 minute when the condition is false.
    {
    set
    }
    Wrong. Your code never clears the pin. The pin will be set at 1:59 and it remains in this state.

    What about a simple flow chart? You think itīs useless? Or a timing diagram...
    What about using a simulator or emulator?

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



    •   AltAdvertisement

        
       

  5. #5
    Full Member level 5
    Points: 2,933, Level: 12
    Achievements:
    7 years registered

    Join Date
    May 2010
    Location
    usa
    Posts
    313
    Helped
    0 / 0
    Points
    2,933
    Level
    12

    Re: how to set and reset pin in microcontroller

    Code:
    if( hour=1&minute=59)/// this will set it every 2 hours but it will reset after 1 minute when the condition is false.
    {
    set
    }
    else // when (hours=0&minute=0 or hours=1&minute= 0 or hours=0&minute=59)
    {
    reset /// for two hours but time is changing so will the condition 
    }
    ok, i will try in simulator, but i need suggestions to assure that i am using the right path.
    Last edited by mshh; 30th April 2019 at 12:50.



  6. #6
    Super Moderator
    Points: 79,050, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,046
    Helped
    3634 / 3634
    Points
    79,050
    Level
    68

    Re: how to set and reset pin in microcontroller

    Hi,

    I think itīs time for a little effort on your own. At least for giving complete informations and specifications. And give some clarifications according post#2.

    For example:
    We donīt know where "hour" and "minute" come from. We donīt know whether this is absolute time in 12h or 24h format or if you may reset the variables as you like.


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



  7. #7
    Member level 3
    Points: 2,107, Level: 10
    Achievements:
    7 years registered

    Join Date
    Dec 2009
    Location
    USA
    Posts
    54
    Helped
    15 / 15
    Points
    2,107
    Level
    10

    Re: how to set and reset pin in microcontroller

    Is this for the Arduino?
    If yes, then just use the millis() fucntion.

    If not then which which micro-controller?
    Typically, a Hardware Timer will trigger an interrupt.
    Every Timer Interrupt increments a variable until the desired Count ( 2 Hours ) is reached.
    Then simply Toggle the Output Bit, reset the variable and repeat forever.



  8. #8
    Super Moderator
    Points: 82,003, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,446
    Helped
    4487 / 4487
    Points
    82,003
    Level
    69

    Re: how to set and reset pin in microcontroller

    How about:
    if (hour % 4) set;
    else reset;

    or even simpler:
    output = (hour & 0x02);

    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

        
       

  9. #9
    Member level 3
    Points: 2,107, Level: 10
    Achievements:
    7 years registered

    Join Date
    Dec 2009
    Location
    USA
    Posts
    54
    Helped
    15 / 15
    Points
    2,107
    Level
    10

    Re: how to set and reset pin in microcontroller

    Quote Originally Posted by betwixt View Post
    How about:
    if (hour % 4) set;
    else reset;

    or even simpler:
    output = (hour & 0x02);

    Brian.
    Where is "hour" coming from on a Micro-controller?



  10. #10
    Advanced Member level 4
    Points: 6,247, Level: 18
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,209
    Helped
    182 / 182
    Points
    6,247
    Level
    18

    Re: how to set and reset pin in microcontroller

    Quote Originally Posted by mshh View Post
    I enabled timer and i want to set and reset pin in microcontroller every two hours, how to do that? i mean i want the pin on for 2 hours and off for another 2 hours.
    Ok. Let's assume your timer generates interrupt every 100mS.
    Now declare a variable as Int or Word (I.e. 16 bits) named Counter.
    Declare another variable as 2-hour-flag.

    In your 100mS isr, increment the Counter. Also if Counter is >= 72000, then SET 2-hour-flag.

    Now in your main routine, setup a loop which checks 2-hour-flag. When it is SET, then TOGGLE your outputpin, and also set 2-hour-flag and Counter to zero.

    That's your full code right there.

    Of course if your timer interruots at a different rate, then calculate the '72000' accordingly.

    - - - Updated - - -

    Okay, slight error in above. The variable for Counter naturally has to be defined to be able to hold the count required. So select accordingly. Typical Int will not hold 72000, only upto 65535.



--[[ ]]--