Thread: Microcontroler masking reg66x.h

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    35

    Microcontroler masking reg66x.h

    Here's the code:

    Code:
    #define mask 0x04
    int main(void)
     {
    while(P3 & mask);
     //CODE
    }

    My question is about the line "while(P3 & mask)"


    OK, so the objective of the program is to wait until the input at port3 of the micro controller is 0000 00100 in binary (0x04 hex, as defined in line 1)

    But i don't see the point to the masking? Also why do you AND it because sure P3 will only ever be 0x00 OR 0x04?

    Can anyone explain this method pleasee

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But i don't see the point to the masking?
    Because P3 probably has 8 bits, you're only interested in one of those bits.

    Also why do you AND it because sure P3 will only ever be 0x00 OR 0x04?
    No P3 (port 3) has at least 8 bits 11111111 is possible.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    >> the objective of the program is to wait until the input at port3 of the micro controller is 0000 00100 in binary

    Not quite. This implies you want to know the entire value of that port (each and every bit), and would be tested with: while(P3 != 0x04)

    >> But i don't see the point to the masking?

    Because this program is only interested in a single bit on P3 - bit 2 specifically. The AND clears the rest of the bits, and the result is thereby determined by the value of bit 2.

    Code:
    Example 1:  Bit 2 is high:
    
    P3      xxxx x1xx
    MASK    0000 0100
            ---------
    Result: 0000 0100 = true
    
    
    Exampe 2:  Bit 2 is low:
    
    P3      xxxx x0xx
    MASK    0000 0100
            ---------
    Result: 0000 0000 = false

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    35
    awww gottcha thank you ever so much!

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I assume the definition for P3 declares it to be volatile (able to be changed by something outside your code), otherwise, your compiler may optimize by only testing P3 one time.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I assume the definition for P3 declares it to be volatile
    This is an embedded processor, P3 usually relates to a specific hardware address for Input/Output port number 3. It was declared in the header file as:

    Code:
    sfr  P3      =   0xB0;
    In this compiler sfr is a space qualifier that contains the registers used to address the microcontroller's internal peripherals.

    Jim
    Last edited by jimblumberg; 04-26-2013 at 03:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Masking
    By fiendslyr in forum C Programming
    Replies: 11
    Last Post: 06-25-2010, 01:59 PM
  2. Bit Masking
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 01-08-2007, 02:23 AM
  3. Masking(or ...)
    By suzanne_lim in forum C Programming
    Replies: 5
    Last Post: 03-07-2006, 01:37 AM
  4. Cin Masking
    By Dunners in forum C++ Programming
    Replies: 8
    Last Post: 02-20-2005, 08:14 PM
  5. Masking
    By Bazzz in forum C Programming
    Replies: 1
    Last Post: 05-12-2004, 06:40 AM