switch for 1 minute

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Hello,

I am still experience trouble to achieve that one minute delay, I tried the code posted lately what happen is the LED stays ON .Is there any way to accomplish this.Please any help.The delay is only for microsecond that I can achieve.
 

AlbertHall

Joined Jun 4, 2014
12,625
Hello,

I am still experience trouble to achieve that one minute delay, I tried the code posted lately what happen is the LED stays ON .Is there any way to accomplish this.Please any help.The delay is only for microsecond that I can achieve.
Post the code that you are using at the moment.
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
I am currently trying between this two code:
the first one:
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 == 1)       // if the switch still pressed
         {
//       for(count=0;count<240;count++){
            
           if(count<=240){
              
             PORTDbits.RD2 = 1;

          
          
       }

   }
   else{

           PORTDbits.RD2 = 0;
        }

          
}
}

For this one , only a microsecond is generate
the second one is the one that was suggested here :

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);        // Provide a delay to the switch
            if (RB0 ==LL)           // if PB signal is still low
            {

                PB = LH;            // PB status high
            }
        }
            
        // LED Control logic
        if (PB == LH)
        {          
            STATE = DOFF;
        }
        else
        {
            STATE = 0;
        }
    
        // LED status control
        switch(STATE)
        {
            case 1:
                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 2:
                LED = STEADY_OFF;                    // OFF
                break;
             default:                    // Do nothing
            break;
        }
    }
    return(0);
}
Moderators note : used code tags
 
Last edited by a moderator:

AlbertHall

Joined Jun 4, 2014
12,625
Try this one:
C:
/*
* File:   Main.c
* Author: AlbertHall
*
* Created on 03 January 2018, 16:47
*
* Description:
* Turn on an LED connected to RD2 for a specified time when a button connected to RB0 is pressed
* The button and the LED connect between PIC pin and 0V
*/

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 20000000
#define LED PORTDbits.RD2
#define BUTTON PORTBbits.RB0

#define LED_TIME 60 // Time to switch on LED in seconds

void main(void)
{
    PORTA = 0;
    PORTB = 0;
    PORTC = 0;
    PORTD = 0;
    ADCON1 = 0x06;  // All pins set as digital
    // Set all pins as outputs except RB0
    TRISA = 0;
    TRISB = 0x01;   // Pin RB0 as input
    TRISC = 0;
    TRISD = 0;
  
    OPTION_REGbits.nRBPU = 0;   // Turn on port B weak pullups
  
    while(1)    // Do forever
    {
        while(BUTTON == 1)   // Wait for button press
                ;

        LED = 1;            // Turn on the LED
        for(int i = 0; i < LED_TIME; i++)
            __delay_ms(1000);  // Wait for one second
        LED = 0;            // Turn off LED
    }          
}
Moderators note : changed code tags for C
 
Last edited by a moderator:

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Hi
Thanks for your support,I tried the code provided still it doesn't work.what is happening is that it doesn't read PORTBbits.RB0, the LED stays ON.I don't know what I am doing wrong
 

AlbertHall

Joined Jun 4, 2014
12,625
What do you have connected to RB0, pin 33 assuming the 40 pin PDIP package?
It should be a normally open switch to 0V (nothing else needed).
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
yes I connected to pin 33.but with the same connection if I use the first code , there is no problem to generate some microsecond delay when the switch is pressed and the input pin can be read
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Thank you so much it working fine now.what I did is I removed the 10k ohm resistor . I can generate a 1 minute delay, thank you so much
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
By any chance do you any advise for me as new programmer of microcontroller.Any materials that can help me practice more because my next steps will be how to control the brightness of an LED using PWM .
 

AlbertHall

Joined Jun 4, 2014
12,625
Try to understand the one minute program - every line - and check with the datasheet.
Get to know the simulator in MPLAB - how to set/clear pins or SFRs.
How to single step through a program or set breakpoints and use like the stopwatch in the simulator.

Approach bigger programs by dividing it into smaller chunks and get each part working before you start to combine them.
 

Thread Starter

ntendayip

Joined Dec 22, 2017
14
Hi

Is there away that I can generate PWM after the switch is pressed to output the on-time of the LED.below is my first attempt of setting up the pwm:
long PWM_freq = 5000;
PWM_Initialize()
{
PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Data sheet // Makes the PWM work in 5KHZ
CCP1M3 = 1;
CCP1M2 = 1; //Configure the CCP1 module
T2CKPS0 = 1;
T2CKPS1 = 0;
TMR2ON = 1; //Configure the Timer module
TRISC2 = 0; // make port pin on C as output
}
PWM_Duty(unsigned int duty)
{
if(duty<1023)
{
duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); // On reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) * TMR2PRESCALE);
CCP1X = duty & 1; //Store the 1st bit
CCP1Y = duty & 2; //Store the 0th bit
CCPR1L = duty>>2; // Store the remaining 8 bit
}
}

void main(void)
{
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
ADCON1 = 0x06; // All pins set as digital
//Set all pins as outputs except RB0
TRISA = 0;
TRISB = 1; // Pin RB0 as input
TRISC = 0;
TRISD = 0;



OPTION_REGbits.nRBPU = 0; // Turn on port B weak pull up
PWM_Initialize(); //This sets the PWM frequency of PWM1



while(1) // Do forever
{
while(BUTTON == 1) // Wait for button press
;

LED = 1; // Turn on the LED

for(int i = 0; i < LED_TIME; i++) //wait for 60 interrupt
__delay_ms(1000); // Wait for one second
LED = 0; // Turn off LED

PWM_Duty(512);
__delay_ms(50);

}
}

What happen is that the MCU can read RC2 pin but it doesn't output the behavior of the LED .
 
Top