+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Full Member level 3
    Points: 2,135, Level: 10
    Achievements:
    7 years registered

    Join Date
    Sep 2011
    Posts
    165
    Helped
    1 / 1
    Points
    2,135
    Level
    10

    Question regarding mask() function

    Dear all,
    recently i encounter " mask()" function while i am doing PIC (Microchip 16F628A) programming with MikroC.

    I search the explanation online then i found this :
    Applying the mask to the value means that we want to clear the first (higher) 4 bits, and keep the last (lower) 4 bits. Thus we have extracted the lower 4 bits. The result is:

    Mask: 00001111b
    Value: 01010101b
    Result: 00000101b

    uint8_t stuff(...) {
    uint8_t mask = 0x0f; // 00001111b
    uint8_t value = 0x55; // 01010101b
    return mask & value;
    }
    In this case i am understand the function.

    However, in the Decimal up down counter sample code i found

    the mask function code is as followed:


    number = mask(digit)
    The full code is as follow ;
    unsigned short mask(unsigned short num) {
    switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
    } //case end
    }

    unsigned int digit; // To Hold Decimal Value
    unsigned short number; // To Hold Equivalent Seven Segment Value

    void main() {
    CMCON |= 7; // Disable Comparators
    TRISB = 0x00; // Set PORTB direction to be output
    PORTB = 0x00; // Turn OFF LEDs on PORTB
    TRISA0_bit = 1; // PA.0 Input for Increment
    TRISA1_bit = 1; // PA.1 Input for Decrement

    digit = 0; // Initial Value of Counter
    number = mask(digit) ;
    PORTB = number;
    do {
    if (Button(&PORTA, 0, 1, 0)) { // Detect logical one to zero
    Delay_ms(300);
    digit ++; // Increase Counter
    number = mask(digit) ;
    PORTB = number;

    }
    if (Button(&PORTA, 1, 1, 0)) { // Detect logical one to zero
    Delay_ms(300) ;
    digit = digit-1; // Decrease Counter
    number = mask(digit) ;
    PORTB = number; // Update flag
    }
    } while(1); // endless loop
    }
    My question is, in this code, the mask value is 0?
    if that is the case, is the result, Number = 0 since all value is clear.

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 4
    Points: 2,383, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    231
    Helped
    10 / 10
    Points
    2,383
    Level
    11

    Re: Question regarding mask() function

    Code:
    unsigned short mask(unsigned short num) {
    switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
    } //case end
    }
    You can clearly see this is the mask function the input argument is the digit and the return value is number. if the digit is 0 the return value is 0x3F. So for each digit value the number changes.


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 80,963, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,270
    Helped
    4432 / 4432
    Points
    80,963
    Level
    69

    Re: Question regarding mask() function

    I think its more fundamental than that:

    The first function makes the top four bits 0000 and returns only the bottom four bits. So it is a 'mask' in the sense that it obscures half the data.

    The second function is a look-up table, it isn't a mask in the same sense as the first code. You can use a LUT as a mask but it is generally inefficient as it needs one entry for each possible input value. In that particular code, it looks to be a segment look up table for a 7-segment LED display anyway, nothing to do with a mask.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 259,183, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,250
    Helped
    13765 / 13765
    Points
    259,183
    Level
    100

    Re: Question regarding mask() function

    People are free to use the function name mask() for anything they want. In the second case, it's used for a 7-segment decoder, not actually related to a masking action.

    You are apparently confused by the function name.



    •   AltAdvertisement

        
       

  5. #5
    Full Member level 3
    Points: 2,135, Level: 10
    Achievements:
    7 years registered

    Join Date
    Sep 2011
    Posts
    165
    Helped
    1 / 1
    Points
    2,135
    Level
    10

    Re: Question regarding mask() function

    Quote Originally Posted by electronicsman View Post
    Code:
    unsigned short mask(unsigned short num) {
    switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
    } //case end
    }
    You can clearly see this is the mask function the input argument is the digit and the return value is number. if the digit is 0 the return value is 0x3F. So for each digit value the number changes.

    Quote Originally Posted by betwixt View Post
    I think its more fundamental than that:

    The first function makes the top four bits 0000 and returns only the bottom four bits. So it is a 'mask' in the sense that it obscures half the data.

    The second function is a look-up table, it isn't a mask in the same sense as the first code. You can use a LUT as a mask but it is generally inefficient as it needs one entry for each possible input value. In that particular code, it looks to be a segment look up table for a 7-segment LED display anyway, nothing to do with a mask.

    Brian.
    I see... So is that mean in the 7 segment LED program, the mask function serve as a compare and move value function?
    It compares the value of digit then move the value that set in each case out. Correct me if I'm wrong.

    Quote Originally Posted by FvM View Post
    People are free to use the function name mask() for anything they want. In the second case, it's used for a 7-segment decoder, not actually related to a masking action.

    You are apparently confused by the function name.
    Yes. I dont understand what is the "mask" function in the 7 segment LED. I search online for explanation and i found the first code.



  6. #6
    Super Moderator
    Points: 80,963, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,270
    Helped
    4432 / 4432
    Points
    80,963
    Level
    69

    Re: Question regarding mask() function

    There is no standard function called 'mask' in the 'C' language but the name is free to be used by whoever writes the program. One persons 'mask' function could be completely different to someone else's. However, the word 'mask' in English language means to hide part of something and if the first code you posted it actually does that. In the second code, I have no idea why they called it 'mask' but presumably it made sense to them. A function name like 'SegTable' or 'segment_lookup' would have been more explanatory.

    I see... So is that mean in the 7 segment LED program, the mask function serve as a compare and move value function?
    It compares the value of digit then move the value that set in each case out. Correct me if I'm wrong.
    Yes, in a way. The value 'num' is used as a parameter to the function call and it returns with the value matching num's entry in the table.
    For example if you use "xyz = mask(5);" it would pass 5 as num, match it in the case values and return 0x6D so the variable xyz would then hold 0x6D.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  7. #7
    Full Member level 3
    Points: 2,135, Level: 10
    Achievements:
    7 years registered

    Join Date
    Sep 2011
    Posts
    165
    Helped
    1 / 1
    Points
    2,135
    Level
    10

    Re: Question regarding mask() function

    Quote Originally Posted by betwixt View Post
    There is no standard function called 'mask' in the 'C' language but the name is free to be used by whoever writes the program. One persons 'mask' function could be completely different to someone else's. However, the word 'mask' in English language means to hide part of something and if the first code you posted it actually does that. In the second code, I have no idea why they called it 'mask' but presumably it made sense to them. A function name like 'SegTable' or 'segment_lookup' would have been more explanatory.


    Yes, in a way. The value 'num' is used as a parameter to the function call and it returns with the value matching num's entry in the table.
    For example if you use "xyz = mask(5);" it would pass 5 as num, match it in the case values and return 0x6D so the variable xyz would then hold 0x6D.

    Brian.
    I see. thank you very much for your explanation.
    Sorry, i have one more question. I just realize that in this code, the case statement dont have "break;"
    May I know why?



    •   AltAdvertisement

        
       

  8. #8
    Full Member level 4
    Points: 2,383, Level: 11
    Achievements:
    7 years registered

    Join Date
    May 2012
    Posts
    231
    Helped
    10 / 10
    Points
    2,383
    Level
    11

    Re: Question regarding mask() function

    As per the syntax break should be there. Since "return" statement is there it will return from the function once match happens, hence it does not impact the functionality.



  9. #9
    Super Moderator
    Points: 80,963, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,270
    Helped
    4432 / 4432
    Points
    80,963
    Level
    69

    Re: Question regarding mask() function

    I just realize that in this code, the case statement dont have "break;"
    'break' forces a return at that point in the switch statement but the 'return' does that anyway in that code so it isn't necessary.
    What it is missing is a 'default:' line which would ensure it returns a known value if a match is not found. At the moment, if a switch value that isn't present in the case statements is used, the return value could be anything. For example:
    Code:
    unsigned short mask(unsigned short num) {
    switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
    default: return 0x00;
    } //case end
    }
    would ensure 0x00 was returned if num was greater than 9.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



--[[ ]]--