Working from Practical SXB Sample Book Examples
DigitalDj
Posts: 207
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
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
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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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:
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
(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
Any update for the group on the rest of this piece of literary/programming art?
Ken Gracey
Thanks,
Kevin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (DigitalDj) : 2/21/2008 11:36:52 PM GMT
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (DigitalDj) : 2/22/2008 3:18:03 AM GMT
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.
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
"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
·
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.
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