Shop OBEX P1 Docs P2 Docs Learn Events
Dual LED Display Counter 00-99 — Parallax Forums

Dual LED Display Counter 00-99

CaineCaine Posts: 4
edited 2012-12-28 15:56 in Learn with BlocklyProp
First off, forgive the bold and larger font, my eyes see better with it this way.

Hi everyone, i'm just wondering how you would go about making a counter from 00 - 99 and repeat itself infinitely. I am doing this for a final exam in my class, and the first method i used was a sub routine method, i'll attach the code. But for my final exam, the teacher wants me to use 1 lookup table to accomplish the same task as i did with sub routines.

Thanks for looking.


~Caine




Sub-Routine Code I Made (teacher called it a brute force method, hehe)



' {$STAMP BS2}
' {$PBASIC 2.5}


     OUTH = 000000
     OUTL = 000000
     DIRH = 111111
     DIRL = 111111




       GOSUB First_Subroutine

       GOSUB Second_Subroutine

First_Subroutine:


                  '[SIZE=1]BAFG.CDE[/SIZE]
  OUTH = 100111
  PAUSE 250
  OUTH = 000100
  PAUSE 250
  OUTH = 010011
  PAUSE 250
  OUTH = 010110
  PAUSE 250
  OUTH = 110100
  PAUSE 250
  OUTH = 110110
  PAUSE 250
  OUTH = 110111
  PAUSE 250
  OUTH = 000100
  PAUSE 250
  OUTH = 110111
  PAUSE 250
  OUTH = 110110
  PAUSE 250

  RETURN


 Second_Subroutine:


                   '[SIZE=1]BAFG.CDE[/SIZE]
    OUTL = 000100
         GOSUB First_Subroutine
    OUTL = 010011
         GOSUB First_Subroutine
    OUTL = 010110
         GOSUB First_Subroutine
    OUTL = 110100
         GOSUB First_Subroutine
    OUTL = 110110
         GOSUB First_Subroutine
    OUTL = 110111
         GOSUB First_Subroutine
    OUTL = 000100
         GOSUB First_Subroutine
    OUTL = 110111
         GOSUB First_Subroutine
    OUTL = 110110

RETURN



