Custom Interrupts – PICmicro – Explanations

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
viki2000
Posts: 190
Joined: Mon Jul 07, 2014 9:38 am
Has thanked: 30 times
Been thanked: 77 times
Contact:

Custom Interrupts – PICmicro – Explanations

Post by viki2000 »

I am looking at Wiki help next pages:
https://www.matrixtsl.com/wikiv7/index. ... -_PICmicro
https://www.matrixtsl.com/wikiv7/index. ... sPIC/PIC24
Could you please explain it with details and simple next questions from “Handler code”:

1) When is that “Handler code” executed?

2) Why is the test condition “if (PIR1 & (1 << RCIF)) // Check to see if the interrupt flag is set” written like that?
I mean, RCIF is a bit in PIR1 register.
Why do you use Shift Left and Bitwise AND?
Why don’t you test only the bit RCIF like “if (RCIF)”?

3) When you call the macro “FCM_%n(); // Call Flowcode Macro” what are the symbols “%” and “n”? From where do you take these symbols? Why is written like that?
I am looking at C code properties:
https://www.matrixtsl.com/wikiv7/index. ... Properties
and I see “FCM_TEST()” to call a macro. So here why is different, why do you use %n?

4) What macro do you call?
If it is the macro specified in the Properties Interrupt icon at the bottom, then why do you have to specify it also here at “Handler code”? And here is not named. Why?

These things produce confusion and are not enough explained.
Could you please answer my questions above and provide some explanations around the “Handler code” from Custom Interrupts?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by LeighM »

1) When the interrupt occurs
2) This is related to V6 and the Boostc compiler, which uses a bit position count, other compilers often use a bit mask.
3) I think that looks like a leftover from a V5 example.
4) Whatever you want.

Note that these examples relate to using your own custom C code interrupt handlers.
Flowcode V7 has the "Interrupt Icon" which is capable of generating most interrupt handlers without the need to resort to custom ones.
Sorry if the WiKi sometimes causes confusion due to legacy information, hopefully it is something we can improve on.

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by LeighM »

Hi,
I have created and attached a custom interrupt example based on the I2C slave LED demo.
It compiles at least, but I have not had chance to test this on hardware yet.
Hope it helps.
Leigh
Attachments
PIC16F1937_I2C_Slave_LEDs_Interrupt.fcfx
(11 KiB) Downloaded 249 times

viki2000
Posts: 190
Joined: Mon Jul 07, 2014 9:38 am
Has thanked: 30 times
Been thanked: 77 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by viki2000 »

Hi,
Thank you for the suggested code. Unfortunately I cannot test it now. I can test it only in 4-5 weeks from now on.
Looking at the suggested code I can only make for the moment next observations:
- In the Disable Code area of the Interrupt, I think it should be “cr_bit(PIE1, SSPIE);” or “PIE1.SSPIE = 0;” or “SSPIE = 0;” whatever syntax is accepted by FC7.
- Inside to I2C macro should be in the beginning a Disable Interrupt icon.

viki2000
Posts: 190
Joined: Mon Jul 07, 2014 9:38 am
Has thanked: 30 times
Been thanked: 77 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by viki2000 »

