• AVR Freaks

Hot!how to count the number of external pulses using pic16f877a

Author
abhi143
Starting Member
  • Total Posts : 59
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: online
2019/03/22 06:13:24 (permalink)
0

how to count the number of external pulses using pic16f877a

Hey everyone,
 
I am new for PIC16F877A I'm trying to connect an incremental encoder with PIC16F877A. I want to count pulses generated by encoder. as I've read it, We can do with interrupt 
 
Which interrupt need to use timer interrupt or External interrupt ?
#1

15 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/22 13:46:33 (permalink)
    +1 (1)
    Neither.
    For counting pulses try configuring Timer1 in Asynchronous Counter Mode.
    See section 6.4 of the data sheet.
    #2
    qhb
    Superb Member
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/22 13:55:09 (permalink)
    +1 (1)
    That's one way to do it.
    Why not show us the exact wording of your homework so we can give the right answer.
     

    Only 5 to go...
    #3
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 04:56:02 (permalink)
    0
    qhb
    That's one way to do it.
    Why not show us the exact wording of your homework so we can give the right answer.




    This is not homework I am trying to make following project (Planning face)
    when sensor detect empty bottle, encoder start to generate pulses and PIC16f877a will count pulses to reject at 600mm distance. for example if take 1200 pulses to reach at 600mm distance then PIC16F877A will count 1200 pulse then it will turn on air jet to reject empty bottle
     
    Encoder is connected to motor shaft which is driving a conveyor
     

     
    I read it we can count external using external interrupt 
     
    Code for external interrupt
    // PIC16F877A Configuration Bit Settings

    // 'C' source line config statements

    // 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)

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #define _XTAL_FREQ 16000000
    #include <xc.h>

    void main(void)
    {
        
        TRISB0=1;     // Make RB0 pin as input
        TRISD1= 0;    // Make RD1 pin as output
        OPTION_REG=1;  // Set Rising Edge Trigger
        INTCON = 1;   // Enable The Global Interrupt
        INTE = 1;     // External Interrupt Enable

     
      while(1)
      {

          
      }
        return;
    }
    void interrupt isr(void)
    {  
        if(INTF==1)
        {
            INTF = 0;  // Clear interrupt flag

        }
    }

     
    I know it's not easy project but your thought would be appreciated
     
    #4
    qhb
    Superb Member
    • Total Posts : 9997
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 05:08:06 (permalink)
    +3 (3)
    So you're designing an industrial product using an 18 year old chip?
    (PIC16F877A was released in 2001)
    PIC16F devices have come a long way since then. People asking questions about them are invariably doing courses that have antique study notes that haven't been updated since then.
     
     
     

    Only 5 to go...
    #5
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 09:41:53 (permalink)
    0
    qhb
    So you're designing an industrial product using an 18 year old chip?
    (PIC16F877A was released in 2001)
    PIC16F devices have come a long way since then. People asking questions about them are invariably doing courses that have antique study notes that haven't been updated since then.
     

    I said in my post I am on the planning phase.
    I think If I understand logic then only I can write code for any micro-controller I don't think there is something wrong starting with PIC16F877A
     
    any help would be appreciated about programming language ?
    #6
    pcbbc
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 10:07:01 (permalink)
    0
    #7
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 10:14:38 (permalink)
    0
    I have been reading this link but I don't know PLC programming
    post edited by abhi143 - 2019/03/23 10:15:39
    #8
    1and0
    Access is Denied
    • Total Posts : 9043
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 10:33:23 (permalink)
    0
    Issues:
    • Watchdog Timer is ON but not being taken care of in code,
    • INTCON = 1 does not enable global interrupt, and
    • has to increment a volatile counter variable in ISR to count pulses.
    Edit:
    • ... and don't think OPTION_REG = 1 set rising edge trigger.
    post edited by 1and0 - 2019/03/23 10:58:25
    #9
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 12:09:43 (permalink)
    0
    1and0
    Issues:
    • Watchdog Timer is ON but not being taken care of in code,
    • INTCON = 1 does not enable global interrupt, and
    • has to increment a volatile counter variable in ISR to count pulses.
    Edit:
    • ... and don't think OPTION_REG = 1 set rising edge trigger.


    my modified code
     
    // PIC16F877A Configuration Bit Settings

    // 'C' source line config statements

    // 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)

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #define _XTAL_FREQ 16000000
    #include <xc.h>

    volatile unsigned int Counter;

    void main(void)
    {
        
         Counter = 0;  //initial zero
        
        TRISB0=1;     // Make RB0 pin as input
        TRISD1= 0;    // Make RD1 pin as output
        OPTION_REG=1;  // Set Rising Edge Trigger
        INTCON = 1;   // Enable The Global Interrupt
        INTE = 1;     // External Interrupt Enable

     
      while(1)
      {

          
      }
        return;
    }
    void interrupt isr(void)
    {  
        if(INTF==1)
        {
            INTF = 0;  // Clear interrupt flag
            Counter++;
           
        }
    }

    post edited by abhi143 - 2019/03/23 12:10:48
    #10
    pcbbc
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/23 12:41:18 (permalink)
    +2 (2)
    You've not disabled the watchdog, fixed the global interrupt enable or set the OPTION_REG correctly?
     
    #11
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/24 09:07:22 (permalink)
    0
    pcbbc
    You've not disabled the watchdog, fixed the global interrupt enable or set the OPTION_REG correctly?

    Please take a look at following
    What would be value of OPTION_REG register  ?
    OPTION_REG= ?
     
    // 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;


    void main(void)
    {
        
        TRISB0=1;     // Make RB0 pin as input
        TRISD1= 0;    // Make RD1 pin as output
     
        INTCON = 1;   // Enable The Global Interrupt
        INTE = 1;     // External Interrupt Enable
        GIE=1;
        Counter = 0;  //initial values at start up
       
        
        while (1)
        {
            
            
        }
     
    }

    void interrupt isr(void)
    {  
        if(INTF==1)
        {
            INTF = 0;  // Clear interrupt flag
            Counter++;    
            
         }
    }

    #12
    1and0
    Access is Denied
    • Total Posts : 9043
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/24 09:37:23 (permalink)
    +1 (1)
    abhi143
    What would be value of OPTION_REG register  ?
    OPTION_REG= ?

    Do you read your PIC datasheet?
    #13
    abhi143
    Starting Member
    • Total Posts : 59
    • Reward points : 0
    • Joined: 2018/05/20 09:06:52
    • Location: 0
    • Status: online
    Re: how to count the number of external pulses using pic16f877a 2019/03/24 09:56:08 (permalink)
    0
    1and0
    abhi143
    What would be value of OPTION_REG register  ?
    OPTION_REG= ?

    Do you read your PIC datasheet?


    Yes http://ww1.microchip.com/downloads/en/devicedoc/39582b.pdf
    Page 22 gives information
     
    But I didn't understand
     
    this link confused me https://exploreembedded.com/wiki/PIC16f877a_Timer
    #14
    pcbbc
    Super Member
    • Total Posts : 927
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/24 10:11:21 (permalink)
    +1 (1)
    OPTION_REG = 0b????????;

    Replace the first ? with either 1 or 0 depending on if you want:
    RBPU: PORTB Pull-up Enable bit
    1 = PORTB pull-ups are disabled
    0 = PORTB pull-ups are enabled by individual port latch values

    Continue replacing a ? for each bit, or group of bits, as specified in order on page 22, until you get the the right most 3 ??? and:
    PS2:PS0: Prescaler Rate Select bits

    Edit: Why did the link confuse you? It gives exactly the same information in the datasheet?
    post edited by pcbbc - 2019/03/24 10:13:31
    #15
    Aussie Susan
    Super Member
    • Total Posts : 3542
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: how to count the number of external pulses using pic16f877a 2019/03/24 18:39:49 (permalink)
    +4 (4)
     By the way is :https://www.edaboard.com/showthread.php?383182-Counting-pulses-from-external-source-PIC16F877a your entry as well? The references to an air jet in both make me thing so - or someone else is working on what appears to be an identical project.
    By the way, you have a design consideration that will possibly trip you up in the future: what if you find a 2nd empty bottle before the first one has reached the station where it is blown out of the way?
    While I suggest that you get some of the real basics out of the way (as discussed above and also in the referenced topic on edaboard) you will need to come up with an alternative approach that will let you handle the 'multiple empty bottle' situation. (Hint it is not all that hard but I suggest that you will need to do a bit of 'pencil and paper' thinking first.)
    As for the OPTION_REG settings, there may be an underlying issue with how to interpret the Microchip register descriptions.  Each bit (or related set of bits) has a name that is further explained in the list below the register diagram. Above the diagram is a set of codes that well you what you can do with the bit and also what its value is after a reset. 'R' means you can read the bit, 'W' means you can write (or set) the bit value, the number (normally 0 or 1) gives the reset value - if you do nothing else with the register then these are the bit settings you will have. You can also look at the 'Legend' block at the bottom of the register descriptions.
    Susan
    #16
    Jump to:
    © 2019 APG vNext Commercial Version 4.5