Okay, i have the single digit 7 segment display connected to I/0 pins 8 - 15 and the tens digit 7 segment display connected to I/O pins 0 - 7. The #3 common cathode is connected to VSS (ground) on each display and the #8 common cathode is left un-used on both LED displays. I've run all power connections through a 1k (brown-black-red) Resistor.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-06 20:22
    Yep, it's brute force.

    Look in the Basic Stamp Syntax and Reference Manual in the sections describing the READ and DATA statements. The DATA statement will allow you to create tables, one table for each digit with a byte value for either each display column or row depending on how you have the display wired. You either output a row at a time or a column at a time with all the bits coming from one byte. The READ statement will let you read a byte from the table, then you'd output it by assigning the value to OUTL as you've shown above.

    Regarding what you posted ... I assume these numbers are supposed to be in binary, not decimal. If so, they need to have a % in front of them to tell the Stamp Editor that the numbers are binary.

    When you want some feedback on a program, you need to say something about what's connected to what. We can guess that I/O pins 0-7 are somehow connected to your LEDs as are I/O pins 8-15, but it's not at all clear how or what.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-06 20:24
    The LOOKUP statement is another good way to make a table and access its entries. How do you select one display digit or the other?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-06 20:50
    For posting code, look here:

    attachment.php?attachmentid=78421&d=1297987572

    Ok, so you have the least significant digit connected to I/O pins 8-15 and the most significant digit connected to I/O pins 0-7. There are lots of ways you can do this so you can get the right digits to the right places.

    Remember that, if you have a value in the variable NUMBER, then the least significant digit is NUMBER DIG 0 and the tens digit is NUMBER DIG 1. Look up the description of the DIG operator in the Manual.

    If you have a 16-bit word like OUTS, you can also look at it as two 8-bit bytes by using .BYTE(x) where x is a 0 or 1. Unfortunately, you've wired the digits so they'll be backwards, so you can write OUTS.BYTE(1-x) to refer to the tens digit by setting x to 1 and the ones digit by setting x to 0.

    You'll need a for loop with x (or something else) going from 0 to 1 and a LOOKUP statement with the index NUMBER DIG x. The selected lookup value gets put into OUTS.BYTE(1-x). Try it.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-06 21:24
    Remember that you've got lots of numbers that look like they're supposed to be binary, but they're decimal. You have to put a percent symbol (%) in front to make them binary.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-12-07 07:48
    The "Stamp Manual" is the "BASIC Stamp Syntax and Reference Manual" listed here (along with lots of other useful documentation). Look, starting on page 81, for info on OUTS and on page 117 for DIG.

    Here is a binary number using the [ code ] tags as described in "How to Post Code".
    %110101
    

    If you get an error message from your code, it helps to show or tell us what line the message referred to.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-07 09:17
    Mike and Caine.

    Sometimes the forum software messes up percent signs even when in code tags. I've had this happen several times but never when I've tried to reproduce it on purpose.
  • CaineCaine Posts: 4
    edited 2012-12-10 11:29
    This is what i ended up turning in guys, and i got an " A ". The professor said " Technically " i achieved what he wanted, making a counter 0 to 99 with one LOOKUP table. I didn't do it as he had hoped, and he said again this was more a brute force method, LOL. He says i should make use of the FOR / NEXT / IF / ELSEIF / ENDIF commands.

    [code]
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}


    index VAR Byte

    DEBUG " Counting 00-99 ", CR

    OUTH = 000000
    OUTL = 000000 'OUTH & OUTL initialized to low.
    DIRH = 111111
    DIRL = 111111 'Set P0-15 to all out-put low.

    DO

    FOR index = 0 TO 9

    LOOKUP index, [ 100111, 000100, 010011, 'Count 10, 20, 30...90
    010110, 110100, 110110,
    110111, 000100, 110111,
    110110 ], OUTL


    OUTH = 000100
    PAUSE 150
    OUTH = 010011
    PAUSE 150
    OUTH = 010110 'Count 1,2,3,4...9
    PAUSE 150
    OUTH = 110100
    PAUSE 150
    OUTH = 110110
    PAUSE 150
    OUTH = 110111
    PAUSE 150
    OUTH = 000100
    PAUSE 150
    OUTH = 110111
    PAUSE 150
    OUTH = 110110
    PAUSE 150
    OUTH = 100111
    PAUSE 150

    NEXT


    LOOP
  • CaineCaine Posts: 4
    edited 2012-12-10 11:34
    Trying to paste code like i was before, it keeps erasing numbers and percent signs.
  • tonyp12tonyp12 Posts: 1,951
    edited 2012-12-10 15:00
    As this a class assignment we can not tell you how to solve it, just teach you how to "think" in the right way.
    You want it to go from 0-99, using one lock-up table that will have a list of what pin should be low/high to show a led zero, one, two......

    I think you could do (pseudo code and not tested)

    A=0
    loop forever
    B =A %10 ' modulo
    set OUTL(B) ' B point to table.
    B =(A %100 -B) /10
    set OUTH(B) ' B point to table.
    A=A+1
  • CaineCaine Posts: 4
    edited 2012-12-14 06:23
    Thanks guys, i'm still playing around for a solution, but i got an " A " already on the final exam. The professor says in industry, it's best to use the shortest code possible.
  • ercoerco Posts: 20,256
    edited 2012-12-28 15:56
    Great job, congrats! Now that you got your 'A', I can safely show you this simple premade 4-digit LED counter. Just buying something premade is virtually no fun and completely bypasses the learning experience. But those amazingly cheap prices make them tempting nonetheless...

    http://www.ebay.com/itm/0-56-4-Digital-Red-LED-Digital-Frequency-Meter-Electronic-Counter-Multimeter-/251197986175?pt=LH_DefaultDomain_0&hash=item3a7c91157f
Sign In or Register to comment.