Timer in MCU not stopping when "asked"

Thread Starter

FroceMaster

Joined Jan 28, 2012
683
Hi,
Working on another project,
PIC 16F690

Here is my code,

C:
// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

#define _XTAL_FREQ 8000000  // Maybee ??? more ???
#define Input1  RB4  // Button pressed.

int ventetid, i, x, y, z, v;  // VARIAB
int sec10_flag, antal_sec;
int out_running, gate_status;

void __interrupt() myISR(void)  // interrupt routine.
{

    if (TMR2IF==1) // Timer2 50 times a sec.  
    { TMR2IF=0; // reset interrupt
      antal_sec++; // and one to counter.
      RC0=!RC0; //  TEST TEST perfect 20 ms square wave  = 50 pr second ?
        if (antal_sec>499) // 10 sec gone
        { TMR2ON=0;   //stop timer.
            sec10_flag=1; // say TIME is gone, handle in MAIN
           antal_sec=0; // set to 0 again,
          }
    } // end TIMER2 interrupt.
    
} // end interrupt

void setup (void)
{
    // speed
    IRCF0=1;
    IRCF1=1;
    IRCF2=1; // 8 mhz,max ?
    // PORTS
    TRISA0=1;
    TRISA1=1;
    TRISA2=1;// input  LawnMover in garage.  LOW When parked.
    TRISA3=1;
    TRISA4=1;
    TRISA5=1;
    TRISB4=1; // Manuel open
    TRISB5=1; // FREE IN
    TRISB6=1; // FREE IN
    TRISB7=1; // FREE IN
    TRISC0=0; // TEST TEST
    TRISC1=0; 
    TRISC2=0; 
    TRISC3=0; // OUT HAVELÅGE OPEN
    TRISC4=0; // OUT PORT CLOSE
    TRISC5=0; // OUT PORT OPEN
    TRISC6=0; // OUT HAVELÅGE CLOSE
    TRISC7=0; // OUT 230V.
    // Interrupt On Change.
    //RC0 RA2.  DONE LATER MAYBE , Not critical, can handle in main,
    
 
    WPUA2=0; // no pull ups.
    // DIGITAL.
    ANSEL=0b00000000;
    ANSELH=0b00000000;
    //Setup timer 2,,, Generate 50 pr sec ~  * 10 sec
    T2CKPS0=1;
    T2CKPS1=1; // Prescale 1:16
   TOUTPS0=1; // Postscale 1:10
   TOUTPS1=0;
   TOUTPS2=0;
   TOUTPS3=1; // set to 2.000.000 / 16 = 125.000 / 250 / 10 = 50 times pr sec,
   
      //interrupts enables.

    TMR2IE=1; // interrupt timer 2
    PEIE=1; // pie interupt
    GIE=1; // GLOBAL INTERRUPT.
     
    
}

void open_port (void) // opens garage port.
{RC7=1; // turn on 230 v.
RC5=1; // set open port.
RC3=1; // open gate.
TMR2ON=1;    // timer to cut power again...
   //delay 1 sec, to prevent double
for (y=0;y>100;y++) // loop for 1 sec, to be sure of debounce
{
__delay_ms(10);
}
out_running=1; // we are out cutting
gate_status=1; // gate is open
}   // end open port in garage

void close_port (void) // close garage port and GATE
{ // wait a little loop to test if really in garage :)
    for (x=0;x<100;x++)
    {
       __delay_ms(20); // delay 2000 ms, and check again 
    }
    if (RA2==0) // LOW WHEN IN GARAGE
    {
    TMR2ON=1; // timer on 10 sec.  
            RC7=1; // 230v
            RC4=1; // close port.
            RC6=1; // close gate
            out_running=0; // not cutting more.
            gate_status=0; // gate is closed.
    }
            
    
}
void open_gate (void) // OPEN GARDENGATE MANUEL
{RC7=1; // 230v
RC3=1; // Open gate
TMR2ON=1; // turn timer on
for (z=0;z>200;z++)
{__delay_ms(10); // delay 2 sec, until button is not pressed, Debounce
}

gate_status=1; // gate is open
}


void close_gate (void) // CLOSE GARDENGATE
{
    RC7=1; // 230v
    RC6=1; // close gate
    TMR2ON=1 ; // turn timer on 
    for (z=0;z>200;z++)
{__delay_ms(10); // delay 2 sec, until button is not pressed,
}
    
    gate_status=0; // gate is closed.
}


