Question regarding mask( ) function

Status
Not open for further replies.

alpha91

Full Member level 3
Joined
Sep 23, 2011
Messages
168
Helped
1
Reputation
2
Reaction score
2
Trophy points
1,298
Activity points
2,625
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 :

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 ;

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.
 

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.
 
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.
 
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.
 

Re: Question regarding mask() function




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.

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.
 

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.
 
Re: Question regarding mask() function


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?
 

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.
 

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.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…