Right Brain Programmer, need a little help :>)
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 ...
________________________________________________________________________________________________________________________________________________________________________________________________
CON
_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.
VAR
long frame[1600] 'Array that holds 1600 Longs.
long delay, leds, T, mask 'Varibles, type Long "32" Bits.
OBJ
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.
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 ...
________________________________________________________________________________________________________________________________________________________________________________________________
CON
_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.
VAR
long frame[1600] 'Array that holds 1600 Longs.
long delay, leds, T, mask 'Varibles, type Long "32" Bits.
OBJ
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.
Comments
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.)
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.
HTH,
--Rich
I found a good explanation for the waitcnt command.
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
Hanno