Modulus in C

 A simple project to delve into Microcontroller C programming

Here is the code to run two 7 segments using one 8-bit port on a microcontroller, and one control pin.
The % operator is called modulus.

 byte CONST LED_MAP[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 
 void display_number(int n) {
  output_f(LED_MAP[n%10]);
  output_low(PIN_C1);
  delay_ms(2);
  output_high(PIN_C1);
 
output_f(LED_MAP[n/10]);
 
output_low(PIN_C1);
 
delay_ms(2);
 
output_high(PIN_C1);
 }
 
  void main(void) {
 
int n;
 
 
while(true) {
 
for(n=0;n<100;n++){
 
display_number(n);
}
 
}
  }

The reason it works is because of the Division and Remainder function and the basic qualities of the Decimal system.

 n/10 devides the numeral in count by 10 and saves only the whole number.
example
. 36/10 = 3
 n%10 devides the numeral in count by 10 and saves only the remainder.
example. 36%10 = 6

Post your comment

Comments

  • thank you

    Posted by farry, 27/09/2010 1:46am (7 years ago)

RSS feed for comments on this page | RSS feed for all comments