Loop: // loop starts again and again
if (S1 = 1) L1 = 1 ; else L1 = 0 ;
if (S2 = 1) L2 = 1 ; else L2 = 0 ;
if (S3 = 1) L3 = 1 ; else L3 = 0 ;
if (S4 = 1) L4 = 1 ; else L4 = 0 ;
if (S5 = 1) L5 = 1 ; else L5 = 0 ;
goto loop;
In this code the code "S1=1" .. "S5 =1" will always evaluate to 1 which is seen as true in C.
So all of these if statments could be written as "if(1)" so you code is the same as
Code:
Loop: // loop starts again and again
if (1) L1 = 1 ; else L1 = 0 ;
if (1) L2 = 1 ; else L2 = 0 ;
if (1) L3 = 1 ; else L3 = 0 ;
if (1) L4 = 1 ; else L4 = 0 ;
if (1) L5 = 1 ; else L5 = 0 ;
goto loop;
Given that the code now has all "if(1)"'s we know they will always be true and write the code as suggested by Ian
But if you use the == operator in the if statements the result will depend on the value of variables S1 .. S5. So the = and == operators do different things and you must use the correct one.
I wrote some code last week and used a bunch of #defines like this found myself wanting to use the port pin names. I then started to think about this and realized that i didn't have any more chips and that's where them #defines shine change only in one place the port pins like from PORTB to PORTC in my case.
But I don't think it was meaningful, maybe a name like SW_RB0 or something like that. But then if you change ports.
So a SW0 goes on any PORTX.0 pin would be the way to go
I was going to post it's a input is why it worked,But Mike pointed that out I wouldn't use the one = even if it works because you'll run into places it doesn't work and wonder for hours why.
I've done that often using C and C++ seeing I used basic and it doesn't care if it's one = it looks at how your using. C is looking a lot better the more I use it.
Hi Pommie,
I did this as college assignment, starting with circuit, flow chart, up to the program. For user friendly terms, I've used SW1 to indicate switch 1 rather clear than PORTA.F2that only Pic hobbies know.
The reason that the single equals works is because it is testing an input. Writing 1 to an input has no effect and so the if looks at the pin value.
...
I get your point Pommie but is that really correct? I thought the compiler would convert
if(blah = 1)
to a statement that writes 1 to blah then always executes the if() as it must be 1, ie always true?
I assume that as the compiler is pretty dumb regarding what is an input and what is not, so it would treat most labels the same.
In MikroC from what I have seen it seems to treat PIC ports just as another memory location and make the same compiled code for anything which is a port (or hardware register) or a RAM location. The compiler just handles the bank selecting if needed and reads/writes that location.
I get your point Pommie but is that really correct? I thought the compiler would convert
if(blah = 1)
to a statement that writes 1 to blah then always executes the if() as it must be 1, ie always true?
I assume that as the compiler is pretty dumb regarding what is an input and what is not, so it would treat most labels the same.
In MikroC from what I have seen it seems to treat PIC ports just as another memory location and make the same compiled code for anything which is a port (or hardware register) or a RAM location. The compiler just handles the bank selecting if needed and reads/writes that location.
So it looks as though first 1 is assigned to PORTB, then the if() is done by PORTB being tested for true or false by the MOVF,F and the result will depend on how PORTB is read.
So you were correct if PORTB is an input the if() test can actually test the inputs, however it won't test for if(PORTB==1) it will actually test if(PORTB!=0) and will include ALL input AND output pins! Pretty nasty.
(edit) I just tested with a variable and I was wrong there the variable is treated differently;
The optimiser removed the if() test. As you said the PORT was treated as volatile and if I changed the variable foob to volatile the result was the same as the PORT example.
It sure appears that way. However it might also be the optimiser which is usually done in pre-processing, so if the optimiser assumed the if() would always be true it might be removing the if() test, and basically overriding whether the variable is volatile or not because there is no longer a test on the variable.
Thanks for the example Mike that was pretty much what i assumed the compiler would do to it which i said in post #12.
So you were correct if PORTB is an input the if() test can actually test the inputs, however it won't test for if(PORTB==1) it will actually test if(PORTB!=0) and will include ALL input AND output pins! Pretty nasty.