The I2C interrupt is a good example for custom interrupt. I am glad you picked it up.
The Flowcode 7 has the extension .fcfx for the files, the same as Flowcode 6. The Flowcode 5 I think has the extension .fcf. The interrupt icon was present on Flowcode 5 and the I2C interrupt on Slave I2C was done since Flowcode 5 in 2010 here:
http://www.matrixtsl.com/mmforums/viewt ... ave#p17145
I did not test the code from link above, but I suppose is working, it has a lot of thumbs up.
Actually I am more interested about the explanations to the initial questions.
The main problem that I notice is that Flowcode does not have a user manual, something like a .pdf or .chm attached offline to the software. We have now: the Wiki help online database, the examples and the forum – all as free help. There are also Youtube examples and some other small forums with discussions – all these beside the paid support. There are pieces of code, like the one in discussion, which if are not explained by the Matrix engineers, then are hard to be understood, that’s why we need your support.
Let’s come back to my initial questions, because I am still confused, there are things that I do not understand clear and I am sure many other users too.
1)“When is that “Handler code” executed?”
“When the interrupt occurs.”
That is clear and I think understood. Let’s go in some details.
The interrupt icon has 3 fields. One for Enable Code, one for Disable Code and one for Handler Code.
When the flowchart diagram hits the Interrupt icon with Enable Interrupt, then the Enable Code is executed.
When the program reaches the Interrupt icon with Disable Interrupt, then the Disable Code is executed – here usually we disable the interrupt.
When the interrupt occurs then the Handler Code is executed and eventually will call a macro, a subroutine.
I hope I understood it right.
2)„Why is the test condition “if (PIR1 & (1 << RCIF)) // Check to see if the interrupt flag is set” written like that?”
“This is related to V6 and the Boostc compiler, which uses a bit position count, other compilers often use a bit mask.”
I do not understand this explanation. Could you please come with more details and explanations?
Here is why I do not understand it:
- If I take the decision from Handler Code “if (PIR1 & (1 << RCIF))” or from link above Slave code “if (pir1 & (1 <<SSPIF))” then RCIF is 1 bit in PIR1 register or SSPIF is 1 bit in PIR1 register. RCIF or SSPIF can have the value 0 or 1. Then the expression (1 << RCIF) means 1 in hex shift left with 0 or 1 positions, in other words multiplied with 2 power 0 or 2 power 1, meaning multiply with 1 or 2. Then the result is a Bitwise AND with PIR1 register. Then the evaluation of the expression results in 0x1h (0b00000001) or 0x2h (0b00000010) depending by the state of the last bits, which belong to Timer 1 or Timer 2 interrupt/overflow bits.
Here comes my problem.
No matter what complier or version of Flowcode is used, I do not understand how the expression “(PIR1 & (1 << RCIF))” or “(pir1 & (1 <<SSPIF))” is evaluated, because it seems it works in reality and only I have a problem to understand how is evaluated.
I need your help to explain me this evaluation step by step.
I would have expected to test only the RCIF or SSPIF bit. Why is used the entire register and how is evaluated properly? Why Timer 1 or 2 bits are involved?
It must be a question of logic and operation and should have nothing to do with the compiler or Flowcode version.
3)”When you call the macro “FCM_%n(); // Call Flowcode Macro” what are the symbols “%” and “n”? From where do you take these symbols? Why is written like that?”
“I think that looks like a leftover from a V5 example.”
But I see it also on FC6 and FC7 examples.
How should be then written the same example code from Wiki help for FC6 and FC7?
What language and sytax is used?
Is it C? Is it C combined with something specific from Flowcode?
Where are these symbols explained? That’s’ why I mentioned about user manual for Flowcode.
I need to understand what these symbols are, what they represent and from where are they coming from as syntax/language, not as previous version of Flowcode. I need to understand their meaning.
What is wrong with my understanding/evacuation of the expression?
4)“What macro do you call?”
“Whatever you want.”
I do not think so, unless is very clear specified as you did in your example code above.
But if the expression “FCM_%n();” is used, then does not mean that is implicit called the macro mentioned at the bottom of the window of the Enable Interrupt where is said “Will call macro” ?
Is it like that? Seems like that.
If is very clear specified as in your example “FCM_I2C();” then is no doubts we can call whatever macro we want, but if it is mentioned as “FCM_%n();” then I guess is called the macro mentioned at the bottom of the window of the Enable Interrupt where is said “Will call macro”.
Is it like that? Do I understand it right?
If yes, then again what does it mean “%n” and from what langugae7sytsmx is coming?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by LeighM »

Hi,
Just in answer to point (2)...
"SSPIF" does not in itself reference the bit in the correct register.
The compiler needs to understand the address location of the registers and their bit meanings, so these are defined in header files.
These differ between compiler versions.

For the V6 compiler the test would be

Code: Select all

if (pir1 & (1 <<SSPIF))
as the headers contain the following...

Code: Select all

#define PIR1                  0x0011 
volatile char pir1            @PIR1;
#define SSPIF                 0x0003 
Where SSPIF is a literal value 3, the bit shift count I referred to earlier.

For the V7 compiler things are different, the bit fields of the registers are defined as a union.

Code: Select all

typedef union {
    struct {
        unsigned TMR1IF                 :1;
        unsigned TMR2IF                 :1;
        unsigned CCP1IF                 :1;
        unsigned SSPIF                  :1;
        unsigned TXIF                   :1;
        unsigned RCIF                   :1;
        unsigned ADIF                   :1;
        unsigned TMR1GIF                :1;
    };
} PIR1bits_t;
extern volatile PIR1bits_t PIR1bits @ 0x011;
So to help users we have a test bit macro "ts_bit", so for the example we would use

Code: Select all

if (ts_bit(PIR1, SSPIF))
This is then the equivalent of

Code: Select all

if (PIR1bits.SSPIF)
Regarding points (3) and (4)
The "FCM_%n();” is incorrect for Flowcode 7, as you noted, my example had “FCM_I2C();”
So, as with variable names, the function names are upper case and these are prefixed by FCM_

Hope that explains,
Leigh

viki2000
Posts: 190
Joined: Mon Jul 07, 2014 9:38 am
Has thanked: 30 times
Been thanked: 77 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by viki2000 »

Thank you very much for the explanations. You brought light into the subject.
Nr. 2 is clear now.
Nr.3 and 4. are clear for FC7.
Would it be possible to explain "FCM_%n();” as I asked in the previous questions for Flowcode 6 and/or Flowcode 5?

P.S. How do I say thanks (thumbs up) in the new forum interface?

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Custom Interrupts – PICmicro – Explanations

Post by medelec35 »

viki2000 wrote:P.S. How do I say thanks (thumbs up) in the new forum interface?
Must be logged in first:
Thumbs up.png
(24.45 KiB) Downloaded 3609 times
Martin
Martin

Post Reply