Post the code that you are using at the moment.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.
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);
}
/*
* 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
}
}
What does it do without commenting out that line?if I comment this line : OPTION_REGbits.nRBPU=0;// Turn on port B weak pullups , then the LED turns ON and stays ON
The program expects the switch to connect RB0 to 0V. You don't need the resistor.I connected a 10k ohm resistor to pin 33 and the ground with the switch and the other side of the switch to 5v