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

ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html
ExperimenterUK
Thu Jul 11 2019, 07:44 PM
@praveen123. Can you give a link to the project ?
Thu Jul 11 2019, 04:47 PM
ramos
buenos dias alguien podria ayudarme necesito saber como descargar la libreria del l298n para proteus
Thu Jul 11 2019, 11:11 AM
praveen123
anyone can help me
Thu Jul 11 2019, 11:11 AM
praveen123
i need source code for 8051 -smart trolley rfid
Thu Jul 11 2019, 11:10 AM
praveen123
hii
ExperimenterUK
Thu Jun 27 2019, 09:14 PM
Hi.. good to see you again
۞ TPS ۞
Thu Jun 27 2019, 07:31 AM
Hello
AdiGhazi
Tue Jun 18 2019, 11:21 AM
Hlo Dear , I've to program AT89C51AC3 RT-LUM using flip but I'm getting time out error for RS232. Will you please suggest me which programmer will be supportable to program this Chip.
Sun Jun 16 2019, 07:48 PM
hanoch Taub
i need to interface sim800 to 8051, the problem is that on the module sim800 there are 9 pins connector and I don't know how to connect him to 8051, which pins to tx and rx on 8051 module sim there is

Downloads

Comments

3AAPC
Sun Jul 21 2019, 12:41 AM
rpels7707
Fri Jul 19 2019, 11:31 AM
ameya123
Fri Jul 19 2019, 07:36 AM
anhsieuno
Thu Jul 18 2019, 11:39 AM
zxcvbnm1234
Sat Jul 13 2019, 07:09 AM
surajghayal
Wed Jul 10 2019, 01:17 PM
figureyang
Fri Jul 05 2019, 04:25 AM
mousompal
Sat Jun 29 2019, 05:02 PM

Online

Guests: 76, Members: 0 ...

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

Members: 38081
Newest member: 3AAPC
Forum Activity
3AAPC Avatar
Posted By 3AAPC
Sun Jul 21 2019, 12:51 AM
3AAPC Avatar
Posted By 3AAPC
Sun Jul 21 2019, 12:47 AM
With the GPS antenna in the outdoors, I use the ub...
ameya123 Avatar
Posted By ameya123
Fri Jul 19 2019, 07:42 AM
Hello,Please share 8051 Assembly language code for...
figureyang Avatar
Posted By figureyang
Fri Jul 05 2019, 04:37 AM
here is the source code prepared for you download ...
figureyang Avatar
Posted By figureyang
Fri Jul 05 2019, 04:30 AM
Cheap_Flash_FS(Normal Version)--Embedded FAT16/32 ...
wimac Avatar
Posted By wimac
Sat Jun 22 2019, 01:26 PM
I’ve use the AT89C51ED2 which has a similar progra...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Jun 20 2019, 10:08 PM
baud rate is 9600 at stop bits of 2. AdiGhaziThe u...
AdiGhazi Avatar
Posted By AdiGhazi
Thu Jun 20 2019, 05:24 AM
Dear,I'm using RS232 serial port cable to use flip...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jun 18 2019, 10:19 PM
Okay.. I have never programmed the AT89C51AC3 or ...
AdiGhazi Avatar
Posted By AdiGhazi
Tue Jun 18 2019, 11:20 AM
Hlo Dear ,I've to program AT89C51AC3 RT-LUM using ...