Tuesday, 25 November 2014

Masking

On the atmega16, to read the input from a port x, we use the PINx command. Now this command reads the input from the entire port.

Now consider a situation where you have connected 8 switches - one to each pin of PORT C.
However, now you want to only read the switches connected to PC3 and PC0. This is where masking comes into picture.

Masking, in simple words is nothing but hiding the information that is not required. In the situation we considered above, we do not require the input from all pins except PC3 and PC0. Therefore, we need to mask these pins.

A simple way to achieve this is to use the AND Mask.

AND Mask:

While using the AND masking technique, we perform a bitwise AND operation on the input we recieve from PINx command. We AND the input bit with 0 if the bit needs to be masked, else we AND it with 1.
Considering the above example, since we require the input only from PC3 and PC0, we mask all other bits by ANDing them with 0. We AND PC3 and PC0 with 1. Hence, the output that we get will contain all bits as 0 except PC3 and PC0, where these two bits will have the values read from the PINC command.


Masking techniques can not only be used while taking inputs, but also while reading bits from certain registers and providing output as well.

Use of masking technique can be seen here:
http://avrlogic.blogspot.in/2014/11/switch-interfacing.html



No comments:

Post a Comment