switch for 1 minute

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Hi

Currently working on a project that I need to use a push a button. the requirement is :you press the switch and the LED stays ON for one minute,
below is my attempts. What happening now is that , the LED stays ON for ever , how can I manage to create that one minute delay after the switch is pressed.Any help to resolve this will be appreciate.I am using pic16f877a, and MPLAB xc8

C:
int count=0;
void main() {
  
TRISD = 0;                     // configuring output port
TRISB = 1;                    // configure as input
TMR1H=0x00;                  // initial count values in timer1 register
TMR1L=0x00;
T1CON=0x01;                

while(1)
{
  
   if ( PORTBbits.RB0 ==0)       // if the switch still pressed
         {
       for(count=0;count<=1;count++){
            
           PORTDbits.RD2 = 1;
           count=0;
          
       }

   }
   else{

           PORTDbits.RD2 = 0;
        }

          
}
}
Moderators note : used code tags
 

QMESAR

Joined Dec 9, 2017
15
Hi,
Two things I miss in your post
(1) is the PIN pulled High or Low (what is the Voltage level at the mcu pin from the Switch when not presses)
(2) What Oscillator speed are you using

The reason is when your code is looking for Logic Low on when the button is pressed and if you for example has the pin pulled down then the LED will always be ON,The OSC speed helps to determine how to setup a counter for 1 minute
 

AlbertHall

Joined Jun 4, 2014
12,625
XC8 includes some useful delay routines:
__delay_ms(x); Which will delay for the specified number of milliseconds
__delay_us(x); Which will delay for the specified number of microseconds.
To use these you need to include a line near the top of the program specifying the crystal frequency.
#include _XTAL_FREQ 4000000 // For a 4MHz clock oscillator.
 

QMESAR

Joined Dec 9, 2017
15
XC8 includes some useful delay routines:
__delay_ms(x); Which will delay for the specified number of milliseconds
__delay_us(x); Which will delay for the specified number of microseconds.
To use these you need to include a line near the top of the program specifying the crystal frequency.
#include _XTAL_FREQ 4000000 // For a 4MHz clock oscillator.
I agree with this totally however I ask myself how much sense does a delay of 1 minute make with delay function as for 1 minute the controller will do nothing else than counting down the delay.in this persons case probably not an issue ,however when he starts to add other tasks in his code the delays will cause him problems
Only my opinion
 

AlbertHall

Joined Jun 4, 2014
12,625
I agree with this totally however I ask myself how much sense does a delay of 1 minute make with delay function as for 1 minute the controller will do nothing else than counting down the delay.in this persons case probably not an issue ,however when he starts to add other tasks in his code the delays will cause him problems
Only my opinion
And I don't disagree with any of that. However TS is clearly not a programming expert and if a blocking delay will do the job then there is no sense in trying to teach how to do it with timer interrupts.
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Thanks for the reply.without a push button , implemented to the code, I can achieve 1 minute delay without a problem.I have this set up
#define _XTAL_FREQ 20000000
//#define TMR2PRESCALE 4
#include <xc.h>
#include<pic16f877a.h>
#include <stdio.h>
#include <stdlib.h>
I am using a 10 kohm pull up resistor .I am supplying 5v to the mcu. is the issue on this level:
  1. for(count=0;count<=1;count++){
  2. PORTDbits.RD2 = 1;
  3. count=0;
  4. }
is there any example that I can follow or anyone who ever done this before who can share their code.thanksif someone can provide a set up idea
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
I try to remove count = 0, on the loop still facing the same issue.is my code even make any sense to accomplish what I am trying to achieve?sorry, I am new to pic mcu.if not please help,
 

spinnaker

Joined Oct 29, 2009
7,830
Hi

Currently working on a project that I need to use a push a button. the requirement is :you press the switch and the LED stays ON for one minute,
below is my attempts. What happening now is that , the LED stays ON for ever , how can I manage to create that one minute delay after the switch is pressed.Any help to resolve this will be appreciate.I am using pic16f877a, and MPLAB xc8