void main(void) 
{
    setup();
    antal_sec=0;
    out_running=0;
    gate_status=0;
    sec10_flag=0;
    x=0;
    y=0;
    z=0;
    v=0;
    RC7=0; // 230 v
            RC5=0; // cut all relays 
            RC4=0;
            RC6=0;
            RC3=0;
    PR2=249; // 0-249 = 250, set PR2 to 249 = part of 50 timer a sec interrupt.
    
while(1)  // things to do allways.
    {
        if ((RA2==1)&& (out_running==0)) // LEAVING GARAGE
        {open_port();  // open port so Lawnmover can get out :)
        
        }
        
        if ((Input1==0)&& (out_running==0)&& (gate_status==0)) // button pressed while in garage. and CLOSED GATE
        { // Loop check 200 ms,
            for (v=0;v<20;v++)
            {
                __delay_ms(10);
            }
            if (Input1==0) // if still pressed.
            {open_gate();
            
            }
            
        } // end open gate manuel
        
        if ((Input1==0)&& (out_running==0)&& (gate_status==1)) // button pressed while in garage. and OPEN GATE
        { // Loop check 100 ms,
            for (v=0;v<10;v++)
            {
                __delay_ms(10);
            }
            if (Input1==0) // if still pressed.
            {close_gate();
            
            }
            
        } // end close gate manuel
        
        
        if ((RA2==0) && (out_running==1)) // Cutting and going to garage again LOW WHEN IN GARAGE
        {
            close_port();
        }
        
        if (sec10_flag==1)  // IF 10 'SEC IS GONE WE WILL KILL 230v AGAIN.
        {
           
            TMR2ON=0; // end has reached.,
            antal_sec=0;
            RC7=0; // 230 v
            RC5=0; // cut all 
            RC4=0;
            RC6=0;
            RC3=0;
            sec10_flag=0; // cut off again
            
        } // end 10 sec timer. //
        
            
    } // end while
    
    
} // end main.
Somehow the timer keeps running if LawnMovers is leaving garage, RA2=1. ( Should only be 10 sec )
When returning to GARAGE it should also start timer2 again and wait 10 sec,
But after the small LOOP to check if in garage, the timer stops,.
 

hexreader

Joined Apr 16, 2011
567
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
First thing to do is turn watchdog timer off....

I have tidied up your code to make it more readable, but I still cannot work out what the code is supposed to do.

Would you be willing to provide a flow chart to show the high level picture of what the code is intended to do?

C:
// messy code found on forum
// seems to be sort of lawn mower borrowing device - hard to guess
// have edited the code to be more readable, but I still cannot see what is supposed to happen.
// maybe a flow diagram would help???

// PIC16F690 Configuration Bit Settings
// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

#define _XTAL_FREQ 8000000                                                      // Maybee ??? more ???
#define Input1  RB4                                                             // Button pressed.

// port usage:
//  input RA2 LOW WHEN IN GARAGE
//  input RA4 inout1 button - low when pressed, normally high
// output RC0 25Hz square wave interrupt test for 20ms
// output RC5 cut all relays
// output RC6 HAVELÃGE CLOSE
// output RC7 1 = turn on 230V

int ventetid, i, x, y, z, v;                                                    // VARIAB
int sec10_flag, antal_sec;
int out_running, gate_status;

void __interrupt() myISR(void)                                                  // interrupt routine.
{
    if (TMR2IF == 1)                                                            // Timer2 50 times a sec. 
    {
        TMR2IF = 0;                                                             // reset interrupt
        antal_sec++;                                                            // and one to counter.
        RC0 = !RC0;                                                             // TEST TEST perfect 20 ms square wave  = 50 pr second ?
        if (antal_sec > 499)                                                    // 10 sec gone
        {
            TMR2ON = 0;                                                         // stop timer.
            sec10_flag = 1;                                                     // say TIME is gone, handle in MAIN
            antal_sec = 0;                                                      // set to 0 again,
        }
    }                                                                           // end TIMER2 interrupt.
}                                                                               // end interrupt

void setup (void)
{
    // speed
    IRCF0 = 1;
    IRCF1 = 1;
    IRCF2 = 1;                                                                  // 8 MHz,max ?
    // PORTS
    TRISA0 = 1;
    TRISA1 = 1;
    TRISA2 = 1;                                                                 // input  LawnMover in garage.  LOW When parked.
    TRISA3 = 1;
    TRISA4 = 1;
    TRISA5 = 1;
    TRISB4 = 1;                                                                 // Manuel open
    TRISB5 = 1;                                                                 // FREE IN
    TRISB6 = 1;                                                                 // FREE IN
    TRISB7 = 1;                                                                 // FREE IN
    TRISC0 = 0;                                                                 // TEST TEST
    TRISC1 = 0;
    TRISC2 = 0;
    TRISC3 = 0;                                                                 // OUT HAVELÃGE OPEN
    TRISC4 = 0;                                                                 // OUT PORT CLOSE
    TRISC5 = 0;                                                                 // OUT PORT OPEN
    TRISC6 = 0;                                                                 // OUT HAVELÃGE CLOSE
    TRISC7 = 0;                                                                 // OUT 230V.
    // Interrupt On Change.
    //RC0 RA2.  DONE LATER MAYBE , Not critical, can handle in main,
    
 
    WPUA2 = 0;                                                                  // no pull ups.
    // DIGITAL.
    ANSEL = 0b00000000;
    ANSELH = 0b00000000;
    //Setup timer 2,,, Generate 50 pr sec ~  * 10 sec
    T2CKPS0 = 1;
    T2CKPS1 = 1;                                                                // Pre-scale 1:16
    TOUTPS0 = 1;                                                                // Post-scale 1:10
    TOUTPS1 = 0;
    TOUTPS2 = 0;
    TOUTPS3 = 1;                                                                // set to 2.000.000 / 16 = 125.000 / 250 / 10 = 50 times pr sec,
  
    //interrupts enables.
    TMR2IE = 1;                                                                 // interrupt timer 2
    PEIE = 1;                                                                   // pie interrupt
    GIE = 1;                                                                    // GLOBAL INTERRUPT.
}

