Shop OBEX P1 Docs P2 Docs Learn Events
Working from Practical SXB Sample Book Examples — Parallax Forums

Working from Practical SXB Sample Book Examples

DigitalDjDigitalDj Posts: 207
edited 2008-02-24 05:45 in General Discussion
Hello All,

I am having a little trouble understanding what is actually going on in this code example. I do know that the ~ means not. So if it is a 1 it will be changed to 0 in the case of Led0 = ~Led0. The problem I'm having is understanding what is decrementing and the values it is doing with ctrl, TLed0 and TLed1. If someone could take a few minutes to explain some things as I need help I would appreciate it!

Thanks,
Kevin

' =========================================================================
'
'   File...... Simple Template
'   Purpose... Blink9.SXB
'   Author....  
'   E-mail....  
'   Started...
'   Updated...
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------
 
DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000

' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

Led0  Pin RB.0 Output
Led1  Pin RB.1 Output
 
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

TLed0  Con 1  'multiplier for Led 0
Tled1  Con 7  'multiplier for Led 1
 
' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
 
Ctrl0  Var Byte  'Timing Control for Led 0
Ctrl1  Var Byte  'Timing Control for Led 1
 
' =========================================================================
  PROGRAM Start
' =========================================================================
 
Start:
 If Ctrl0 = 0 Then   'Timer expired?
   Led0 = ~Led0    'Invert led state
   Ctrl0 = TLed0    'Reload timer
 Else
   Dec Ctrl0    'Count down
 EndIf
 
 If Ctrl1 = 0 Then
   Led1 = ~Led1
   Ctrl1 = TLed1
Else
   Dec Ctrl1
EndIf
 
