Discussion in "Project Help" started by    Ansh12    Mar 22, 2019.
Fri Mar 22 2019, 03:45 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, 04:00 PM ]
Fri Mar 22 2019, 09:10 PM
#2
Using the external interrupt on pin B0 is the best option.
 Ansh12 like this.
Sat Mar 23 2019, 05:08 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 ?
Sun Mar 24 2019, 09:52 PM
#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, 04:55 AM ]
Sun Mar 31 2019, 09:56 AM
#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
Sun Mar 31 2019, 08:33 PM
#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 Sun Mar 31 2019, 08:39 PM ]
Mon Apr 01 2019, 11:34 AM
#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


Mon Apr 01 2019, 07:11 PM
#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, 09:12 AM
#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;
    } 
}
Thu Apr 25 2019, 08:20 PM
#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

Conversation

Wed Oct 23 2019, 05:49 AM
latamdatewsc
swimming
Wed Oct 23 2019, 04:03 AM
Trey Carswell
How would you like to promote your business on 1000's of Advertising sites every month? For one small fee you'll get virtually unlimited traffic to your site forever! Check out our site for details: http://lotsofadsposted4u.dealz.site
Sun Oct 13 2019, 02:02 AM
Aline Hutchinson
Hey there,
ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum

Downloads

Comments

ilylily07
Tue Oct 15 2019, 04:32 PM
hientran123
Tue Oct 15 2019, 02:27 AM
Sat Oct 05 2019, 02:38 PM
zexalo
Thu Oct 03 2019, 09:53 AM
thanhle12345
Thu Oct 03 2019, 07:37 AM
ramon_vsc
Fri Sep 20 2019, 04:53 PM
youssoufba
Thu Sep 19 2019, 11:57 AM
Taiphan000
Tue Sep 17 2019, 04:21 AM

Online

Guests: 30, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 38097
Newest member: ilylily07
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Oct 22 2019, 05:18 AM
wimac Avatar
Posted By wimac
Mon Oct 21 2019, 09:18 AM
I’ve been following this thread and looking at the...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:05 PM
sir, 3rd and 4th dot matrix display flickering, ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 18 2019, 09:18 PM
Hopefully you understand some of the program.Which...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Thu Oct 17 2019, 09:38 PM