Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problems measuring time between two pulses

Status
Not open for further replies.

zanor

Member level 4
Joined
Feb 17, 2006
Messages
79
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Norway
Activity points
2,039
Just for fun I'm trying to make a digital RPM meter for my car. But I'm not able to get exact readings.
I have measured with an oscilloscope that there should be ~0,06802721 sec (14,7Hz) between the pulses when my engine is idling. "TMR_count" in my source code should be 66 then. But I'm getting 61. It is ~0.00512 sec too slow, why?
Is there some delays in timer/interrupt operation I don't know?

I am using a PIC16F84a on a breadboard. Could the "long" way from the crystal to the microchip slow it down? Cables are ~3-4 cm...

Here is my code, in MikroC format.
Code:
#include <built_in.h>                                                           // For Lo(); & Hi();

int i = 0;
int TMR_count = 0;  // 1x = 0.001024 sec
int TMR_value = 0;  // 1x = 0.000004 sec
int RPM = 0;
float TIME = 0;

void interrupt() {
  if (INTCON.INTF)
  {
    if (i <= 63)
    {
      TMR_value = TMR0;
      TMR0 = 0;

      TIME = (TMR_count * 0.001024) + (TMR_value * 0.000004);                   // Time between two sparks
      RPM = (1 / TIME) * 60;                                                    // RPM , 2 coiler and wasted spark = 1 spark/revolution

      EEPROM_WRITE(i, TMR_count);
      i++;
      EEPROM_WRITE(i, TMR_value);
      i++;
      
      TMR_count = 0;
    }
    INTCON.INTF = 0;
  }
  if (INTCON.T0IF)
  {
    TMR_count++;
    INTCON.T0IF = 0;
  }
}


void main() {
     PORTA = 0;
     TRISA = 0b00001000;
     PORTB = 0;
     TRISB = 0b00000001;

     PORTB.B3 = 1;
     Delay_MS(1000);
     PORTB.B3 = 0;
     
     INTCON.RBIF = 0;

     INTCON.GIE = 1;                                                            // Enable Interrupts
     INTCON.INTE = 1;                                                           // Enable Interrupt on RB
     INTCON.T0IE = 1;                                                           // Enable timer 0 owerflow interrupt
     
     OPTION_REG = 0x81;                                                         // Pullups DISABLED!!!
     
     while(1);

     }
 

Executing an EEPROM write in your interrupt function results in an exhausted EEPROM within at least a few hours and also huge timing errors.
 

    zanor

    Points: 2
    Helpful Answer Positive Rating
FvM said:
Executing an EEPROM write in your interrupt function results in an exhausted EEPROM within at least a few hours and also huge timing errors.

The EEPROM write is just for debugging, and is only filled once every time the chip is being reset.
I see now in the datasheet that it takes typicaly 4ms for one EEPROM write, that is WAY more than I thought.. Thank you :!:

I'm now going to make some chamges to my program and get back with the result :)

Added after 2 hours 51 minutes:

Cased closed, it was my EEPROM writes that delayed my timing.... Work fine now ;D
 

Is there some delays in timer/interrupt operation I don't know?

zanor,
Yes there is a timer between the interrupt is produce and the below line:
Code:
TMR_value = TMR0;
This is because the microcontroller always jumps to the same memory location when the interruption occurs to process that kind of interruption is.

Hope, that helps, regards,
Enzo.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top