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

Thu May 23 2019, 03:55 AM
charmdatercw
cooking
DragonDad
Tue May 21 2019, 03:02 PM
Hi. Who is online?
Tue May 14 2019, 12:38 AM
Jayson Contreras
Hello
Xkalebar
Sun Apr 28 2019, 09:06 AM
Please provide me the code for gsm based sms controlled notice board
ExperimenterUK
Sun Apr 14 2019, 10:52 PM
@hassan malik post your problem in the "General help Guidance and Discussion" forum
Sun Apr 14 2019, 05:06 PM
RAJESH MAPARi
please send me code to interface atmega32 with 24c1024 serial eeprom
Sun Apr 14 2019, 01:33 PM
hassan malik
hello sir i installed proteus 7.8 professional on window 7 ultimate and window 10 but there would be same errors onn both windows. the error is bad license key.please any one help me
Sun Apr 14 2019, 01:32 PM
hassan malik
hello sir i installed proteus 7.8 professional on window 7 ultimate and window 10 but there would be same errors onn both windows. the error is bad license key.
ExperimenterUK
Sat Apr 06 2019, 05:59 PM
@deshrar. Please post your question in 8051 forum
Sat Apr 06 2019, 07:43 AM
deshraj
hallo mam.. i have downloded gsm notice board code its running fine on lcd but how so send msg to the module . i mean format of password and msg.. thank you

Downloads

Comments

DragonDad
Tue May 21 2019, 02:50 PM
dhiyamtech
Thu May 09 2019, 10:20 AM
harsha.reddy
Fri May 03 2019, 09:02 AM
soumen1963
Fri May 03 2019, 06:01 AM
Vasily
Mon Apr 29 2019, 04:03 AM
Xkalebar
Sun Apr 28 2019, 08:59 AM
obibikwe
Fri Apr 19 2019, 05:55 PM
gigyani
Thu Apr 18 2019, 01:43 AM

Online

Guests: 37, Members: 0 ...

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

Members: 38066
Newest member: DragonDad
Forum Activity
Ansh12 Avatar
Posted By Ansh12
Sun Apr 28 2019, 02:15 PM
I made a minor change, it works in Proteus.Try the...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Apr 25 2019, 08:20 PM
I made a minor change, it works in Proteus.Try the...
gigyani Avatar
Posted By gigyani
Thu Apr 25 2019, 01:17 AM
when i add cufirst.hex its not working.after 2-3 s...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Apr 24 2019, 12:29 AM
http://www.8051projects.net/download-d205-electron...
gigyani Avatar
Posted By gigyani
Tue Apr 23 2019, 09:22 PM
How to enter the electronic voting machine into vo...
Ansh12 Avatar
Posted By Ansh12
Mon Apr 22 2019, 09:12 AM
Then flash the led when you get any input on B0.Le...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Apr 19 2019, 05:35 PM
The full error is control_unit.asm(1294): error A4...
gigyani Avatar
Posted By gigyani
Thu Apr 18 2019, 10:09 PM
I'm using keil uvision5. attached is the rar file ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Apr 18 2019, 04:58 PM
Please zip your full code and post it.Which assemb...
gigyani Avatar
Posted By gigyani
Thu Apr 18 2019, 02:01 AM
STATREAD:MOV A, #WTCMD ;LOAD WRITE CO...