View Full Version : Right Brain Programmer, need a little help :>)

John Michael
11-18-2011, 01:14 PM
Hello All, I am learning how to program the propeller with a PDB and using Viewport. I can come up with ideas on what to do with the Propeller all day long but the Programming is coming slow. I figured the Best way to become a Prop programmer is to take it a step at a time with a fun example and comment the code to death :>) At the moment I am just playing with lighting some LED's and monitoring the timing via Viewport.
This code is working on my PDB but would like someone to look at my comments and Correct me where my understanding of the code is wrong. With respect to a few lines I don't have a clue and need a full explanation, as with respect to masking the LED's, ect.
I have learned at a late stage in the game how important "Reading Comprehension" is ... Many people will comment with something like "Just read the Thread so in so or follow the example in so in so. Simply put for me it takes a little more explaning :>) and I beleive that there are many people out there like myself that understand the Hardware better than the Programming side of things.

I hope this Thread will help more "Right Brained Programmers" with a Simple example ...

__________________________________________________ __________________________________________________ __________________________________________________ __________________________________________

_CLKMODE = XTAL1 + PLL16X 'Sets Propeller Clock mode for 80 Mhz.
_XINFREQ = 5_000_000 'Crystal Base Frequency to be multiplied by 16 "the statement above".

WAIT = 3_000_000 'Wait 3,000,000 Cycles.

long frame[1600] 'Array that holds 1600 Longs.
long delay, leds, T, mask 'Varibles, type Long "32" Bits.

qs : "QuickSample" 'Include the QuickSample Object in my Project.
vp : "Conduit" 'Include the Conduit Object in my Project.
Serial : "FullDuplexSerial_rr004" 'Serial Driver Object "Handles Serial Routines".

PUB LedControl 'The name of this Function and the Starting point of the Program.

vp.register(qs.sampleINA(@frame,4)) 'Tell the compiler to to use the QuickSample object.
vp.share(@delay, @leds) 'Share address of delay & leds with Viewport.

dira[4..9]~~ 'Set Pins 4-9 to Outputs.
us := clkfreq/1_000_000 'Set the the varible "us" to 80.
delay := 10_000 'Delay is equal to 125 Micro-Seconds at 80 Mhz.
T := cnt 'T = the Internal Clocks counter.
mask := %111<<7 'Mask the LED outputs? need help with this.

repeat 'Repeat the following in a Loop.
waitcnt(T+=(delay*us)) 'Need help with this.
outa[6..4]++ 'Increment the output of LED's 4 through 6.
outa[9..7]:=(leds&mask)>>7 'Need hekp with this line of code.

Serial.Stop 'Stop the Serial Cog
Serial.rxFlush 'Flush Receive Buffer.
Serial.start(8,7,2,9600) 'Start Serial Driver in a New Cog", Parameters: Pin(8)Receive, Pin(7)Transmit, Mode=2 & Baud=9600.
waitcnt(10_000_000 +cnt) 'Wait for the Clock counter to increment 125 Micro-seconds.
Serial.tx(20) 'Transmit data out on Pin- 7.
waitcnt(10_000_000 +cnt) 'Wait for the Counter to go through 10,000 cycles.
Serial.tx(16) 'Transmit data out on Pin- 7.

John Michael
11-18-2011, 01:19 PM
Also, can someone tell me how to attach the code within a "Grayed out Box" correctly so it is easier to read. I'm as Green as a tommato :>)

11-18-2011, 02:02 PM
{code} {/code} -- with the { } replaced by [ ] is good for showing code:

mask := %111<<7 'Mask the LED outputs? need help with this.

The '%' operator shows the following number is binary -- base 2. It's a lot easier to see what's happening with bits using binary. Decimal and hexadecimal (base 216) would be '7' .

The '<<' operator says to shift the number -- in this case %111 -- to the left n times -- in this case, 7 times. The command is telling the micro "Work like a shift register, with a load of %111, and do 7 shifts."

The first shift will result in a value of %1110, the second in a value of %11100, up to the seventh shift to give a value of %11_1000_0000. (In SPIN the '_' is seen as a comma in "normal" life. It's there to make reading the number easier for us humans; the compiler ignores it.)

outa[9..7]:=(leds&mask)>>7 'Need hekp with this line of code.

This takes the value in leds, and does a bitwise AND with the value of mask (think of a bunch of AND gates, with one set of inputs being the leds value, and the other inputs the mask value). Then it takes the result and reverses what the '<<' operator did. So if the result of the AND was %10_1000_0000, you will have %101 when the shift-right (>>) command is finished. And then this value will be sent to Pins 9..7.

I'll leave the description of the WAITCNT to someone more familiar with it than I am.


John Michael
11-18-2011, 06:57 PM
[Testing the Brackets]]

John Michael
11-18-2011, 06:59 PM
Thanks so much Rich, that makes sense ...
I found a good explanation for the waitcnt command.

11-18-2011, 07:39 PM
http://forums.parallax.com/attachment.php?attachmentid=78421&d=1297987572 (http://forums.parallax.com/showthread.php?129690&p=978076&viewfull=1#post9780 76)

11-19-2011, 03:43 AM
Hi John,
Glad you're giving the Propeller a spin! You'll soon get the hang of it, it's well thought out.
I'll contribute a better comment for:
vp.register(qs.sampleINA(@frame,4)) 'Tell the compiler to to use the QuickSample object.
could be:
'Start sampling the INA port into the frame array and share it with ViewPort for display

John Michael
11-19-2011, 12:21 PM
Thank you Hanno, that's is definetly Better :>)