C:
int count=0;
void main() {
 
TRISD = 0;                     // configuring output port
TRISB = 1;                    // configure as input
TMR1H=0x00;                  // initial count values in timer1 register
TMR1L=0x00;
T1CON=0x01;               

while(1)
{
 
   if ( PORTBbits.RB0 ==0)       // if the switch still pressed
         {
       for(count=0;count<=1;count++){
           
           PORTDbits.RD2 = 1;
           count=0;
         
       }

   }
   else{

           PORTDbits.RD2 = 0;
        }

         
}
}
Moderators note : used code tags
What does your debuuger say? Just set a breakpoint and see what is going on.

Also you should be setting latches not ports. You read ports and set latches.
 

AlbertHall

Joined Jun 4, 2014
12,625
Also you should be setting latches not ports. You read ports and set latches.
Unfortunately that is dependant on which PIC it is.
For some PICs, there is no latch register.
For some PICs they will automatically direct writes to the port to a latch register.
For the rest, this only matters if you are changing a port pin between input and output, or you have some load connected to a pin which will pull an output pin to the opposite state.
 

spinnaker

Joined Oct 29, 2009
7,830
Unfortunately that is dependant on which PIC it is.
For some PICs, there is no latch register.
For some PICs they will automatically direct writes to the port to a latch register.
For the rest, this only matters if you are changing a port pin between input and output, or you have some load connected to a pin which will pull an output pin to the opposite state.
Yep looks like the pic16f877a has none. My mistake.

But the debugger comment still stands. ;) Tell me the pic16f877a can't be debugged too. ;)
 

Picbuster

Joined Dec 2, 2013
1,058
Hi

Currently working on a project that I need to use a push a button. the requirement is :you press the switch and the LED stays ON for one minute,
below is my attempts. What happening now is that , the LED stays ON for ever , how can I manage to create that one minute delay after the switch is pressed.Any help to resolve this will be appreciate.I am using pic16f877a, and MPLAB xc8

C:
int count=0;
void main() {

TRISD = 0;                     // configuring output port
TRISB = 1;                    // configure as input
TMR1H=0x00;                  // initial count values in timer1 register
TMR1L=0x00;
T1CON=0x01;              

while(1)
{

   if ( PORTBbits.RB0 ==0)       // if the switch still pressed
         {
       for(count=0;count<=1;count++){
          
           PORTDbits.RD2 = 1;
           count=0;
        
       }

   }
   else{

           PORTDbits.RD2 = 0;
        }

        
}
}
Moderators note : used code tags
The most simple way is to use interrupts

C:
#define Led  PORTDbits.RD2 // in your case

// below will work for all PIC processors
Timer interrupt flag  // adjust to one second  or make a counter to hit one sec.
If (Led)  {Timer++ ;}
      if (Timer >= 60) 
      {
      Timer=0;
      Led=Off;
      }

  }

//================  pin int  
  On int pin  connected to pushbutton

          Clear int;
           Led=On;  // this stays on untill Timer >= 60
//============= end pin int
 
Last edited by a moderator:

eetech00

Joined Jun 8, 2013
4,705
Hi

Its blocking code but I've done it this way:

C:
#define _XTAL_FREQ  4000000     // Oscillator frequency 4mhz
#define RB0 PORTBits.RB0        // input port
#define LED PORTDbits.RD2        // output port
#define STEADY_ON 1                // LED ON
#define STEADY_OFF 0            // LED OFF
#define LL        0                // Logic Low
#define LH        1                // Logic High
#define DB      100               // Debounce Time in milliseconds
#define DOFF    60000           // Delay Off Time in milliseconds

int main(void)
{
    // Initialization
    TRISD = 0;                     // Set as output
    TRISB = 1;                    // Set as input
       
    // set global variables
    unsigned int PB = LL;
    unsigned int STATE = LL;

     while(LH)                        // Begin infinite loop
    {
        if (RB0 == LL)                // if PB signal is low
        {
            __delay_ms(DB);            // debounce
            if (RB0 == LL)            // if PB signal is still low
            {  
                PB = LH;            // PB status high
            }
        }
              
        // LED Control logic
        if (PB == LH)
        {
            STATE = DOFF;
        }
        else
        {
            STATE = OFF;
        }
      
        // LED status control
        switch(STATE)
        {
            case ON:
                LED = STEADY_ON;            // steady on
                break;
            case DOFF:
                LED = STEADY_ON;               // steady on
                __delay_ms(DOFF);            // wait "Delay off" time
                LED = STEADY_OFF;            // steady_off
                break;
            case OFF:
                LED = OFF;                    // OFF
                break;
            case default:                    // Do nothing
            break;
        } 
    }
    return(0);
}
Moderators note : used code tags
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
Hi

