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.

PIC multiple interrupts

Status
Not open for further replies.

nikens

Junior Member level 2
Joined
Dec 20, 2009
Messages
23
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
slovenia
Activity points
1,503
I would like to read digital signal from hall efect speed sensor and I was thinking of making two interrup routines. In first I'll be generating Timer0 interrupt that will increase counter on timer overflow.
Second interrupt will occur on sensor signal change connected to PIC input and will increase variable X.

In my main program I'll be watching the counter (counting 100 mili sec time interval) and X (counting sensor latch) and compare how many times signal changed its state over 100 mili sec interval.

Now do I need to use 18F family PIC or is there a solution to use 16F instead and use only one interrupt at the time?

I was thinking if it is possible to switch between interrupts in the program and thus using more than one intterupt (but not at the same time).
 

You can use PIC 16 since the two interrupt sources would raise different flags. So, you can check those two flags, one for TMR0, the other for the change.

You can use PIC 18 and use the interrupts (high and low priorities) for two interrupt service routines.

I was thinking if it is possible to switch between interrupts in the program and thus using more than one intterupt (but not at the same time).

This is what is usually done with the PIC 16. Multiple interrupt sources are set. Each raises a different flag. In the single ISR, each of the required flags is checked to see which interrupt has occurred.

Hope this helps.
Tahmid.
 
  • Like
Reactions: nikens

    nikens

    Points: 2
    Helpful Answer Positive Rating
Helps a lot, you are great. Thought that I was unintelligible since shahbaz.ele did not understand me.
If it is possible I'll preffer to use 16F since my PCB is allready made for DIP14 PIC MCU (for solder through hole ,18F family only comes with minimum of DIP18 and I am a bit low on housing space)
Be greatful if you could explain me in details how to make that. I'll be using mikroPascal compiler by MikroE to make my program and for now I am only able to make a working program with either TMR0 interrupt or RA2 external interrupt.
 
16F has only one interrupt vector. But all interrupts jump to that vector. So, in the ISR, you can check which interrupt has occurred by checking the corresponding flag.

Eg. If you're using INT0 for reading the square wave and TMR0 overflow, you can set the INT0IE and TMR0IE. In the ISR, check if INT0IF has been set. Also check if TMR0IF has been set. If INT0IF has been set, INT0 interrupt has occurred; if TMR0IF has been set, TMR0 has overflown. You then need to do what you need and then clear the flags and return from interrupt.

Eg:
Code:
void interrupt(void){ //Interrupt service routine
   if (TMR0IF_bit == 1){
      counter++;
      //............ increase variable counter
      TMR0IF_bit = 0;
   }
   if (INT0IF_bit == 1){
      X++;
      //............. increase variable X
      INT0IF_bit = 0;
   }
}

The above syntax is for mikroC PRO for PIC.

EDIT: I see you've now mentioned that you're using mikroPASCAL. I don't know PASCAL syntax, but I hope you can use this code snippet to understand what I mean.

Hope this helps.
Tahmid.
 
yes code is completly understandable, it won't be problem for me to transform into mpascal, since the logics is the same.
Also I read that better response is achived if you calculate speed based on measuring the time between two signal changes, rader than counting signal changes in a specific time interval.

So I was thinking like:
Code:
procedure Interrupt;
begin
  if INTCON.T0IF=1 then
   begin
    Inc(counter);        //increase counter on Timer0 overflow 
    INTCON.T0IF:=0;      //clear TOIF bit (interupt flag)
   end;
  if INTCON.INTF=1 then
   begin
    Inc(x);                // If the external flag is 1, raise x
    INTCON.INTF:=0;        //clear INTF bit (interrupt flag)
   end;
end;

//main program  measure time it takes for three ext interrupts to occur
if x=0 then old_counter:=counter;
if x=3 then 
begin
  x:=0;
  speed:=some_number / (counter-old_counter);
end;


PS I was also thinking of using CCP in capture mode instead. RC5 pin of PIC16F616 has schmitt triger input and this would also probably help if the signal is not perfect square.
 

Yes, it'd be more accurate if you measured time difference between two signal changes. You've probably read how. But if you're confused I'll try to explain later.

For that purpose, using the capture mode of CCP would be better.
 
  • Like
Reactions: nikens

    nikens

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top