Discussion in "Project Help" started by    Ansh12    Mar 22, 2019.
Fri Mar 22 2019, 09:15 pm
#1
I have pic16f877a and MPLABX8 and I want to count pluses generated by incremental encoder

for example : I want to count 40000 pulses

I see we can count pulses with an interrupt

1 which interrupts would to use to count encoder pulses timer or external interrupt?
2. Is there any better option?


[ Edited Fri Mar 22 2019, 09:30 pm ]
Sat Mar 23 2019, 02:40 am
#2
Using the external interrupt on pin B0 is the best option.
 Ansh12 like this.
Sat Mar 23 2019, 10:38 pm
#3


Using the external interrupt on pin B0 is the best option.

ExperimenterUK


Thank's ExperimenterUK

Here is code for external interrupt I don't have an idea how to make progress for counting pulses
// 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

    } 
}


any help with programming would be appreciated ?
Mon Mar 25 2019, 03:22 am
#4
Counting pulses can be as simple as
void interrupt isr(void)
{
if(INTF==1)
{
INTF = 0; // Clear interrupt flag
pulseCount++;
}
}

declare pulseCount in the body of you program as for example

volatile unsigned int pulseCount; //0- 64000
void main(void)
{
pulseCount=0;
.......etc

Really it depends on what you are doing with the count.

In the latest version of MPLAB IDE (v5.15) you need to declare the interrupt handler as

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


[ Edited Tue Mar 26 2019, 10:25 am ]
Sun Mar 31 2019, 03:26 pm
#5


ExperimenterUK



Following code are not working

if 30 count complete turn on / off LED

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

volatile unsigned int Counter;
char Trigger;

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 (not INTF)
    GIE=1;
    Counter = 0;  //initial values at start up
    Trigger = 0;
    
    while (1)
    {
        if(Trigger == 1)
        {
             Trigger = 0;
             Counter = 0;
            
             RD1 = 1;  // LED ON
             __delay_ms(1000); // 
             RD1 = 0;  // LED OFF
             __delay_ms(500); 
        }
    }
 
}

void interrupt isr(void)
{  
    if(INTF==1)
    {
        INTF = 0;  // Clear interrupt flag
        Counter++;    // add 1 to the Counter
        if(Counter == 30) 
        Trigger = 1;
    } 
}


after some time led goes turn on and staying turn on

can you tell me why it's staying on
Mon Apr 01 2019, 02:03 am
#6
I tried your code in Proteus and it works.
How are you driving B0 ?
How fast are you driving B0 ?
How long before the LED comes on ?




[ Edited Mon Apr 01 2019, 02:09 am ]
Mon Apr 01 2019, 05:04 pm
#7


I tried your code in Proteus and it works.
How are you driving B0 ?
How fast are you driving B0 ?
How long before the LED comes on ?

ExperimenterUK



I have incremental encoder of 5000 PPR. encoder work on 24V DC so I have converted output of encoder into 5V DC using opt-coupler

When I burned program LED stay on for forever

Please find attachment


Tue Apr 02 2019, 12:41 am
#8
The problem may not be with your code.
You will have to go back to basics and test what works.
So..first write program to just flash the led on and off.

Then flash the led when you get any input on B0.
Disconnect the encoder and change level by hand.
Let me know what happens.




 Ansh12 like this.
Mon Apr 22 2019, 02:42 pm
#9


Then flash the led when you get any input on B0.

Let me know what happens.

ExperimenterUK


as suggested I have modified program but I don't know why LED doesn't flash when sensor become high

// 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;
    } 
}
Fri Apr 26 2019, 01:50 am
#10
I made a minor change, it works in Proteus.
Try the attached hex code, just in case you have a compiler problem.

Are you sure you are getting a good signal into pin RB0 ?

if(INTF==1)
    {
         RD4 = 1;  // LED ON
         __delay_ms(1000); // 
         RD4 = 0;  // LED OFF
         __delay_ms(1000); // 
         
         INTF = 0;
    } 

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

JamesNip
Tue Mar 19 2024, 02:57 pm
EdwardFew
Mon Mar 18 2024, 01:14 pm
EmeryPah
Mon Mar 18 2024, 11:51 am
RobertMax
Sun Mar 17 2024, 10:22 pm
DanielJar
Fri Mar 15 2024, 06:52 pm
Tuzaimecor
Fri Mar 15 2024, 02:32 am
PromotionFoode
Thu Mar 14 2024, 08:11 pm
EdwardGeawn
Sun Mar 10 2024, 12:24 pm