+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Junior Member level 1
    Points: 332, Level: 3

    Join Date
    Nov 2015
    Posts
    16
    Helped
    0 / 0
    Points
    332
    Level
    3

    12f675 using push to on push to off

    Hi,

    i write with c program in hitech compiler below this program was push to on push to off working but if push to switch continuously the output was continuously on & off glow ....how to stop this problem

    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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    #include<htc.h>
    #include<pic.h>
    #include <delay4.c>
     
    #define ON GPIO0==1
    #define OFF GPIO0==0
     
    #define _XTAL_FREQ   4000000 
     
     
    __CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);
     
     
    void main()
    {
        PIE1=0x00;
        ANSEL  = 0x00;       // Set ports as digital I/O, not analog input
        ADCON0 = 0x00;       // Shut off the A/D Converter
        CMCON  = 0x07;       // Shut off the Comparator
        VRCON  = 0x00;       // Shut off the Voltage Reference
        TRISIO = 0b000100;
        GPIO = 0x00;
        
              
    for(;;) 
       { 
         
         if (GPIO2 == 0 && GPIO0 == OFF) 
          { 
             
             GPIO0=1; 
             GPIO0 =ON;         
            
          } 
       
     
         if (GPIO2 == 0 && GPIO0 == ON) 
          { 
            
             GPIO0=0; 
             GPIO0=OFF; 
                    
          }
        DelayMs(3000); 
      
    }
     
     
     }

    •   Alt17th February 2017, 11:01

      advertising

        
       

  2. #2
    Advanced Member level 2
    Points: 4,732, Level: 16

    Join Date
    Jan 2015
    Posts
    679
    Helped
    221 / 221
    Points
    4,732
    Level
    16

    Re: 12f675 using push to on push to off

    Work through you code with a pencil and paper and you will see the problem. Start with GPIO2 at 0 and GPIO0 also 0. That will mean the first 'if' statement will be true so you then execute the 2 statements (that really do the same thing - only one is needed) which sets GPIO0 to 1.
    What happens then? The next 'if' statement will be executed which looks to see if GPIO2 is 0 (and we have not changed that so we know that part is true). Also it looks to see if GPIO0 is 1 - which we have just set it to so the whole condition is again true. That means the two (again duplicated) statements will set GPIO0 back to 0.
    You then have a 3 second delay and repeat the whole process again.
    What that means is that while GPIO2 is 0 then the output on GPIO0 with flash on then off quickly, wait 3 seconds and then flash on and off quickly again.
    Now repeat the exercise with GPIO2 set to 1 (presumably with the switch in the other position). Now both 'if' statements cannot ever become true and so nothing will happen.
    To fix this you need to think in terms of a simple state machine: you will need 4 boxes with the first being the initial state - button unpressed and the output 'off'. What is needed to move to the 2nd box which represents setting the output to 'on'? The answer is the button needs to be pushed.
    The next questions is what is the next thing that needs to happen: the button is down and the light is on but you need to be able to take your finger off the button and still leave the light on. Therefore the event that triggers the move to the next state is the button begin released so the 3rd box represents the output on but the button released.
    The next step will be when the button is again pressed in which case we want to turn the output off so this will take us to the 4th state. When we take out finger off the button we still want the output to stay off so this will take us back to the 1st box again.
    Coding up this simple state machine is very straight forward and there are lots of examples on the internet.
    That will work well in theory but there are some practical things you need consider. For a start, a physical switch or push button does not turn on and off immediately but will "bounce". That means the physical contacts will make and break the connection multiple times before they settle down to the new condition. This can take many milliseconds. Given that the instruction time of the device you are using is more than 1000 times faster, it will see and try to react to each of those bounces.
    Therefore you need to "debounce" the switch and this can be done with a few components (basically a resistor and capacitor as a low pass filter) or in software (where you sample the input every (say) 5mSec and tell your program that the switch is in the new position when you get (say) 5 consecutive samples that are the same). Again there are many examples of both hardware and software 'debounce' on the internet.
    I also assume that you have the MCU correctly connected and there are bypass capacitors on the power supply - if you don't have the electrical side of your circuit set up correctly then you can have other issues with the way the device operates.
    Susan



+ Post New Thread
Please login
--[[ ]]--