Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

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

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.


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.

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

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?
 

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top