@Suraj143
It will work.
The point into the variable name isn't a good choice. Some compilers use it to seperate a variable name from a choosen bit in it.
But be warned, in your variables are stored the values 1, 2, 4, 8 an so on.
So, when testing don't test for 1 - if(PORTB_B2==1) - instead test for greater 0 - if(PORTB_B2>0) -.
Another possible writing is:
PORTC_B5 = Temp & (1<<2);
volatile bit PORTC_B1 ; Defination as bit
When using bit variables you have to set it with an if argument if((temp&0b00000001)>0){PORTC_B1=1;}
But this can help to save RAM space.
In the light of day, I'm going to doubt my answer. Some compilers will cast a byte value to a bit by simply taking bit 0 of the byte. So you need to try it with your compiler. You may need,
This is still writing single bits to the port (the compiler will use BSF or BCF), which is in risk of a "read modify write" error that PICs are famous for.
A better system would be to use a shadow register for any output port, and manipulate the pits int he shadow register and then write the whole 8bit shadow register to the port. Once you have done the shadow register code I think the original problem would have been sorted.
I don't actually understand the question "Is my method ok". If it works, it is ok. Did you have some kind of trouble using your own method, or are you just insecure?
I don't know if the "PORTB.B0" expression is correct. It is not ANSI C, but some compilers implement extra features for convenience. What compiler do you use?
It will work ok if you drop the " !=0" at the end.... (Temp & 0b00000001) will return true (1) if Temp.0 = 1, and false if Temp.0 = 0
It wont work if you don't drop the "!=0" at the end.... (Temp & 0b00000001) returns true which isn't 0 (!=0), and true as well if (Temp & 0b00000001) returns false which IS != 0.
It will work ok if you drop the " !=0" at the end.... (Temp & 0b00000001) will return true (1) if Temp.0 = 1, and false if Temp.0 = 0
It wont work if you don't drop the "!=0" at the end.... (Temp & 0b00000001) returns true which isn't 0 (!=0), and true as well if (Temp & 0b00000001) returns false which IS != 0.
Sorry Ian, Temp & 0b00000001 returns 1 or 0, Temp && 0b00000001 returns True or False. However, Temp && 0b00000001 with Temp=128 will return true which is not the desired result. (They are both non zero)
The next line is also a problem,
PORTB.B2 = Temp & 0b00000010;
It will return 2 or 0. Some compilers when casting a byte to a bit simple use the bottom bit and so this will also be wrong.
Changing to,
PORTB.B2 = ((Temp & 0b00000010)!=0);
Will return True if the bit is set and so will work.
Ok Pommie.. but every C compiler I've worked with states the a 0 is false ANYTHING else is true... If the compiler tries to assign an integer, will not generate a warning it will set the bit to true. I think the ambiguity is with c++ compilers... they DO need the correct syntax.
But the test has to be (Temp & 0x01) other wise the option that Mister T applied.
No it shouldn't, it should evaluate to 0 or 2. On the line RC0=Temp & 0b00000010; there is no reason for the compiler to treat it as boolean and if it did then it would be wrong. That is also the reason I added !=0 to force it to boolean. I'm sorry to keep going on about it but it's really important that the OP understands the difference.
If you mean "I never use below method what you have mentioned.":
That is OK. The ternary conditional is difficult to read and easy to replace with if/else statement: https://embeddedgurus.com/stack-overflow/2008/06/