Pause 100
Goto Start

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • John CoutureJohn Couture Posts: 370
    edited 2008-02-21 04:30
    Kevin,
    I am currently teaching a class using this book also.· Jon Williams was demonstrating LED's blinking at different rates.
    LED0 will blink 7 times for every once that LED1 blinks.

    The way I approach it is start with a blank piece of paper and show the values of LED0, LED1, CTRL0 and CTRL1 as I step through the program.
    Thus:
    *********** LED0·· LED1·· CTRL0·· CTRL1
    At Startup:··0····· 0····· 0······ 0
    ·········· · 1············ 1
    ··········· ······· 1············· 7
    ··········· ······· 0··············6
    ············ 0············ 1
    etc.

    In other words, LED0 changes every time and LED1 changes once every 7 loops.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
  • DigitalDjDigitalDj Posts: 207
    edited 2008-02-21 13:11
    Thanks John for the response!

    Ok so everything starts out at 0, so the decrement doesn't go -0 or back around to 255 in this case? Where you don't have any numbers in your chart would be zeroes corect?

    I guess i'm used to seeing where there would be a higher value to decrement from.

    See if I understand this correctly on program flow

    ································ First Pass:······························································· ·Second Pass and Third pass

    If Ctrl0 = 0 Then·········· 'Ctrl0 is at zero at program start····································Ctrl0 = 1·then goes to Else and·Dec Ctrl0 to 0 makes If Then true
    ·· Led0 = ~Led0············'Led0 being at zero now changes to 1··turns on RB.0·········Led0 = 1 and changes to 0 turns off RB.0
    ·· Ctrl0 = TLed0············ ' Ctrl0 now = Tled0 which is constant 1··························Ctrl0 now at 0 changes back to 1
    ·Else
    ·· Dec Ctrl0
    ·EndIf

    All of this goes on while Led1 has it on on and off times!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-02-21 16:00
    Kevin,

    You don't seem to understand the IF-THEN-ELSE structure; only one part of it runs: the THEN section runs only if ctrl0 is equal to zero, otherwise the code in the ELSE section runs. So, if ctrl0 is not zero then the LED toggling and reinitializing is skipped as the program jumps to the ELSE section where ctrl0 is decremented. There is no possibility of wrap around of the variable.

    Keep in mind that IF-THEN-ELSE is not something we can do in assembly, so we synthesize it. Here's what that code would look like without IF-THEN-ELSE; this might help you understand the program flow:

    IF_0:
      IF ctrl0 <> 0 THEN ELSE_0
        Led0 = ~Led0
        ctrl0 = TLed0
        GOTO ENDIF_0
    
    ELSE_0:
        DEC ctrl0
    
    ENDIF_0:
    


    Note that we have to invert the logic internally, i.e., we use ctrl <> 0 to skip past the THEN section. Note, too, that the compiler inserts a GOTO at the end of the THEN section to skip past the ELSE section.


    BTW, in my world, variables start with a lowercase letter so that I can distinguish them from constants that start with an uppercase letter.

    Post Edited (JonnyMac) : 2/21/2008 10:17:45 PM GMT
  • John CoutureJohn Couture Posts: 370
    edited 2008-02-21 18:31
    Kevin,

    (Bean and Jon, this would be easier with line numbers in the source code :-)

    Here is a couple more iterations.· Print out on graph paper to get vertical lines to make it easier to see which variables are changing.



    ***********············· LED0·· LED1·· CTRL0·· CTRL1
    At Startup:··············· 0····· 0····· 0······ 0
    Start:
    · If ctrl0 = 0 then· ' true
    ···· led0 = ~led0········· 1·
    ···· ctrl0 = Tled0······················ 1
    · ELSE
    ···· 'not executed
    · ENDIF

    · IF ctrl1 = 0 then· ' true
    ···· led1 = ~led1················ 1
    ···· ctrl1 = tled1······························ 7
    · else
    ···· 'not executed
    · endif
    · pause 100
    · goto start

    '=====================
    'SECOND TIME THROUGH
    '=====================
    Start:
    · If ctrl0 = 0 then· ' false
    ···· ' not executed
    · ELSE
    ···· DEC ctrl0·························· 0
    · ENDIF

    · IF ctrl1 = 0 then· ' false
    · else
    ···· dec ctrl1·································· 6
    · endif
    · pause 100
    · goto start

    '=====================
    'THIRD TIME THROUGH
    '=====================
    Start:
    · If ctrl0 = 0 then· ' true
    ···· led0 = ~led0··············· 0·
    ···· ctrl0 = Tled0······················ 1

    · ELSE
    ···· ' not executed
    · ENDIF

    · IF ctrl1 = 0 then· ' false
    · else
    ···· dec ctrl1································· 5
    · endif
    · pause 100
    · goto start


    '=====================
    'FOURTH TIME THROUGH
    '=====================
    Start:
    · If ctrl0 = 0 then· ' false
    ···· ' not executed
    · ELSE
    ···· DEC ctrl0·························· 0
    · ENDIF

    · IF ctrl1 = 0 then· ' false
    · else
    ···· dec ctrl1·································· 5
    · endif
    · pause 100
    · goto start


    etc.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-02-21 22:14
    Neither Bean nor Jon have any control over adding line numbers to the source code.
  • Ken GraceyKen Gracey Posts: 7,404
    edited 2008-02-21 22:40
    JonnyMac,

    Any update for the group on the rest of this piece of literary/programming art?

    Ken Gracey
  • DigitalDjDigitalDj Posts: 207
    edited 2008-02-21 23:29
    Ok, Got It! Just a little stumbling block and·luckily I didn't scratch my knees!

    Thanks,

    Kevin




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



    Post Edited (DigitalDj) : 2/21/2008 11:36:52 PM GMT
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-02-21 23:49
    Fun stuff is afoot Ken and gang, and I sincerely appreciate your patience with me. In addition to learning a lot about the SX, I've learned that it's harder to write about writing programs, than it is to write programs! [noparse];)[/noparse]
  • DigitalDjDigitalDj Posts: 207
    edited 2008-02-22 03:10
    Ok not stay on this to long but I thought I had it figured out but for some reason I may be missing something typo whatever. What is happening is when the code starts out now it turns both leds on then in the begining
    then after 3 pulses of the first led the 2nd one comes on. It stays on for 2 - 2 1/2 pulses then goes off for 2 pulses of the first led. I know this shouldn't be possible and it should be once for every 7.

    Can someone look over the code and see what I am doing wrong?

    Thanks,
    Kevin


    ' =========================================================================
    '
    '   File...... Simple Template
    '   Purpose... 
    '   Author....  
    '   E-mail....  
    '   Started...
    '   Updated...
    '
    ' =========================================================================
    
    ' -------------------------------------------------------------------------
    ' Program Description
    ' -------------------------------------------------------------------------
    
    ' -------------------------------------------------------------------------
    ' Device Settings
    ' -------------------------------------------------------------------------
     
    DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
    FREQ            4_000_000
    
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    
    Led0  Pin RB.0 Output
    Led1  Pin RB.1 Output
     
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    
    TLed0  Con 1  'multiplier for Led 0
    TLed1  Con 7  'multiplier for Led 1
     
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
     
    ctrl0  Var Byte  'Timing Control for Led 0
    ctrl1  Var Byte  'Timing Control for Led 1
     
    ' =========================================================================
      PROGRAM Start
    ' =========================================================================
     
    Start:
    
    If ctrl0 = 0 Then  'Timer expired?
       Led0 = ~Led0    'Invert led state
       ctrl0 = TLed0   'Reload timer
    Else
       Dec ctrl0       'Count down
    EndIf
     
    If ctrl1 = 0 Then
       Led1 = ~Led1
       ctrl1 = TLed1
    Else
       Dec ctrl1
    EndIf
     
    Pause 500
    Goto Start
    


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



    Post Edited (DigitalDj) : 2/22/2008 3:18:03 AM GMT
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-02-22 07:55
    Two things: 1) You're WAY over-thinking this stuff which is leading to 2) You're not observing accurately (my opinion). Zero is a valid number and you seem to be skipping it. Led0 cycles every other time through the loop (0..1 = 2 cycles). Led1 cycles every eighth time through the loop (0..7 = 8 cycles).

    If you have an SX-Key then you should add a variable called iterations and watch the program flow via Debug-->Run. You'll see that Led0 changes state on iterations 1, 3, 5, 7, etc., and Led1 changes state on 1, 9, 17, 25, 33, etc.... both are behaving just as they should.

    Start:
      IF ctrl0 = 0 THEN
        Led0 = ~Led0
        ctrl0 = TLed0
      ELSE
        DEC ctrl0
      ENDIF
    
      IF ctrl1 = 0 THEN
        Led1 = ~Led1
        ctrl1 = TLed1
      ELSE
        DEC ctrl1
      ENDIF
    
      INC iterations
    
      WATCH ctrl0, 8, udec
      WATCH ctrl1, 8, udec
      WATCH iterations, 8, udec
      BREAK
    
      GOTO Start
    
  • DigitalDjDigitalDj Posts: 207
    edited 2008-02-22 12:54
    I understand what is going on with program flow now, I just can't figure out why it is doing what it is doing as explained above. It bothers me when people can't answer a direct question and go everywhere else around it.

    What do you mean that I am overthinking this stuff, you know it comes easy to you guys because you know it. I take this as an insult to my intelligence. I am just trying to figure out why I am getting what I am getting so I can understand it. If I'm that big a nunuisance then just don't respond. It seems you guys don't have any problem helping anybody but it seems when it comes to me you have a problem even from the very beginning of being on this forum. Tell me what it you guys don't like about me, let's clear the air and get it over with! Now i'm pissed, thanks!

    Kevin




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



    Post Edited (DigitalDj) : 2/22/2008 1:05:55 PM GMT
  • BeanBean Posts: 8,129
    edited 2008-02-22 13:13
    Kevin,
    "Overthinking" is not a insult. If anything it is a compliement. You are trying your best to make the problem harder than it is.
    It helps to pretend you are the SX. Go through each commands and do on paper what the SX would do.

    There are many ways to do the same thing. I suspect that Jon has chosen a way that very different from how you would do it.

    You are correct, when you understand something it seems very easy. And I can tell you that a person that understands something very well is a usually NOT a good teacher. Because they sometimes assume too much.

    Keep working on it. Maybe write a program to do the same thing in your own way.

    I know Jon pretty well, and I highly doubt he meant his comment as an insult. Please don't assume that.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-02-22 15:57
    Kevin,

    I'm sorry you took it as an insult -- as Bean points out that was absolutely NOT my intention. Sometimes we get so close to the tree we can't tell if it's a pine or an oak, you know what I mean? Trust me, I know, especially when it comes to the SX as I've written a lot of code for it this past year. I have had some really frustrating days where problems just slayed me, only to have one of my very smart tech friends point out that I was over-thinking the problem, and that things were simpler than I was making them.

    If I didn't care about helping you (and others), I wouldn't have shown you another way of looking into the program to "see" what's going on (our eyes are easily fooled). I'll be more careful with my responses to your posts.
  • DigitalDjDigitalDj Posts: 207
    edited 2008-02-24 05:45
    OK apology accepted and sorry I took it the wrong way!

    Tried the Inc Iterations and that line is giving me a variable expected when compiling, I have all the code as typed above.

    I think I also know what you mean when the code is running with the cycles, after thinking about it my code is running as it should.

    Thanks,

    Kevin




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔



    Post Edited (DigitalDj) : 2/24/2008 5:54:25 AM GMT
Sign In or Register to comment.