alpha91
Full Member level 3
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:
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.
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.