SPIN "VAR" Theory Question

I have been tinkering with Mr. McPhalen's "jm_sled4c_demo". I have a ten LED bar graph connected to the MC14489BPE chip. I started typing the program again so I would always have the original for when I screw it up trying to figure out how to program the chip through the Propeller. I came across my first issue... I ran the program after completion of typing it. The LEDs did not perform in the same manner as Mr. McPhalen's program.

I found the VAR section I copied, by me typing, was not the same. I had put the "disp1, disp2,..." in numbered order instead of "disp5, disp4,...".

Question: Why would reversing the declaration of variables affect the program from the original?

I thought the variables got declared and the programming didn't care in what order, just as long as they were there.
Jason
GySgt USMC(ret)
Dayton, OH

Comments

  • 6 Comments sorted by Date Added Votes
  • JonnyMacJonnyMac Posts: 5,858
    edited November 13 Vote Up0Vote Down
    I thought the variables got declared and the programming didn't care in what order, just as long as they were there.
    Most of the time that is true, but in some cases we use a block of variables as an implicit array and the order of declaration matters. It does in this program. I should have put a comment in the VAR section to warn not to do this. If you look at the code you'll see this:
      bytefill(@dpctrl, 0, 6)
    
    This line fills dpctrl and the next five bytes with 0 -- if you changed the order variables are declared you could be fouling other variables, or even the top of your code.

    To update the display, we manipulate the display registers and then do this:
      shiftout(@dpctrl, 6, 4)
    
    Note that we're passing the address of dpctrl and telling the method that we want to send six bytes (dpctrl plus the next five bytes in RAM). The display expects those bytes to arrive in a specific order -- if you change the order of declaration you will foul the display.

    You could, of course, call shiftout() six times with the individual byte in the order the display wants, but that would be clunky:
      shiftout(@dpctrl, 1, 4)
      shiftout(@disp5, 1, 4)
      shiftout(@disp4, 1, 4)
      shiftout(@disp3, 1, 4)
      shiftout(@disp2, 1, 4)
      shiftout(@disp1, 1, 4)
    
    You'd also have to change the code that sets all display registers to zero.

    I wrote shiftout() the way I did so an address and the number of bytes are passed so that a display update call can be handled in a single line. The requirement for this is that the display register variables appear in a specific order.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Jon,

    Thank you for responding so quickly and for the little theory class. I learn alot better by understanding the underlying theory about why something was wrote the way it was. I am trying to do the Science_Fiction LED program that is in the Stamp Works Manual that you authored. I have no idea even where to begin to transpose BASIC into SPIN due to I am still learning how to use SPIN. I completed the PE Kit Lab Manual but even that material still has me scratching my head...missing some of the theory about why the program was written the way it was.

    I got the below code to look like the Science_fiction LEDs,,,I think. Remember the LEDs on the black Trans Am 80's TV show? Trying to do that with the MC14489. Works straight off the Propeller, but I think there is a better way. I'm having trouble seeing past this code.

    repeat
    repeat 10

    if outa[0..9] == 0
    outa[0..9] := %1000000000

    waitcnt(clkfreq/16 + cnt) ' Looks better with divide by 32
    outa[0..9] >>= 1

    repeat 10

    if outa[0..9] == 0
    outa[0..9] := %0000000001

    waitcnt(clkfreq/16 + cnt) ' Looks better with divide by 32
    outa[0..9] <<= 1

    I'm not asking to write you to write the code but could you provide me a more efficient programming path to take? In addition, could you and other members suggest which keywords and direction to take to program the MC14489 to do the same thing; the Science_Fiction LEDs. Thank you.
    Jason
    GySgt USMC(ret)
    Dayton, OH
  • JonnyMacJonnyMac Posts: 5,858
    edited November 13 Vote Up0Vote Down
    Gunny (May I call you that? -- I was a SSgt in the USAF/R),

    I started learning BASIC in 1980 and in 1994 bought my first BASIC Stamp. You don't always have to do things the direct Spin way -- you can use objects to make the code a bit more "comfortable." That's part of the point of objects and libraries. The display you're talking about is called a Larsen scanner, named for Glen A. Larson who created Battlestar Galactica (Cylons) and the Knightrider (Kitt car) that used this display. I live and work in Hollywood, so I have lots of practice with the Larson scanner.

    Using my io and time objects, I can make the code more like PBASIC. Here's how I would rewrite your basic scanner:
      repeat
        repeat 10
          repeat pin from 0 to 8
            io.high(pin)
            time.pause(60)
            io.low(pin)
          repeat pin from 9 to 1
            io.high(pin)
            time.pause(60)
            io.low(pin)
    
    For people coming from PBASIC, this is pretty easy to understand.

    Note that I use offset groups for the back and forth motion -- this keeps the ends from having an undesirable bounce delay.

    Creating a Larsen scanner on discrete outputs is easy; it's going to be a pain-in-the-backside using the MC14489. Why? Because the "No Decode" mode -- that you need for individual bit control -- only controls segments A..D, E..G are turned off, H (decimal point is quirky to control). You might want to reconsider using this chip for Larsen scanners.

    Since it may be all you have, for giggles I did a quick review of the MC14489 data sheet and came up with this. Given the limits of the MC14489, you max out a 20 individual LEDs for the Larsen scanner.
    pub main | bit, reg                                                   
                                                                     
      setup
    
      config := %00111111                                           ' set config byte (no decode on all)
      shiftout(@config, 1, 8)                                         ' send it
    
      bytefill(@dpctrl, 0, 6)                                       ' initialize array
    
      repeat
        repeat bit from 0 to 18                                     ' forward
          larsen := 1 << bit                                        '  update bit position
          repeat reg from 0 to 4                                    '  loop thru display regs
            disp1[reg] := larsen & %1111                            '   get nibble from larsen bits
            larsen >>= 4                                            '   adjust for next nibble
          shiftout(@dpctrl, 6, 4)                                   '  update outputs
          time.pause(60)                                            '  hold  
          
        repeat bit from 19 to 1
          larsen := 1 << bit               
          repeat reg from 0 to 4           
            disp1[reg] := larsen & %1111   
            larsen >>= 4                    
          shiftout(@dpctrl, 6, 4)          
          time.pause(60)
    

    For fun, you can see a different take on a Larsen scanner in this picture of artist and cosplayer Shea Standefer wearing a Cylon dress at San Diego Comic Con a few years ago. This uses smart LEDs (WS2812) so I was able to create a blended effect for a larger "eye" spot that fades off at the edges. Shea was able to control the animation of the Cylon display during her stroll down the catwalk. We also had an "up close" mode (dim) for the after party that would not blind the guests.

    17a765b41e3d0ff145695bab115299a7.jpg

    You can see of the pictures of my "Hollywood" stuff on my Pinterest page:
    -- https://www.pinterest.com/jonmcphalen/techno-art/

    Notice that it's all about very fancy control of LEDs -- sometimes a whole lot of them. Almost all of the project on the page are controlled with the Propeller (sometimes multiples), all coded in Spin.

    I've attached a couple archives with the code ideas so you have access to my io and time objects. They're very simple, and I use them in every program I write.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Jon,

    Sure! You can call me Gunny! Once a Marine, Always a Marine!

    Thank you for sending me your program code for the io and time objects. I do recall seeing these before and how they were used. I should be able to implement them easy enough.
    Jason
    GySgt USMC(ret)
    Dayton, OH
  • HBD 242, Gunny
  • OOH RAH!!

    Thank you, MrBi11!!
    Jason
    GySgt USMC(ret)
    Dayton, OH
Sign In or Register to comment.