PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
pumper
Posts: 278
Joined: 10 Jan 2011 17:37

PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#1 Post by pumper » 08 Nov 2023 20:12

I am using this interrupt to sense wheel speed from 0 to 6 Hz by a Reed switch and magnet sensing on the going down side of the pulse.
I am using a 1K pull up resistor and a 0.1uF capacitor to GND.

Is this the best combination? I know that a 0.01Uf capacitor and 1k Resistor does not work.

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#2 Post by paulfjujo » 02 Dec 2023 20:38

Hello,

For this low frequency, maybe you can use an analog input and comparator instead of Int0 RB0 interrupt
wich works with Edge detection
With a filter you dont'get sharp edge ..

pumper
Posts: 278
Joined: 10 Jan 2011 17:37

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#3 Post by pumper » 02 Dec 2023 21:49

Thanks Paul.
The resistor and capacitor are not really a filter. The resistor just holds the signal at 5 volts. When the magnet closes the 0.1uF capacitor minimizes the reed switch flutter.
The RB0 interrupt port has a One Shot (not sure that is the correct name) to lock out the switch bounce so I get the interval of time between switch closures when the tire makes a revolution.
I know if I use too small of a capacitor like 0.01uF I get the flutter. A resistor from 1K to 10K has been used without any change in signal. If I use a 0.33uF capacitor I get an occasional false pulse around 5Hz. I am going to try a 0.033uF capacitor to see if the signal is better.

Maybe I have found a good combination with a resistor from 1K to 10K and a 0.1uF capacitor.

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#4 Post by paulfjujo » 04 Dec 2023 10:34

Hello Pumper,

I don't know, how you treat the RB0 interrupt .....

A better way is to use software solution,
a litle delay ,10 to 60mS inside the RB0 Int0 interrupt
to debounce the reed contact...
then read the satus of RB0 Pin input to confirm, yes , it is a true Low level 0
i get it ...to start the elapsed time measurement ..

...and stop it for the next RB0 Interrupt ...

the additional delai will not disturb the measurement ,
because included at both Start and STOP

at 6Hz -> T=166mS => minimum periode time width larger than debouce delay .

pumper
Posts: 278
Joined: 10 Jan 2011 17:37

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#5 Post by pumper » 04 Dec 2023 15:26

Hello Paul
Here is my Interrupt routine, it has some delay in it by by the code following the interrupt.
Can you show me how to calculate the time the interrupt routine takes?

Code: Select all

void interrupt(void)
  {if (PIR1.TMR1IF) //1:1 PRESCALER
       {// TMR1 OVERFLOW FLAG CAUSED THE INTERRUPT, WHEEL PULSE TIME
        TIMER1_OVFL++;   //65536 USEC PER INC
        PIR1.TMR1IF = 0;  //CLEAR THE INTERRUPT FLAG
       }

   if(INTCON.INT0IF == 1) //WHEEL PULSE caused interrupt
       {Inactive_Time = 0;  //GOT WHEEL PULSE TURN ON DISPLAY
        if(T1CON.TMR1ON == 1)
           {//GET PULSE TIME
            Lo(TMR1HI) =  TMR1L;
            Hi(TMR1HI) =  TMR1H;
            PulseTime = (unsigned long)TIMER1_OVFL*65536*1 + TMR1HI;
            if(PulseTime == 0) PulseTime = 35000000*4;
           }
        else  T1CON.TMR1ON = 1;//TIMER1 ON IF IT WAS OFF BY NO PULSE FOR SOME
                           //seconds IN MPH.c
        
        //RE-SET TIMER1
        TMR1H = 0;         //MUST SET TMR1H FIRST TO PUT IN BUFFER
        TMR1L = 0;         //RESET TIMER1
        TIMER1_OVFL = 0;   //REGISTERS

        //----------------------------------------------------------------------
        TRIP_WHEEL_REVS_COUNT++;  //32BIT
        if(TRIP_WHEEL_REVS_COUNT == TENTH_MILE)
         {MILES_TRIP++;  //IN TENTHS OF A MILE  16BIT
          TRIP_WHEEL_REVS_COUNT = 0; //FOR 8" TIRE 0.237% INACCURATE 10" 0.144%
          if(MILES_TRIP >= 65000) MILES_TRIP = 0; //6500.0 MILES RESET TO 0
         }
        //----------------------------------------------------------------------
        MILES_WHEEL_REVS_COUNT++;  //16BIT  ODOMETER
        if(MILES_WHEEL_REVS_COUNT == MILE_MILE)
          {MILES++;  //32BIT
           MILES_WHEEL_REVS_COUNT = 0;
           if(MILES > 99999) MILES = 0;
          }
       //***********************************************************************
       //FOR ASCENDING GAS MILES
       MILES_SERVICE_WHEEL_REVS_COUNT++;  //16BIT
       if(MILES_SERVICE_WHEEL_REVS_COUNT == MILE_MILE)
          {if(MILES_SERVICE_AC == MILES_SERVICE_SET)
             {MILES_SERVICE_AC = MILES_SERVICE_SET;//KEEP IT AT THIS VALUE
             }
           else
             {MILES_SERVICE_AC++; //INCREMENT MILES AFTER GETTING GAS
              MILES_SERVICE_WHEEL_REVS_COUNT = 0;
             }
          }
       //-----------------------------------------------------------------------

        INTCON.INT0IF = 0; //CLEAR THE INTERRUPT FLAG
       }

  } //END INTERRUPT ROUTINE

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#6 Post by paulfjujo » 06 Dec 2023 11:06

Hello Pumper,

* what is the purpose of Inactive_Time variable ?

* PulseTime = 35000000*4; // 140 000 000 how this value is fixed ? seems not related to FOSC MCU
* what is the FOSC value ?

* where, when , T1CON.TMR1ON is reset ?

* at wich speed is running the main loop of programme ? 1seconde ? more ?

At 1sec 1000mS for the main loop of programme ,and maxi speed 6Hz => 166mS
you have 1000mS - 166ms - 50mS delay - 1ms of interrupt treatment => 1000 - 217 = 783
you have 783ms to do else ... displaying data on LCD ?

to evaluate the Interrupt duration :
the best way is to use a Logic Analyser ...

else
you can use another 16 bit Timer with 1µS step 0 to 65535µS
65,535mS maxi enough for that
Start it at begining of interrupt ..stop at the end
and get result in the main program ..via printout on UART (terminal) or LCD..
you can disable the delay to avoid the adding of 50mS ..

pumper
Posts: 278
Joined: 10 Jan 2011 17:37

Re: PIC18F46K22 INTERRUPT ON PORTB.0 5 volts

#7 Post by pumper » 06 Dec 2023 14:27

Hello Paul
Here is answers to your questions

The Inactive Time is used by the main program to turn off the display in about 5 minutes of no wheel pulse.
PulseTime = 35000000*4 is so the main program loop will display 0 MPH
The FOSC is 16MHz
The Timer1 is reset in the interrupt with this code

Code: Select all

//RE-SET TIMER1
        TMR1H = 0;         //MUST SET TMR1H FIRST TO PUT IN BUFFER
        TMR1L = 0;         //RESET TIMER1
        TIMER1_OVFL = 0;   //REGISTERS
The main loop is executed at 7 to 10 times per second depending on what it is doing. It is doing nothing during the interrupt so the display is not updating.
The display has to show MPH, time of day, fuel status, trip time and miles and temperature. Non of these require fast updating unless the delay is seen by the human eye.

Post Reply

Return to “mikroC PRO for PIC General”