Saturday, January 27, 2018

Arduino & seven-Segment: Counter-Down / Counter-Up


To be easy to understand, a counter-up or counter-down is a term to mean 'to count by incrementing' or 'to count by decrementing'. However in the electronics term a counter is digital circuitry constructed by a number of flip-flops connected in cascades, it simply counts pulse signal that is commonly generated by an oscillator.
In this article today, we are going to try to implement the concept of this counter-up/down using arduino and sevent-segment. I am also going to explain a bit about bitmask and left-shift bitwise operations '<<'.

Firstly, just make the circui like presented below using proteus simulation:
Figure 1 Counter-up/down Simulation

Secondly, type the script below on the arduino IDE: 

const char segment_ch[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};

const uint8_t seg_sum = 8;
uint8_t counter = 0;
char add_or_dec = 1;

void setup() {
  // put your setup code here, to run once:

  for (uint8_t i = 0; i < seg_sum; i++)

    pinMode(i, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
 
  displaySeg(counter);

  counter += add_or_dec;

  if(counter >= 9) add_or_dec = -1;

  else if(counter <= 0) add_or_dec = 1;

  delay(250);

}

void displaySeg(const uint8_t seg) {

  for (uint8_t i = 0; i < seg_sum; i++)

    digitalWrite(i, (segment_ch[seg] << i) & 0x80);

}

Compile the code (Ctrl+R) and then paste the hex file that is on the arduino IDE into our simulation.


The following is an explanation of some pieces of source code above:
  • const char segment_ch[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
    -
    This line of code is a list of array characters (0-9) that we use for the seven-segment.

  • for (uint8_t i = 0; i < seg_sum; i++)
    pinMode(i, OUTPUT);
    -
    As we have known that on the arduino we need to configure the pin(s) first in the setup() scope. Here we need eight loop iterations for each of the pin that we are going to activate (use); from pin-0 to pin-7 are configured as OUTPUT (the pins are activated).
     
  • displaySeg(counter);
    -
    This function is to show integer figures of the seven-segment, as an example if counter==2, the character figure going to be displayed is 2. The explanation of this function is explained later.
     
  • counter += add_or_dec; //increment or decrement
    if(counter >= 9) add_or_dec = -1; //having reached 9 do decrementing
    else if(counter <= 0) add_or_dec = 1; //having reach 0 do incrementing
    delay(250); //wait for 0.25 second
    -
    In this line of code is where we apply the concept of cointer-up/down that is discussed earlier. For the state of add_or_dec variable will be altered (plus or minus) automatically when the value has reached 9 or 0: if add_or_dec is 1 (positive) the counter is increasing (counter-up) otherwise the value -1 (negative) the counter is decreasing (counter-down). Therefore on this line of code the integer figures showed on the seven-segment is decreasing (by one) or increasing (by one) continously with the delay of 0.25 second in each operation.
     
  • void displaySeg(const uint8_t seg) {
    for (uint8_t i = 0; i < seg_sum; i++)
    digitalWrite(i, (segment_ch[seg] << i) & 0x80);
    }
    -
    The displaySeg() function above as stated before will show any integer figure (0-9) accroding to any integer value passed to it.
    For the beginner the content of this function may look strange at first, but never mind! that is a process of learning beacause we cannot do any magic about code, even magicians need to learn, don't they? :D so let's go back to the topic, the content of the function contains:

    digitalWrite(i, (segment_ch[seg] << i) & 0x80);

    As we know that digitalWrite() function is used to trigger a pin of a particular register (for this case the pin-0 to the pin-7 is on D register), or for more simple term it is to control the pins state on the arduino board to be activated (HIGH) or deactivated (LOW). for example:

    digitalWrite(2,HIGH);

    note: HIGH and LOW can be represented by 1 and 0, therefore the above line of code is equivalent to:
    digitalWrite(2,1);
    The above code makes the pin-2 on the arduino boaard (PD2) in the HIGH state: This is how our program above works in which the each of the eight bits of the array element, segment_ch[] is shiftet and assigned one by one on the digitalWrite().
    To understand more easily of the concept, below is the sequence process of the operation:

    when i = 0, and seg = 2 (e.g., we want to display '2' integer figure to the seven-segment):

    pin-0(i) = (segment_ch[2] << 0) & 0x80 equivalent to
    pin-0(i) = (0x5b << 0) & 0x80 quivalent to
    pin-0(i) = (0b01011011 << 0) & 0b10000000 (in binary form), shift-left as 0
    pin-0(i) = 0b01011011 & 0b10000000
    0b01011011
    AND   0b10000000
    pin-0(i)= 0b00000000 (just take the 7th bit (MSB), since it's the mask-bit)
    pin-0(i)= 0 (LOW)

    On the sequence above, the shifting bit to the left is 0-fold (or there is no any shifting here) and masking the eight bits with 0x80 (& 0x80). This process results in state of LOW (0), therefore the arduino pin, the pin-0 has LOW (0) state.
    when i = 1:

    pin-1(i) = (segment_ch[2] << 1) & 0x80 equivalent to
    pin-1(i) = (0x5b << 1) & 0x80 equivalent to
    pin-1(i) = (0b01011011 << 1) & 0b10000000 (in binary form), shift-left as 1
    pin-1(i) = 0b10110110 & 0b10000000
    0b10110110
    AND   0b10000000
    pin-1(i)= 0b10000000 (just take the 7th bit (MSB), since it's the mask-bit)
    pin-1(i)= 1 (HIGH)

    On the sequence above, the shifting bit to the left is 1-fold and masking the eight bits with 0x80 (& 0x80). This process results in state of HIGH (1), therefore the arduino pin, the pin-1 has HIGH (1) state.
    when i = 2:

    pin-2(i) = (segment_ch[2] << 2) & 0x80 equivalent to
    pin-2(i) = (0x5b << 2) & 0x80 equivalent to
    pin-2(i) = (0b01011011 << 2) & 0b10000000 (in binary form), shift-left as 2
    pin-2(i) = 0b01101100 & 0b10000000
    0b01101100
    AND   0b10000000
    pin-2(i)= 0b00000000 (just take the 7th bit (MSB), since it's the mask-bit)
    pin-2(i)= 0 (LOW)

    On the sequence above, the shifting bit to the left is 2-fold and masking the eight bits with 0x80 (& 0x80). This process results in state of LOW (0), therefore the arduino pin, the pin-2 has LOW (0) state, and so forth till i is equal to 7 (as stated by for-loop above). The final result of this bit sequence to be:

    pin-0 = 0
    pin-1 = 1
    pin-2 = 0
    pin-3 = 1
    pin-4 = 1
    pin-5 = 0
    pin-6 = 1
    pin-7 = 1

    And then according to the circuitry on the figure 1 the character shown is '2'.

    Actually there is an easier way to show just any integer figure (0-9) like above, it is:

    switch(konter){
    ..
    case 2 :

    digitalWrite(0,LOW);
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
    digitalWrite(5,LOW);
    digitalWrite(6,HIGH);
    digitalWrite(7,HIGH);

    break;

    case 3: ...
    case 4: ...
    ..
    }

    Unfortunately by this way, our arduino IDE space, where we type our code will be burnt up and also tmay eat our arduino memory much if compared with the earlier code above.


So that is how to make our counter-up/down using arduino & seven-segment.

May you learn something new ..

No comments:

Post a Comment

Arduino & seven-Segment: Counter-Down / Counter-Up

To be easy to understand, a counter-up or counter-down is a term to mean 'to count by incrementing' or 'to count by decreme...