• AVR Freaks

Hot!when sensor detect object LED doesn't trigreed

Page: 12345 > Showing page 1 of 5
Author
abhi143
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: online
2019/04/24 00:09:16 (permalink)
0

when sensor detect object LED doesn't trigreed

I have one proximity sensor and LED I want to triggered LED when sensor detect any object 
Hardware : PIC16F877A
IDE : MPLAB XC8
Here is my code
// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#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)
 
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
 
 
volatile unsigned int Counter;
char Trigger;
 
void main(void)
{
    PORTD = 0b00000000; //
    TRISB=0b00000001;     // Make RB0 pin as input
    TRISD= 0b11101111  ;    // Make RD1 pin as output
    OPTION_REG=1;  // Set Rising Edge Trigger
    INTCON = 1;   // Enable The Global Interrupt
    INTE = 1;     // External Interrupt Enable (not INTF)
    GIE=1;
    
    while (1)
    {
       
    }
 
}
 
void interrupt isr(void)
{  
    if(INTF==1)
    {
         RD4 = 1;  // LED ON
         __delay_ms(1000); //
         RD4 = 0;  // LED OFF
         
         INTF = 0;
    }
}

 
so problem is when sensor detect any object LED doesn't triggered I don't understand how to resolve problem 
#1

86 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 01:03:00 (permalink)
    +2 (2)
    It is a beginner's mistake to try to do too much in your first test.
    Forget about detecting the input or using interrupts to start with, and just make the LED blink once per second.
    Tht way, you know your PIC is actually running, and at the speed that you expect.
     
    n.b. you have selected the HS oscillator. Have you connected a crystal across the OSC pins?
    That PIC does NOT have a complete internal oscillator.
     

    Only 10 to go...
    #2
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 01:52:10 (permalink)
    +1 (1)
    Agree with qhb - start simple:
    1. Flash a led in main.
    2. Detect an input (just by polling in main, no interrupt), and get it to toggle the led.
    3. Use a timer interrupt to flash the led.
     
    And, if you are using interrupts, never ever put delay routines in them.
    If you are doing that, you have a fundamental misunderstanding of interrupts.  If you think this is correct, take class 101 again.
    #3
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 02:16:48 (permalink)
    0
    qhb
    It is a beginner's mistake to try to do too much in your first test.
    Forget about detecting the input or using interrupts to start with, and just make the LED blink once per second.
    Tht way, you know your PIC is actually running, and at the speed that you expect.




    I have already done that
     
    This is my working code for led blinking
     
    #define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ

    #include <xc.h>

    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #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)
    //END CONFIG

    int main()
    {
      PORTD = 0b00000000;
      TRISD= 0b11101111  ;    // Make RD4 pin as output
     
      while(1)
      {
        RD4 = 1;  // LED ON
        __delay_ms(1000); // 1 Second Delay
        RD4 = 0;  // LED OFF
        __delay_ms(1000); // 1 Second Delay
      }

    }

    #4
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 03:14:13 (permalink)
    +1 (1)
    Me thinks not working, or not working well.
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)

    No reset anywhere in sight.
    #5
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 03:17:17 (permalink)
    0
    working code :  When sensor become high tun on LED
    #define _XTAL_FREQ 2000000

    #include <xc.h>

    // BEGIN CONFIG
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #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)
    //END CONFIG

    int main()
    {
      TRISB = 0b11111110; //RB0 as Output PIN
      TRISD = 0b00000001; //RD0 as Input PIN


      while(1)
      {
        if(RD0 == 1) //If sensor activate
        {
          RB0 = 1; //LED ON
          __delay_ms(1000);
          RB0 = 0; //LED OFF
        }
      }
     
    }

     
    I hope now someone would help with interrupt program
    #6
    Mysil
    Super Member
    • Total Posts : 3322
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 03:34:13 (permalink)
    +2 (2)
    Edit:
    Too late, you already got this working:
     
    Good, then you have something that work.
    Then you may put in one small change at a time.
    Try to read the Port input for the pin used for the sensor, and change the LED output accordingly.
    If it do not work, investigate the TRISB and PORTB registers.
     
    Also investigate if there really is a signal from the sensor, on the input pin. You may use a multimeter.
    Some sensors may have a Open Drain Output that require a pull-up resistor to make a High logic level signal.
    What do datasheet for the sensor say?
     
        Mysil
    post edited by Mysil - 2019/04/24 03:37:22
    #7
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 03:34:38 (permalink)
    +3 (3)
    Your "working" code still contains this line:
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)

    Where is your clrwdt instruction?
     
    Anyway, now take your "working" code and add in main (i.e. without using an interrupt routine):
    1. Code and configuration to setup the external interrupt
    Do NOT enable GIE or INTE for now
    2. Code to check INTF in your main loop, and flash the led if you detect it is set
    Remember to clear INTF after you detect it
     
    Is your code still "working"?
    #8
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:01:03 (permalink)
    0
    pcbbc
    Me thinks not working, or not working well.
    #pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)

    No reset anywhere in sight.


    Where to reset Watchdog Timer ?
    #9
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:05:26 (permalink)
    0
    MysilSome sensors may have a Open Drain Output that require a pull-up resistor to make a High logic level signal.
    What do datasheet for the sensor say?



    I did test led with sensor without pull-up resistor. Program was simple if sensor is high turn on LED else turn off LED
    #10
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:19:03 (permalink)
    +1 (1)
    abhi143
    Where to reset Watchdog Timer ?

    That is what the CLRWDT instruction does, but really, if you don't understand how the WDT works, turn it OFF!
     
     

    Only 10 to go...
    #11
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:20:31 (permalink)
    +1 (1)
    abhi143
    I did test led with sensor without pull-up resistor. Program was simple if sensor is high turn on LED else turn off LED

    This is a non-answer.
    You don't mention what the result of this test was, and if it worked as you expected, that may have been more good luck than good planning if you did not have the pullup resistor.
     

    Only 10 to go...
    #12
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:31:53 (permalink)
    0
    qhb
    abhi143
    I did test led with sensor without pull-up resistor. Program was simple if sensor is high turn on LED else turn off LED

    This is a non-answer.
    You don't mention what the result of this test was, and if it worked as you expected, that may have been more good luck than good planning if you did not have the pullup resistor.
     


    That was working
     
    qhb
    abhi143
    Where to reset Watchdog Timer ?

    That is what the CLRWDT instruction does, but really, if you don't understand how the WDT works, turn it OFF!
     
     


    I tried following program where I disabled WDT
     
    // CONFIG
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #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)
     
    #define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
    #include<xc.h>
     
    void main(void)
    {
        PORTD = 0b00000000; //
        TRISB=0b00000001;     // Make RB0 pin as input
        TRISD= 0b11101111  ;    // Make RD1 pin as output
        OPTION_REG=1;  // Set Rising Edge Trigger
        INTCON = 1;   // Enable The Global Interrupt
        INTE = 1;     // External Interrupt Enable (not INTF)
        GIE=1;
        
        while (1)
        {
           
        }
     
    }
     
    void interrupt isr(void)
    {  
        if(INTF==1)
        {
             RD4 = 1;  // LED ON
             __delay_ms(1000); //
             RD4 = 0;  // LED OFF
             
             INTF = 0;
        }
    }

     
    But LED doesn't flash when sensor become High
    post edited by abhi143 - 2019/04/24 05:14:32
    #13
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:38:25 (permalink)
    +1 (1)
    abhi143
    That was working

    Like I said, probably good luck, not good design.
     

    I tried following program where I disabled WDT

    That code does NOT disble the WDT.
    All you did is change the comment, not the code.
     
    This:
    #pragma config WDTE = ON       // Watchdog Timer Enable bit (WDT disabled)

    should be
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)

     

    Only 10 to go...
    #14
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:45:01 (permalink)
    +1 (1)
    Your first go at this program was better.  At least in post one you had the WDT disabled:
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)

     
    1. Do NOT code an ISR.
    2. Do not enable the interrupt.  So remove these lines:
        INTE = 1;     // External Interrupt Enable (not INTF)
        GIE=1;

    3. Move the test for INTF into main and flash the led.
    Now you have removed a level of complexity from your program, which means less to get wrong.
     
    Have you looked at the INTCON register in the datasheet page 24?
    INTCON = 1; // Enable The Global Interrupt

    That sets the RBIF interrupt flag.  It doesn't enable any interrupts.
    Exactly what interrupt are you trying to enable?
    #15
    Sobottech
    Super Member
    • Total Posts : 252
    • Reward points : 0
    • Joined: 2015/12/02 03:32:17
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 04:59:00 (permalink)
    0
    Hi,
     

    OPTION_REG=1; // Set Rising Edge Trigger

     
    looks strange / not right.
    ...
    ..
    .

    OPTION_REG = 0b01000000;

    #16
    abhi143
    Starting Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 05:11:11 (permalink)
    0
    pcbbc
    Your first go at this program was better.  At least in post one you had the WDT disabled:
    Exactly what interrupt are you trying to enable?


    INTF: RB0/INT External Interrupt Flag bit
     // CONFIG
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #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)
     
    #define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
    #include<xc.h>
     
     

     
    void main(void)
    {
        PORTD = 0b00000000; //
        TRISB=0b00000001;     // Make RB0 pin as input
        TRISD= 0b11101111  ;    // Make RD1 pin as output
        OPTION_REG=1;  // Set Rising Edge Trigger
        
        while (1)
        {
             if(INTF==1)
        {
             RD4 = 1;  // LED ON
             __delay_ms(1000); //
             RD4 = 0;  // LED OFF
             
             INTF = 0;
        }
            
           
        }
     
    }

    post edited by abhi143 - 2019/04/24 05:13:15
    #17
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 05:23:11 (permalink)
    +1 (1)
    abhi143
    pcbbc
    Exactly what interrupt are you trying to enable?

    INTF: RB0/INT External Interrupt Flag bit

    Read the datasheet.
    RB0/INT External Interrupt Flag bit is NOT located in bit 0.
    Do not write the whole register.  Just set or clear the individual flags you require.

     // CONFIG
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = ON       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #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)
     
    #define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
    #include<xc.h>
     
     

     
    void main(void)
    {
        PORTD = 0b00000000; //
        TRISB=0b00000001;     // Make RB0 pin as input
        TRISD= 0b11101111  ;    // Make RD1 pin as output
        OPTION_REG=1;  // Set Rising Edge Trigger
        
        while (1)
        {
             if(INTF==1)
        {
             RD4 = 1;  // LED ON
             __delay_ms(1000); //
             RD4 = 0;  // LED OFF
             
             INTF = 0;
        }
            
           
        }
     
    }

    That's better.  But is it working?
    As qhb would say, we can't see your screen from here.
     
    You are still insisting on coding WDTE = ON, despite not using the watchdog.
    Why do you do this despite being asked numerous times, by numerous people, (at least 3 by me now) not to do that?
    Why make things hard for yourself?
    #18
    qhb
    Superb Member
    • Total Posts : 9990
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 05:23:59 (permalink)
    +1 (1)
    ... but yet again, you have ENABLED the WDT.
    This whole helping process doesn't work when you keep ignoring advice!
     

    Only 10 to go...
    #19
    Sobottech
    Super Member
    • Total Posts : 252
    • Reward points : 0
    • Joined: 2015/12/02 03:32:17
    • Location: 0
    • Status: offline
    Re: when sensor detect object LED doesn't trigreed 2019/04/24 05:50:10 (permalink)
    0
    It might help, if you specify your sensor. Is the output digital or analog? is a Pull-up/down resistor needed...?
     
    Perhaps you try using first a button/switch to simulate the sensor, after changing WDT...
     
    #20
    Page: 12345 > Showing page 1 of 5
    Jump to:
    © 2019 APG vNext Commercial Version 4.5