Shop OBEX P1 Docs P2 Docs Learn Events
Display variable in binary using LEDs — Parallax Forums

Display variable in binary using LEDs

skylightskylight Posts: 1,915
edited 2012-07-20 10:11 in Propeller 1
Source Code: lesson5.spin

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  
VAR

 byte LED
  
PUB LedCounter                   ' Method declaration 
                       
    dira[16..23] := %1111_1111   ' Set P16 thru P23 to output
    
    repeat LED from 0 to 255   ' Repeat 256 times, counting forward(substituting 255 for 256 here causes an endless loop)     
     
        outa[16..23] := LED      ' Set display number in binary on LEDs   
        waitcnt(clkfreq/8 + cnt) ' Wait a moment

I have been going over the lessons for the quickstart board again and just to see what would happen, in the repeat line I substituted 255 for 256, expecting a compile error or some sort of error due to the LED variable being declared as only a byte but it compiled and ran ok and I noticed it had created an endless loop instead of stopping as before.
This seems a quick and easy way to get an endless loop but the question is will this "bad practice" cause problems in future programming? or is it an acceptable way of forcing a program not to stop? Am i heading for trouble doing it this way?

Comments

  • pik33pik33 Posts: 2,413
    edited 2012-07-20 04:53
    Byte variable can never be 256, a loop end condition can never be met so it is endless loop

    This is simply a bug, we don' know if byte variable overflow doesn't cause any side effects, and this kind of programming should be avoided. Better add another loop. This is clean, simple and understandable.
    CON
        _clkmode = xtal1 + pll16x
       _xinfreq = 5_000_000
     
    VAR
       byte LED
    
    PUB LedCounter          ' Method declaration
                                  
    dira[16..23] := 11_1111   ' Set P16 thru P23 to output
         repeat
           repeat LED from 0 to 255   ' Repeat 256 times, counting forward(substituting 255 for 256 here causes an endless loop)
               outa[16..23] := LED      ' Set display number in binary on LEDs            
               waitcnt(clkfreq/8 + cnt) ' Wait a moment
    
    
  • skylightskylight Posts: 1,915
    edited 2012-07-20 04:58
    Thanks pik33 for the quick reply, I thought it would be the case but was just wondering if eventually it may cause problems such as an overflow or something like that?
  • cavelambcavelamb Posts: 724
    edited 2012-07-20 08:08
    One small change to display the full 8 bits.
    The "1" in the dira mask need to be a binary number.
    111111 is decimal.
    CON
        _clkmode = xtal1 + pll16x
       _xinfreq = 5_000_000
     
    VAR
       byte LED
    
    PUB LedCounter                          ' Method declaration
                                  
    dira[16..23] := 111111            ' Set P16 thru P23 to output 
         repeat
           repeat LED from 0 to 255     ' Repeat 256 times, counting forward(substituting 255 for 256 here causes an endless loop)
               outa[16..23] := LED          ' Set display number in binary on LEDs            
               waitcnt(clkfreq/8 + cnt)     ' Wait a moment
    
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-07-20 08:20
    I bet the forum ate the % sign - it does that sometimes.

    Preface the 111111 with % in your code and all should be well.
  • cavelambcavelamb Posts: 724
    edited 2012-07-20 09:11
    mindrobots wrote: »
    I bet the forum ate the % sign - it does that sometimes.

    Preface the 111111 with % in your code and all should be well.


    Holy Cow! It did.
    It also ate a couple of 1's off of the dira mask.
    Thanks for pointing that out.

    (I ain't saying it!)
  • skylightskylight Posts: 1,915
    edited 2012-07-20 10:11
    Phil Pilgrim points out in the "Support for using this forum" section that the advanced text editor messes about with code even if between tags, his suggestion to use the basic text editor works as I found out today.
    One problem is that the post new thread button sends you to the advanced editor but the reply to post button doesn't I ended up having to switch off the advanced editor in settings! I will ask if anything can be done about it in the support section.
Sign In or Register to comment.