Changing code for a normally closed switch from normally open

Thread Starter

RodneyB

Joined Apr 28, 2012
697
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.


C:
#include <xc.h>
 
// PIC10F200 Configuration Bit Settings
#pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
#pragma config CP = OFF  // Code Protect (Code protection off)
#pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
  // Because PIC10F200 doesn't have configurable POR or BOR, 
  // I advise an external RC reset on /MCLR.

 
#define  _XTAL_FREQ  4000000  //4Mhz INTOSC
#define RUN_TIME  (60*1)
#define START_TIME  (30*1)

// Hardware Support

#define SW  GPIObits.GP0
#define LED1 sGPIObits.GP1
#define LED2 sGPIObits.GP2

  //  6543210
#define myTRIS 0b1001  // sets input output bits.
 
// Port Shadowing 
volatile unsigned char sGPIO=0; // Port shadow register (global) 
#define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
#define updGPIO()  (GPIO = sGPIO)

// Time to Ticks scaling for Timer 0 half period
#define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128)))

unsigned int ticks;

void main (void) {
 
  GPIO = 0;
  OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
  TRISGPIO = myTRIS;
  
  ticks = Time2Ticks(START_TIME);
  
 while(1) { // loop forever 
  
  //Handle timer
  
  if (TMR0&0x80){ //check the high bit
  
  TMR0&=0x7F; //clear it
  
  if(ticks) --ticks; //decrement tick count
  }
  
  //Handle Switch 
  
  if(SW==1 && ticks==0){
  
  ticks =Time2Ticks(RUN_TIME);
  }
  //Handle LED  
  
  if(ticks) {
  
  LED1 =1;
  
  }
  else {
  
  LED1 = 0;
  
  }
  
  updGPIO();  // Actual update the GPIO port from the shadow register
  __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
  // Caution: using the compiler generated delays costs two bytes of RAM
 }
}
Amended code not working for normally closed switch

C:
#include <xc.h>
 
// PIC10F200 Configuration Bit Settings
#pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
#pragma config CP = OFF  // Code Protect (Code protection off)
#pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
  // Because PIC10F200 doesn't have configurable POR or BOR, 
  // I advise an external RC reset on /MCLR.

 
#define  _XTAL_FREQ  4000000  //4Mhz INTOSC
#define RUN_TIME  (60*1)
#define START_TIME  (30*1)

// Hardware Support

#define SW  GPIObits.GP0
#define LED1 sGPIObits.GP1
#define LED2 sGPIObits.GP2

  //  6543210
#define myTRIS 0b1001  // sets input output bits.
 
// Port Shadowing 
volatile unsigned char sGPIO=0; // Port shadow register (global) 
#define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
#define updGPIO()  (GPIO = sGPIO)

// Time to Ticks scaling for Timer 0 half period
#define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128)))

unsigned int ticks;

void main (void) {
 
  GPIO = 0;
  OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
  TRISGPIO = myTRIS;
  
  ticks = Time2Ticks(START_TIME);
  
 while(1) { // loop forever 
  
  //Handle timer
  
  if (TMR0&0x80){ //check the high bit
  
  TMR0&=0x7F; //clear it
  
  if(ticks) --ticks; //decrement tick count
  }
  
  //Handle Switch 
  
  if(SW==0 && ticks==1){
  
  ticks =Time2Ticks(RUN_TIME);
  }
  //Handle LED  
  
  if(ticks) {
  
  LED1 = 0;
  
  }
  else {
  
  LED1 = 1;
  
  }
  
  updGPIO();  // Actual update the GPIO port from the shadow register
  __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
  // Caution: using the compiler generated delays costs two bytes of RAM
 }
}
 

Brownout

Joined Jan 10, 2012
2,390
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'
 
Top