void open_port (void)                                                           // opens garage port.
{
    RC7 = 1;                                                                    // turn on 230 v.
    RC5 = 1;                                                                    // set open port.
    RC3 = 1;                                                                    // open gate.
    TMR2ON = 1;                                                                 // timer to cut power again...
    //delay 1 sec, to prevent double
    for(y = 0; y > 100; y++)                                                   // loop for 1 sec, to be sure of de-bounce
    {
        __delay_ms(10);
    }
    out_running = 1;                                                            // we are out cutting
    gate_status = 1;                                                            // gate is open
}                                                                               // end open port in garage

void close_port (void)                                                          // close garage port and GATE
{                                                                               // wait a little loop to test if really in garage :)
    for (x = 0; x < 100; x++)
    {
        __delay_ms(20);                                                         // delay 2000 ms, and check again
    }
    if (RA2 == 0)                                                               // LOW WHEN IN GARAGE
    {
        TMR2ON = 1;                                                             // timer on 10 sec. 
        RC7 = 1;                                                                // 230v
        RC4 = 1;                                                                // close port.
        RC6 = 1;                                                                // close gate
        out_running = 0;                                                        // not cutting more.
        gate_status = 0;                                                        // gate is closed.
    }
}
void open_gate (void)                                                           // OPEN GARDENGATE MANUEL
{
    RC7 = 1;                                                                    // 230v
    RC3 = 1;                                                                    // Open gate
    TMR2ON = 1;                                                                 // turn timer on
    for (z = 0; z > 200; z++)
    {
        __delay_ms(10);                                                         // delay 2 sec, until button is not pressed, De-bounce
    }
    gate_status = 1;                                                            // gate is open
}


void close_gate (void)                                                          // CLOSE GARDENGATE
{
    RC7 = 1;                                                                    // 230v
    RC6 = 1;                                                                    // close gate
    TMR2ON = 1;                                                                 // turn timer on
    for (z = 0; z > 200; z++)
    {
        __delay_ms(10);                                                         // delay 2 sec, until button is not pressed,
    }
    gate_status = 0;                                                            // gate is closed.
}


void main(void)
{
    setup();
    antal_sec = 0;
    out_running = 0;
    gate_status = 0;
    sec10_flag = 0;
    x = 0;
    y = 0;
    z = 0;
    v = 0;
    RC7 = 0;                                                                    // 230 v
    RC5 = 0;                                                                    // cut all relays
    RC4 = 0;
    RC6 = 0;
    RC3 = 0;
    PR2 = 249;                                                                  // 0-249 = 250, set PR2 to 249 = part of 50 timer a sec interrupt.
    
    while(1)                                                                    // things to do always.
    {
        if ((RA2 == 1) && (out_running == 0))                                   // LEAVING GARAGE
        {
            open_port();                                                        // open port so Lawn mower can get out :)
        }
        
        if ((Input1 == 0) && (out_running == 0) && (gate_status == 0))          // button pressed while in garage. and CLOSED GATE
        {                                                                       
            for (v = 0; v < 20; v++)                                            // Loop check 200 ms
            {
                __delay_ms(10);
            }
            if (Input1 == 0)                                                    // if still pressed.
            {
                open_gate();
            }     
        }                                                                       // end open gate manuel
        
        if ((Input1 == 0) && (out_running == 0) && (gate_status == 1))          // button pressed while in garage. and OPEN GATE
        {                                                                       
            for (v = 0; v < 10; v++)                                            // Loop check 100 ms,
            {
                __delay_ms(10);
            }
            if (Input1 == 0)                                                    // if still pressed.
            {
                close_gate();
            }
        } // end close gate manuel
        
        if ((RA2 == 0) && (out_running == 1))                                   // Cutting and going to garage again LOW WHEN IN GARAGE
        {
            close_port();
        }
        
        if (sec10_flag == 1)                                                    // IF 10 'SEC IS GONE WE WILL KILL 230v AGAIN.
        {
            TMR2ON = 0;                                                         // end has reached.,
            antal_sec = 0;
            RC7 = 0;                                                            // 230 v
            RC5 = 0;                                                            // cut all
            RC4 = 0;
            RC6 = 0;
            RC3 = 0;
            sec10_flag = 0;                                                     // cut off again
        }                                                                       // end 10 sec timer. //           
    }                                                                           // end while   
}                                                                               // end main.
 
Last edited:

Thread Starter

FroceMaster

Joined Jan 28, 2012
683
Hi,
Code is for controlling a Lawnmover, Where i will have a port/gate that will open when mover is drivin out.
HAve also a GardenGate that also will have to be open, when LM is moving, And a button so i manuel can open gate when i want to pass,...

But anyhow, now it works :) Thanks to watchdog.
 
Top