NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Dont understand the &= and |=
April 05, 2012 by Akeshish ![]() |
Hello everyone, I understand the functions of AND and OR, i just done understand when you use one over the other in register setting. It seems to me that you use &= to change from 1 to a 0
is there more to it? Is there a way in C to just directly change a register example in VHDL we would use something like PORTC(4) = '1'; or place a logic 1 in register 4 of PORTC. |
---|---|
April 06, 2012 by pcbolt ![]() |
Akeshish - There's just a little bit more to it. When you use the OR operator with 1, you not only change 0 to 1 but also 1 to 1. So if 'x' is unknown, x | 1 will always be 1. When you use the AND operator with 0, x & 0 will always equal 0. When you use multi-bit numbers, bit 1 of the first number will only interact with bit 1 of the second number. Same with all the other bits that make up the two numbers. You can certainly make your own "PORTC(4) = 1" type function and use it in your projects. Or go further and create a whole library (like lcd.c/lcd.h) for you to include. |
April 06, 2012 by Akeshish ![]() |
Thank you, I do understand how it works, i was just a bit confused on the last project of the tutorial. In one case the tutorial says to use an OR operation to change the bit and the next time around it says to use an AND operation. It was a bit confusing because both operations where trying to change a single bit in a register. Did i read it wrong? |
April 06, 2012 by Rick_S ![]() |
Typically AND is used to clear a bit and OR is used to set a bit. This is because with a simple mask the logic allows for this. Some people make up macros for C to make setting and clearing or register bits a little more similar to what you are used to. I don't do that myself so I'm not going to try to describe that process but if you search the forums here, you'll probably find it. One user that hasn't been around here in a while that had some excellent macro's for this was NOTER. You might try searching for him or looking at his posts in the library section if you are interested. Rick |
April 06, 2012 by Ralphxyz ![]() |
And then just to add to the confusion:
As opposed to:
Ralph |
April 06, 2012 by Akeshish ![]() |
Thanks guys |
April 06, 2012 by Akeshish ![]() |
Hey Ralph, I tried to google a ASCII code chart but cant seem to find 0xFO types of codes. Im kinda of new to that style of coding. Can you link me to a reference. |
April 06, 2012 by pcbolt ![]() |
Akeshish - Ralph is just using hexadecimal notation to change the port direction pins. The 0x part means "treat what follows as a hexadecimal number". It's much easier to write 0xFF than it is to write 11111111 in binary notation. 0xF0 is the same as 11110000. Keep in mind that when you write to a register you MUST write 8-bits at a time. Since each register controls 8 pins, you have to be careful when you write to that register NOT to disturb the other pins. That is what Rick was referring to as "masking". When you use this code;
This actually expands to;
Since PC3 is just a symbol for the number 3, we get;
Or;
Or;
Since you do not know what DDRC is, use x's to indicate its value (x can only be 1 or 0)
Now x | 1 = 1 and x | 0 = x, we get;
And the pin is set! To clear the pin use the following facts x & 1 = x and x & 0 = 0. If we work it backwards, we want;
|
Please log in to post a reply.
Did you know that the microcontroller's crystal oscillator can be used to keep accurate time? Learn more...
|