Its blocking code but I've done it this way:

#define _XTAL_FREQ 4000000 // Oscillator frequency 4mhz
#define RB0 PORTBits.RB0 // input port
#define LED PORTDbits.RD2 // output port
#define STEADY_ON 1 // LED ON
#define STEADY_OFF 0 // LED OFF
#define LL 0 // Logic Low
#define LH 1 // Logic High
#define DB 100 // Debounce Time in milliseconds
#define DOFF 60000 // Delay Off Time in milliseconds

int main(void)
{
// Initialization
TRISD = 0; // Set as output
TRISB = 1; // Set as input

// set global variables
unsigned int PB = LL;
unsigned int STATE = LL;

while(LH) // Begin infinite loop
{
if (RB0 == LL) // if PB signal is low
{
__delay_ms(DB); // debounce
if (RB0 == LL) // if PB signal is still low
{
PB = LH; // PB status high
}
}

// LED Control logic
if (PB == LH)
{
STATE = DOFF;
}
else
{
STATE = OFF;
}

// LED status control
switch(STATE)
{
case ON:
LED = STEADY_ON; // steady on
break;
case DOFF:
LED = STEADY_ON; // steady on
__delay_ms(DOFF); // wait "Delay off" time
LED = STEADY_OFF; // steady_off
break;
case OFF:
LED = OFF; // OFF
break;
case default: // Do nothing
break;
}
}
return(0);
}

PLEASE use code tags!
 

bertus

Joined Apr 5, 2008
22,921
Hello,

You can put code tags around a piece of code like this:
[code]here comes the code
second line of code[/code]
the result will be:
Code:
here comes the code
second line of code
The code can be highlighted using the language name.
Look here for more info:
Small improvement to CODE tag

Bertus
 

spinnaker

Joined Oct 29, 2009
7,830
Hi

Its blocking code but I've done it this way:

C:
#define _XTAL_FREQ  4000000     // Oscillator frequency 4mhz
#define RB0 PORTBits.RB0        // input port
#define LED PORTDbits.RD2        // output port
#define STEADY_ON 1                // LED ON
#define STEADY_OFF 0            // LED OFF
#define LL        0                // Logic Low
#define LH        1                // Logic High
#define DB      100               // Debounce Time in milliseconds
#define DOFF    60000           // Delay Off Time in milliseconds

int main(void)
{
    // Initialization
    TRISD = 0;                     // Set as output
    TRISB = 1;                    // Set as input
      
    // set global variables
    unsigned int PB = LL;
    unsigned int STATE = LL;

     while(LH)                        // Begin infinite loop
    {
        if (RB0 == LL)                // if PB signal is low
        {
            __delay_ms(DB);            // debounce
            if (RB0 == LL)            // if PB signal is still low
            { 
                PB = LH;            // PB status high
            }
        }
             
        // LED Control logic
        if (PB == LH)
        {
            STATE = DOFF;
        }
        else
        {
            STATE = OFF;
        }
     
        // LED status control
        switch(STATE)
        {
            case ON:
                LED = STEADY_ON;            // steady on
                break;
            case DOFF:
                LED = STEADY_ON;               // steady on
                __delay_ms(DOFF);            // wait "Delay off" time
                LED = STEADY_OFF;            // steady_off
                break;
            case OFF:
                LED = OFF;                    // OFF
                break;
            case default:                    // Do nothing
            break;
        }
    }
    return(0);
}
Moderators note : used code tags

I am not sure how this still does not block in the delay. Anyway you need a delay after the final off or LEDS will only be off of a few micro seconds
 
Top