I asked the below questions of Kevin Watson in a PM. He answered them and ask that I post them publicly because he thought others might have the same questions. The code that I refer to is on his website (
www.kevin.org/frc).
My message:
Kevin-
Everything you have posted has been extremely helpful and has cleared up alot of stuff in my mind. One little thing that I don't understang is in your edu_interrupts template. on or about line 140 of user_routines_fast.c you use this line of code:
if (Port_B_Delta & 0x10)
....
Int_3_Handler(Port_B & 0x10 ? 1:0)
a few questions....
1. what does 'Port_B_Delta & 0x10' and Port_B & 0x10 ? 1:0 represent?
2. why don't you use:
if (PORTBbits.RB0)
3. what is it that you are sending to interrupt3_handler?
4. why are you sending it?
His answers:
Quote:
Originally Posted by mightywombat
1. what does 'Port_B_Delta & 0x10'...
Port_B_Delta contains a map of interrupt input bits that have changed since the last time an interrupt 3-6 happened. Logically anding with 0x10 isolates the interrupt 3 bit. If the result after this anding is not '0' (i.e., the interrupt input changed), the associated interrupt handler is called. Without something like Port_B_Delta, you won't be able to tell which bit(s) have changed and which interrupt handler to call.
Quote:
Originally Posted by mightywombat
...and Port_B & 0x10 ? 1:0 represent?
This expression evaluates as 0x01 if the interrupt input is a '1' or 0x00 if the interrupt input is a '0'. Just sending in 'Port_B & 0x10' evaluates to 0x08 if the input is a '1'. I just wanted to be consistant by sending in a 0x01 or 0x00 to each interrupt handler.
Quote:
Originally Posted by mightywombat
2. why don't you use: if (PORTBbits.RB0)
Because you want to call the interrupt handler if the interrupt pin has changed state (i.e., '0'->'1' or '1'->'0'). 'if(PORTBbits.RB0)'would only call the interrupt handler if the pin has changed from a '0' to a '1', which may be a desirable mode of operation if you only want to call your interrupt handler on a rising edge (i.e., a transition from '0' to '1' on the pin).
Quote:
Originally Posted by mightywombat
3. what is it that you are sending to interrupt3_handler?
4. why are you sending it?
So that the interrupt handler knows why the interrupt was generated, I send in the current state of the pin. If the value is 1, the handler knows that the pin just changed from a '0' to a '1'. conversely, if '0' is passed in, the handler knows that the pin just changed from a '1' to a '0'
An example of where you might want to use this functionality is to measure the amount of time a pulse is a '1'. When the interrupt handler is called on the rising edge, the handler can start a timer and then exit. When the interrupt handler is called again on the falling edge, the handler will stop the timer and see how much time has elapsed. This pulse width encoding is how data is transferred on the PWM outputs, where a 0.9ms pulse is 0, 1.5ms is 127 and a 2.1ms pulse is 255.
Let me know if you're still unclear on any of this stuff.
-Kevin
(end message)
I hope this helps.