View previous topic :: View next topic |
Author |
Message |
Pichuqy_1
Joined: 03 Aug 2010 Posts: 38
|
|
Posted: Tue Feb 26, 2019 8:46 pm |
|
|
PCM programmer,
Could you explain to me, please, why the code #elif HIGHTOLOW, is never executed? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Feb 26, 2019 9:20 pm |
|
|
I'll try to explain...
That #if...else... is a 'pre processor' directive
..from the manual
The pre-processor evaluates the constant expression and if it is non-zero will process the lines up to the optional #ELSE or the #ENDIF.
So what happens is the compiler, on the 1st pass, determines which of the if 'sections' it will then 'process' or compile during the 2nd pass which then gets put INTO your program.
This is not the same 'If then else' that you put INTO your program that does 'this' or 'that' based upon variables when your program runs.
hope this helps
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 26, 2019 10:09 pm |
|
|
Pichuqy_1 wrote: |
What I interpreted with this code is that, for the falling edge, the #if
LOWTOHIGH is executed. And for rising edge the #elif HIGHTOLOW is
executed. Am I wrong?
|
You are wrong.
Pichuqy_1 wrote: |
But in this case the segment #if LOWTOHIGH is executed twice, and I can not
understand why. |
Use Google to read about the difference between "runtime" and "compile-time".
#if LOWTOHIGH is a compile-time command.
if(something) is a run-time command. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Feb 27, 2019 12:57 am |
|
|
Key is to get into your head, the difference between 'source code', and the
'runtime' code this generates.
#if is a command executed at _compile_ time, affecting which part of the
source code is actually compiled.
if is a command executed at _run time_ affecting which part of the code
actually executes. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun Mar 03, 2019 3:14 pm |
|
|
You're wrong..but I understand why you're confused.
You need to read Mt. T's reply again
#IF (condition is true...) CREATES code that gets loaded into the program that you download into the PIC
if (condition is true) EXECUTES code WHILE the program is running
If you look at some of the CCS suppled example programs you'll see at the top of the code something like
#IF __PCM__
...do this code
#ELIF __PCH__
...do this code
#ENDIF
What this is doing is telling the compiler to 'configure' or 'create' a section of code based upon what type of PIC you're using. It then adds that code
to the overall program THEN compiles it.
Only ONE section of code is put into the program.
I understand English is difficult ! I live in Canada and after 65 years STILL cannot understand some of it !!
Hope this helps
Jay |
|
|
|