Changing code for a normally closed switch from normally open

Discussion in 'Embedded Systems and Microcontrollers' started by RodneyB, Aug 22, 2015.

  1. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    I originally used this code for an emergency lighting system using a normally open push button switch. I now want to used a normally closed switch and adjusted the code but cant get it to work.

    I have posted my code below and my amended code below that.


    Code (C):
    1.  
    2.  
    3. #include <xc.h>
    4.  
    5. // PIC10F200 Configuration Bit Settings
    6. #pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
    7. #pragma config CP = OFF  // Code Protect (Code protection off)
    8. #pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
    9.   // Because PIC10F200 doesn't have configurable POR or BOR,
    10.   // I advise an external RC reset on /MCLR.
    11.  
    12.  
    13. #define  _XTAL_FREQ  4000000  //4Mhz INTOSC
    14. #define RUN_TIME  (60*1)
    15. #define START_TIME  (30*1)
    16.  
    17. // Hardware Support
    18.  
    19. #define SW  GPIObits.GP0
    20. #define LED1 sGPIObits.GP1
    21. #define LED2 sGPIObits.GP2
    22.  
    23.   //  6543210
    24. #define myTRIS 0b1001  // sets input output bits.
    25.  
    26. // Port Shadowing
    27. volatile unsigned char sGPIO=0; // Port shadow register (global)
    28. #define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
    29. #define updGPIO()  (GPIO = sGPIO)
    30.  
    31. // Time to Ticks scaling for Timer 0 half period
    32. #define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128)))
    33.  
    34. unsigned int ticks;
    35.  
    36. void main (void) {
    37.  
    38.   GPIO = 0;
    39.   OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
    40.   TRISGPIO = myTRIS;
    41.  
    42.   ticks = Time2Ticks(START_TIME);
    43.  
    44.  while(1) { // loop forever
    45.  
    46.   //Handle timer
    47.  
    48.   if (TMR0&0x80){ //check the high bit
    49.  
    50.   TMR0&=0x7F; //clear it
    51.  
    52.   if(ticks) --ticks; //decrement tick count
    53.   }
    54.  
    55.   //Handle Switch
    56.  
    57.   if(SW==1 && ticks==0){
    58.  
    59.   ticks =Time2Ticks(RUN_TIME);
    60.   }
    61.   //Handle LED  
    62.  
    63.   if(ticks) {
    64.  
    65.   LED1 =1;
    66.  
    67.   }
    68.   else {
    69.  
    70.   LED1 = 0;
    71.  
    72.   }
    73.  
    74.   updGPIO();  // Actual update the GPIO port from the shadow register
    75.   __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
    76.   // Caution: using the compiler generated delays costs two bytes of RAM
    77.  }
    78. }
    79.  
    80.  
    Amended code not working for normally closed switch

    Code (C):
    1.  
    2.  
    3. #include <xc.h>
    4.  
    5. // PIC10F200 Configuration Bit Settings
    6. #pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
    7. #pragma config CP = OFF  // Code Protect (Code protection off)
    8. #pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
    9.   // Because PIC10F200 doesn't have configurable POR or BOR,
    10.   // I advise an external RC reset on /MCLR.
    11.  
    12.  
    13. #define  _XTAL_FREQ  4000000  //4Mhz INTOSC
    14. #define RUN_TIME  (60*1)
    15. #define START_TIME  (30*1)
    16.  
    17. // Hardware Support
    18.  
    19. #define SW  GPIObits.GP0
    20. #define LED1 sGPIObits.GP1
    21. #define LED2 sGPIObits.GP2
    22.  
    23.   //  6543210
    24. #define myTRIS 0b1001  // sets input output bits.
    25.  
    26. // Port Shadowing
    27. volatile unsigned char sGPIO=0; // Port shadow register (global)
    28. #define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
    29. #define updGPIO()  (GPIO = sGPIO)
    30.  
    31. // Time to Ticks scaling for Timer 0 half period
    32. #define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128)))
    33.  
    34. unsigned int ticks;
    35.  
    36. void main (void) {
    37.  
    38.   GPIO = 0;
    39.   OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
    40.   TRISGPIO = myTRIS;
    41.  
    42.   ticks = Time2Ticks(START_TIME);
    43.  
    44.  while(1) { // loop forever
    45.  
    46.   //Handle timer
    47.  
    48.   if (TMR0&0x80){ //check the high bit
    49.  
    50.   TMR0&=0x7F; //clear it
    51.  
    52.   if(ticks) --ticks; //decrement tick count
    53.   }
    54.  
    55.   //Handle Switch
    56.  
    57.   if(SW==0 && ticks==1){
    58.  
    59.   ticks =Time2Ticks(RUN_TIME);
    60.   }
    61.   //Handle LED  
    62.  
    63.   if(ticks) {
    64.  
    65.   LED1 = 0;
    66.  
    67.   }
    68.   else {
    69.  
    70.   LED1 = 1;
    71.  
    72.   }
    73.  
    74.   updGPIO();  // Actual update the GPIO port from the shadow register
    75.   __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
    76.   // Caution: using the compiler generated delays costs two bytes of RAM
    77.  }
    78. }
    79.  
    80.  
     
  2. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Try changing this:

    if(SW == 0 && ticks==1)

    to this:

    if(SW==0 && ticks==0)

    Changing the swtich type shouldn't change the timing variable 'ticks'
     
Loading...