+ Reply to Thread
Page 2 of 41 FirstFirst 12345612 ... LastLast
Results 21 to 40 of 813

Thread: Propeller Assembly for beginners

  1. #21

    Default Re: Propeller Assembly for beginners

    In this book, I will assume that you have a serious interest in learning PASM. The book will follow the format, and the programs that were delivered in my book on learning SPIN. I will assume that you have the following resources available to you.

    1. My book on spin, which you will have as a general reference.
    2. The propeller manual version 1.0 and the published errata. See Propeller Tool help tab.
    3. The data sheet for the propeller chip. This too can be downloaded from the help menu tab in the propeller tool.

    The first program that is is always considered in any tutorial on any language is the blinking of an LED. The propeller manual provides an example of this on this on page 340. Here is a listing of that program as provided by Parallax:

    {{AssemblyToggle.spin}}
    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000

    pub main
    cognew(@toggle, 0)

    dat
    org 0
    toggle mov dira, pin
    mov time, cnt
    add time, #9
    :loop waitcnt time, delay
    xor outa, pin
    jmp #:loop

    pin long |<1
    delay long 6_000_000
    time res 1

    The explanations provided in the manual are not as detailed as they need to be for a beginning student of the language. To remedy this, I will provide a line by line description, or if you will, documentation for this program as the first example of proceeding with our learning process.

    I will be setting up all the experiments in this book on the propeller professional development board. If you have the education Kit provided by parallax it will provide most of the electronic components that you need. The other components I've been list in the appendix so that you can have them all one hand as you start your experimentation. It is not strictly necessary to use the professional development board but the board does make life a lot easier in that it has an awful lot of the devices that we will be experimenting with built right into the board, and they are well organized and easy to use.

    Let's go over the first program in the propeller manual a line at the time so that we can understand exactly what the programmer had in mind as you go over the program.

    {{AssemblyToggle.spin}}
    the first line is a comment, and as such is not executed by the language.

    CON
    the second line identifies the following two lines as constants. These constants are the same as we see in SPIN, and they define the speed of the processor as 5 X 16 megahertz. We could have defined this as any other speed as long as we used valid values. Keep in mind that the slower we run the processor, the less energy it uses.

    PUB Main
    The next line defines the object as a public object and provides a handle for the program.

    cognew(@toggle, 0)
    this line launches a new cog that will holld the program for blinking the LED that we are addressing.
    It tells the propeller that the new program is to be installed starting at location zero in the memory of this cog.

    We now need to define what the total toggle program consists of. It is defined in the DAT data provided for this program.

    The next line.

    DAT.
    Tells us that the following lines contain the data that will be interpreted as a program by cognew the cog that we started earlier under MAIN.

    {toggle P16}
    Is a comment to remind us that we will be toggle line 16 on the propeller. We have a choice of any line from one to 31, but we may want to avoid the last four lines in that they have other uses that we may not want to interfere with at this time.

    ORG 0
    This line tells us that we are going to start storing our data points into the memory of this cog starting with the first memory location in the cog. We have the option of starting at any memory location within the cog but at this point, there is really no good reason not to start at location zero.

    Toggle Mov Dira, Pin
    in this line, Toggle is a marker that has no particular purpose. It marks the beginning of a oobject. The command MOV tells the processor to move the value of PIN into the DIRA register. On start up a propeller chip has all its pins in the input mode. In this mode, thy are high impedance pins that will accept data both from TTL level signals and from CMOS level signals. Remember that the propeller is a CMOS device running at 3.3 Volts. The switching point between high-level and low-level signals in a 3.3 volt device is 1.65 V or half of 3.3 volts. Further down in the program our constants can is defined as a long or 32 bit variables. In this 32 bit variable bit 16 is made a one and then the 32-bit number will be moved into the register DIRA.

    There are 16 registers that are pre-defined in PASM. A list of these 16 registers is in the data sheet on page 34. Some of these registers can be written to, and some cannot. We will not go into witch is which at this time, but you need to be aware that these registers exist and one of them (DIRA) represent the direction that the pins will be programmed to in our programs.

    Before we started DIRA was

    00000000_00000000_0000000_0000000

    After reading is executed. The instruction DIRA will contain

    00000000_00000001_0000000_0000000

    This tells us that all the pins on our propeller are inputs except in 16, which is the 17th pin because we always start counting at zero.

    In this discussion, and in all subsequent discussions, I will use binary format as used above so that it is easy to see which pin is set to what without having to do any mental manipulations.

    mov time cnt
    Thi instruction moves the current content of the system clock or counter into the Time register. CNT is also one of the 16 registers that was referred to earlier as being predefined in each cog ram. This register contains the current count in the system clock and is it a read only register. You cannot set the system clock, you can only read it.

    add time, #9
    this instruction adds, the number nine to the time variable. This is necessary, because there is a short delay between putting the counter into time variable and starting the looping process. Adding the nine compensates for the delay between the two instructions. This number can be nine or higher. Making it higher, would detract from the accuracy of the first delay and making it lower, would make it necessary for the conqueror to go all the way around the 32-bit count the before it could be used correctly.

    :loop waitcnt time, delay
    this is the wait instruction that determines the delays between turning pin 16 on and off in the program. This delay is based on the clock frequency that we specified under CON. The delay is 6/80 seconds as specified. The LED stays on for 6/80 seconds and then stays off for 6/80 seconds for a full cycle of 3/80 seconds. The way this works in PASM the delay is dependent on the specified speed of the system clock.

    xor outa, pin
    this instruction inverts the signal on pin 16 each time through the loop.


    jmp #:loop
    tells the program to jump to the location marked ":loop". This starts the process of converting the target line, and the delay over and begins routine that links the LED at location PIN

    We still have the business of defining our constants and telling the processor, where we want the information stored. This is done on the next three lines.

    Pin Long |<16
    this identifies PIN as location 16 in the register that pin will be used with.

    Delay long 6_000_000
    tells the processor that delay will be a four byte long with a value of 6 million placed in it.

    Time res 1
    tells the system that the time variable will be located within the workspace assigned in RES 1. This is one of the registers in the resources area, and we are addressing the first register in this space.

    Experimentation.
    It is best not to connect the output pin on a propeller directly to an LED. It would be best to use a resistor of between 1 and 5 hundred ohms to limit the load on the line. The larger the value you use the dimmer the LED will be.

    Run the program and try changing the various parameters to see what happens. Off particular interest is the number nine, and the number 6 million..


    Harprit
    Last edited by Harprit; 07-20-2011 at 10:36 PM.

  2. #22

    Default Re: Propeller Assembly for beginners

    A good start!

    Just a suggestion but you may want to write your book with V1.1 of the manual as the reference. A reader can get that just by clicking on help in the Propeller Tool (just like you tell them for the datasheet). To get V1.0, I need to go hunting on the Parallax website??.....oops, the only other version on the website is v1.01....or the errata to v1.0....no actual download for v1.0 of tthe manual!

    The reader also needs to be encouraged to read the hardware section of the manual and the datasheet so some of the basics of the hardware are understood. The tight coupling of PASM to the hardware requires an understanding of the chip, how it's structured and what some of the important architecture features and pieces are. Some of this you may need to help explain or elaborate on in the first chapter of your book - anything you found confusing? Anything in particular that makes PASM easier to write and understand?

    I like taking the PASM code apart line by line and explaining what it does and what things are. From this respect, you become the processor and perform each instruction step by step.
    MOV OUTA, PEACE

    ... Rick


    I've stopped using programming languages with Garbage Collection, they keep deleting my source code!!

    "Forth is FUN!"

    Introduction to TACHYON Forth


    PropFORTH Wiki

  3. #23

    Default Re: Propeller Assembly for beginners

    Quote Originally Posted by Harprit View Post
    In this book, I will as shown that you have a serious interest in learning PASM.
    I assume your editor will fix this and other simple foobars.

    Quote Originally Posted by Harprit View Post
    My book on spin, which you will have as a general reference.
    The propeller manual version 1.0.
    The data sheet for the propeller chip. This can be downloaded from the help menu item on the propeller tool.
    The propeller manual is also accessible from the Propeller Tool Help Menu.


    Quote Originally Posted by Harprit View Post
    The first program that is is always considered in any tutorial only language is the blinking of an LED.
    The first program in most languages is "Hello, World!" .... Blinky is fine for an MCU though.

    Quote Originally Posted by Harprit View Post
    To remedy this, I will provide a line by line description, or if you will documentation for this program as the first example of proceeding with our learning process.
    Edit.

    Quote Originally Posted by Harprit View Post
    CON
    the second line identifies the following two lines as constants. These constants are the same as we see in spin, and they define the speed of the processor as 5 X 16 megahertz. We could have defined this as any speed. We thought appropriate, keeping in mind that the slower we run the processor. The less energy. It uses.
    Alternatives for clock are _clkmode RCFAST and _clkmode RCSLOW but can't specify _xinfreq

    Quote Originally Posted by Harprit View Post
    PUB Main
    The next line is a public object that provides a handle for the program that we are writing. ...

    cognew(@toggle, 0)
    PUB main is a public method in the object and the starting point for the program.
    The next line starts the PASM program. The @ sign means use the address of the toggle program.

    Quote Originally Posted by Harprit View Post
    Tell us the propeller that the new called movie ...
    movie?

    Quote Originally Posted by Harprit View Post
    With that is the end of the program.
    The end of main should end with REPEAT to keep the spin interpreter from accidently executing some bytes.


    Quote Originally Posted by Harprit View Post
    {toggle P16}
    Is a comment to remind us that we will be tall going line 16 on the propeller.
    "tall going" ... spell checker?

    Quote Originally Posted by Harprit View Post
    We have a choice of any line from one to 31, but ....
    That would be from zero to 31 or output P0 to P31 ... Phil would say PA0 to PA31, but there will never be a Propeller with PB0..PB31 so it doesn't matter.

    Quote Originally Posted by Harprit View Post
    Toggle Mov Dira, Pin

    ... The command MOV tells the processor to move the value of PIM into the DIRA register.
    PIN to DIRA

    Quote Originally Posted by Harprit View Post
    On start up a propeller chip has all its pins in the input mode. In this mode, the our high impedance pins that will accept data bolt from TTL level signals and from CMOS level signals.
    Input signals > 3.3V (or less than 0 for that matter) should have a series resistor that does not allow the current of the potential difference over the resistor to exceed the specified 0.5mA . R > E/0.5mA

    Quote Originally Posted by Harprit View Post
    In this 32 bit variable bit 16 Ruby made a one and then the fellow 32-bit number will be moved into the register DIRA.
    Who's Ruby?

    Quote Originally Posted by Harprit View Post
    mov time cnt
    Missing comma.

    Quote Originally Posted by Harprit View Post
    add time, #9
    this instruction adds, the number nine to the time variable.
    normally people do it in this order
    MOV time, #9
    ADD time, CNT

    Quote Originally Posted by Harprit View Post
    xor outa, pin
    this instruction inverts the signal on pin 16 each time through the loop.
    If i didn't know boolean operators, I would be a little miffed by now. You might say this is indistinguishable from magic right now and will be explained later.

    Quote Originally Posted by Harprit View Post
    jmp #:loop
    does the program to jump to the location marked ":loop". This starts the process of converting the target line, and the delay over and behead every program that links the LED at location PIN
    I would add "please don't forget the # on this line!" Octothorpe, whatever you want to call it ....


    Quote Originally Posted by Harprit View Post
    Time res 1
    tells the system that time will be located within the workspace assigned in RES one. This is one of the registers and the resources area, and we are addressing. The first register in this space.
    I would add "under no circumstances should a res command be used in the middle of a PASM program" .... blah res N must be at the end of code because it does not allocate space for the variable and will cause any longs or instructions that might come after it to have the wrong register addresses.

    Quote Originally Posted by Harprit View Post
    It is best not to connect the output pin on a propeller directly to an LED. It would be best to use a resistor off between 1 and 5 hundred ohms to limit the load on the line.
    100 to 500 Ohms is easier to understand ... if that's what you meant.

    Hope it's helpful.

    Cheers.
    --Steve
    Last edited by jazzed; 07-21-2011 at 12:03 AM. Reason: after _clkmode, _xtal changed to _xinfreq
    “I have no special talents. I am only passionately curious.” Einstein
    ~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v ~^~v~^~v~
    Learn Parallax PropellerC: http://learn.parallax.com/propeller-c
    PropellerGCC Documents: http://sites.google.com/site/propellergcc
    PropellerGCC Wiki Page : http://code.google.com/p/propgcc/w/list
    PropellerC Arduino Code : tonokip project

  4. #24

    Default Re: Propeller Assembly for beginners

    Quote Originally Posted by Harprit View Post
    cognew(@toggle, 0)
    this line launches a new cog that will holld the program for blinking the LED that we are addressing.
    It tells the propeller that the new program is to be installed starting at location zero in the memory of this cog.
    You don't have a choice. Cog memory is always filled starting at 0 when using cognew/coginit. In case (why not) you refer to the second parameter, that's what ends up in the read-only par register.

    Quote Originally Posted by Harprit View Post
    ORG 0
    This line tells us that we are going to start storing our data points into the memory of this cog starting with the first memory location in the cog. We have the option of starting at any memory location within the cog but at this point, there is really no good reason not to start at location zero.
    Again, using e.g. org 32 doesn't magically move your program somewhere else. This directive just initialises what you might call a base counter for all the address calculations the assembler has to do. There are legitimate reasons for using org with something other than 0 but that's maybe a bit too much for beginners.

    Note that - English not being my 1st language - I interpret what you wrote the way I just described. It's possible that you meant it in a different way. That said it should be made absolutely clear - without room for mis-interpretation - what's happening here.

    The manual makes a good attempt at explaining org. Maybe it would be better to apply Steve's miffed-xor clause and leave a detailed analysis for later.

    Quote Originally Posted by Harprit View Post
    PUB Main
    The next line defines the object as a public object and provides a handle for the program.
    This suggests that you can have other types of objects e.g. private. Fact is, an object needs at least one public method and the first (public) one is used as the start method (if run/used as the main object).
    Last edited by kuroneko; 07-21-2011 at 12:52 AM. Reason: added note re: deferring complex explanations

  5. #25

    Default Re: Propeller Assembly for beginners

    Quote Originally Posted by kuroneko View Post
    Again, using e.g. org 32 doesn't magically move your program somewhere else. This directive just initialises what you might call a base counter for all the address calculations the assembler has to do. There are legitimate reasons for using org with something other than 0 but that's maybe a bit too much for beginners.
    Kuroneko is correct of course. There are advanced cases where ORG N is used to properly align a "base counter" for block of code's references in overlay loading for example (could be a beginner's topic, but unlikely). Magically moving can be done with the "long 0 [xxx]" statement.
    “I have no special talents. I am only passionately curious.” Einstein
    ~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v ~^~v~^~v~
    Learn Parallax PropellerC: http://learn.parallax.com/propeller-c
    PropellerGCC Documents: http://sites.google.com/site/propellergcc
    PropellerGCC Wiki Page : http://code.google.com/p/propgcc/w/list
    PropellerC Arduino Code : tonokip project

  6. #26

    Default Re: Propeller Assembly for beginners

    First we have:

    Code:
    cognew(@toggle, 0)
    this line launches a new cog that will holld the program for blinking the LED
    that we are addressing. It tells the propeller that the new program is to be
    installed starting at location zero in the memory of this cog.
    The way that is written one might confuse "location zero" in the cog with the
    zero value of the second parameter to cognew.

    Better to say that the first parameter to cognew is the address of the PASM
    code, in a DAT section, that will be loaded from hub memory into cog and
    executed. It will always be loaded at address zero within the cog. The "@"
    symbol is the operator used to get the address of a label rather than the
    value stored at that address.

    You should also mention that what the second parameter is and that it is not
    used here. Expand on it later of course.


    Then we have;

    Code:
    Toggle Mov Dira, Pin
    in this line, Toggle is a marker that has no particular purpose.
    Toggle is a label for a place in hub ram, it has a very important purpose here, it is used as the first parameter to cognew and tells were the cog code is to be loaded from.

  7. #27

    Default Re: Propeller Assembly for beginners

    I am getting great feedback from the forum.
    I am learning more than I thought I would, faster than I thought I would.
    Thanks

    I have
    pin long %0000_0000_0000_0000_0000_0000_0000_0001
    It then goes into DIRA and a pause

    If I move the data with SHL and SHR I can make line 0 go on and off
    However, line 1 does not go on and off
    What am I doing wrong?
    What do I need to do.

    Harprit

  8. #28

    Default Re: Propeller Assembly for beginners

    Are you shifting both DIRA and OUTA? If so it sounds like it should work. Let's see the actual code

  9. #29

    Default Re: Propeller Assembly for beginners

    This code inverts line 0 but not line 1 so the the 1 is moving left one and then back
    but when the 1 is in position 1, it does not seem to turn line 1 on and off.

    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000

    pub main
    cognew(@toggle, 0)

    dat
    org 0
    toggle mov dira, pin
    mov time, cnt
    add time, #9

    :loop waitcnt time, delay
    xor outa, pin
    shl outa, 1

    waitcnt time, delay
    xor outa, pin
    shr outa, 1
    jmp #:loop

    pin long %00000000_00000000_00000001
    delay long 6_000_000
    time res 1

    HSS

  10. #30

    Default Re: Propeller Assembly for beginners

    You really must start to use code tags around you code snippets.
    You only set bit zero of DIRA so you wil not see any output on bit 1.
    Your use of xor and shifts only ever puts 0 on bit 1 of outa so even if you dira was correct you would not see a high on the pin.

  11. #31

    Default Re: Propeller Assembly for beginners

    This has some problems even I can now see
    Let me work on it a bit.

    Harprit

  12. #32

    Default Re: Propeller Assembly for beginners

    Thanks Heater

    The following code alternates the LEDs on lines 0 and 1
    It works (with your input)
    What I want to know is, Is this the right way to do it for beginners
    Also tell me what you mean by code tags.

    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000

    pub main
    cognew(@toggle, 0)

    dat
    org 0
    toggle mov dira, pin
    mov time, cnt
    add time, #9

    :loop waitcnt time, delay
    mov outa, pin2

    waitcnt time, delay
    mov outa, pin3

    jmp #:loop

    pin long %00000000_00000000_00000011
    pin2 long %00000000_00000000_00000010
    pin3 long %00000000_00000000_00000001
    delay long 10_000_000
    time res 1

  13. #33

    Default Re: Propeller Assembly for beginners

    A general rule is not to use mov on outa as it will also turn off all other pins that this cog have as outputs in dira.

    or ' turn pin high

    1100 'current state for example
    0010 ' ora this value
    ------
    1110 ' result


    andn 'turn pin low

    1110 'current state for example
    1101 ' and this value, this would be the bit negative value of pin so that's the reason to use andN to save you from declearing a not version
    -------
    1100 ' result

    xor 'toggle pin.

    use [ c o d e ] and [ / c o d e] without space in your postings.

    You only show 24 bits in pin, to be correct you should have 4 * 8bits
    or use: 1<<pin# (or also: |<pin#)

    could also use (to get rid of the pin _00000011)
    mov dira,pin2
    or dira,pin3

    or at least call it 'myoutputs"
    and the other pin1 and pin 2
    Last edited by tonyp12; 07-21-2011 at 08:17 PM.

  14. #34

    Default Re: Propeller Assembly for beginners

    Tony:

    Looking at just the loop

    :loop waitcnt time, delay
    mov outa, %01
    waitcnt time, delay
    xor outa, %11
    jmp #:loop

    This works but I really don't know why because I have tried a bunch of other
    ways of doing it and they work too. I don't understand them either. Could you
    write this loop for me exactly the way is should be with comments.

    Harprit.

  15. #35

    Default Re: Propeller Assembly for beginners

    There are many ways to get the same end result.
    But it's always good to learn coding in the 'correct way' so the code can be easily expanded.

    I toggle two leds here, I would not say the code is in a correct way. (click see more to see the source code)
    http://www.youtube.com/watch?v=_5zRxYyfDN0

  16. #36

    Default Re: Propeller Assembly for beginners

    Using "MOV OUTA, value" is fine as long as the COG only drives a known set of outputs.

    There are advantages in some cases and hazards in others. One of the more interesting instructions is "RDBYTE OUT, ptr". Often if a signal such as write enable WE* needs to be driven in addition to P0..P7, DIRA can be set to drive WE* at the same time. A pull up can be used for making WE* high with DIRA setting it to an input which allows other operations such as read.

    Using "OR OUTA, value" and "ANDN OUTA, value" certainly have their place when complicated COG IO is being used. Very often however the instructions mentioned along with other operations on the bus can slow things down.
    “I have no special talents. I am only passionately curious.” Einstein
    ~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v~^~v ~^~v~^~v~
    Learn Parallax PropellerC: http://learn.parallax.com/propeller-c
    PropellerGCC Documents: http://sites.google.com/site/propellergcc
    PropellerGCC Wiki Page : http://code.google.com/p/propgcc/w/list
    PropellerC Arduino Code : tonokip project

  17. #37

    Default Re: Propeller Assembly for beginners

    Tony. Could not get to your code
    Jazzed. Your note was over my head.

    Harprit

  18. #38

    Default Re: Propeller Assembly for beginners

    Quote Originally Posted by Harprit View Post
    Also tell me what you mean by code tags.
    Simply put [code] in front of your code block and [/code] at the end. Example:
    Code:
    :loop   waitcnt time, delay
            xor     outa, #%11             ' toggles bits 0 and 1
            jmp     #:loop

  19. #39

    Default Re: Propeller Assembly for beginners

    Thank you kuroneko.

    That really captured the essence of it and made me see it in a flash
    And yes of course I knew that, but I could not see it for the life of me and
    you made it so clear. I hope I can transfer such clarity to the readers in
    the text.

    Harprit.

  20. #40

    Default Re: Propeller Assembly for beginners

    Here is the next batch, there is still a lot of editing to do and the figures are not in place
    The two LEDs to to lines 0 and 1

    Change the program so that it looks like the following:
    Code:
    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    pub main
    cognew(@toggle, 0)
    
    dat
           org      0                      'start of the program storage locations
    toggle mov      dira,   pin            'pin now sets lines 0 and 1 as outputs
           mov      time,   cnt            'sets the delay time to 10m cycles
           add      time,   #9             'adds 9 to the time count
    
    :loop  waitcnt  time,   delay          'the wait instruction
           mov      outa,   pin0on         'sets output 0 on and 1 off
    
           waitcnt  time,   delay          'the wait instruction 
           mov      outa,   pin1on         'sets output 1 on and 0 off
    
           jmp      #:loop                 'go back and loop.
    
    pin      long %00000000_00000000_00000000_00000011    'used to set 0 and 1 as outputs
    kine0on  long %00000000_00000000_00000000_00000001    'used to turn on line 0
    line1on  long %00000000_00000000_00000000_00000010    'used to turn on line 1
    delay    long 10_000_000                              'delay cycles defined
    time  res  1                                          'storage location for time
    Program XXX
    Blinking lines alternately.

    The above program XXX will blink the LEDs on lines 0 and 1 on and off alternately. This code is easy to read but it is archaic and it is not the best way to do it. An only slightly better way to write this program is shown next in Program XXX. Again the changes are to the looped part of the program
    Code:
    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    pub main
    cognew(@toggle, 0)
    
    dat
           org      0                      'start of the program storage locations
    toggle mov      dira,   pin            'pin now sets lines 0 and 1 as outputs
           mov      time,   cnt            'sets the delay time to 10m cycles
           add      time,   #9             'adds 9 to the time count
    
    :loop  waitcnt  time,   delay          'the wait instruction
           or      outa,   %01             'sets output 0 on and 1 off
    
           waitcnt  time,   delay          'the wait instruction 
           or      outa,   %10             'sets output 1 on and 0 off
           jmp      #:loop                 'go back and loop.
    
    delay    long 10_000_000                              'delay cycles defined
    time  res  1                                          'storage location for time
    Program XXX a slightly better way to write program XXX
    Blinking lines 0 and 1 alternately.

    When you run the above program you will see that two LEDS can be turned on and off alternately with the above register assignments. The program shows you how to set the lines as Inputs and Outputs with simple statements and how to affect the I/O operation of the pins. However this is both the tedious and archaic way of doing it. A number of better ways to do it follow. In these examples we are using shorthand notation and binary math techniques to manipulate the registers. Handling registers in this way is important in almost all the things that we will be doing. The techniques we use next use the following principles:

    There are three basic things we can do to one bit in one operation.
    Change it from a low to high-level (0-->1)
    Change it from a high to low-level (1-->0)
    Invert its state, in other words toggle the bit. Make it high if low and low if high.

    Assume that the current 4 bits under consideration are %1010
    We can change any bit in this group to high with the ora operation on that bit.

    1010 current content
    ora 0001 addresses the last bit
    1011 result: the last bit is turned on (changed from 0 to 1)

    If we want to turn a bit off we can do it with the andn operation

    1010 current content
    andn 0010 addresses the third bit
    1000 result: the third bit is turned off (changed from 1 to 0)

    If we want to change (or toggle) the state of a bit we use the xor command

    1010 current content
    xor 0011 addresses the third and fourth bits.
    1001 result: the third and fourth bit are toggled (changed from 1 to 0 and 0 to 1)

    Now let us look at the looping part of the code in our program and see how we can use the above commands to make the LEDS flash alternatively.

    The simplest way is to first set the two bits that control the LEDs to 01 or 10 with the outa command and then do a toggling procedure with the xor command.
    Code:
    dat
           org      0                   'start of the program storage locations
    toggle mov      dira,   #%11        'pin now sets lines 0 and 1 as outputs
           mov      time,   cnt         'sets the delay time to 10m cycles
           add      time,   #20         'adds 12 to the time count
           mov      outa,   #%01        'sets the two bits 
    :loop  waitcnt  time,   delay
           xor      outa,   #%11        'toggles bits 0 and 1
           jmp      #:loop
    Now that we understand the simplest of bit manipulations and the creation of the simplest of loops. Let us expand on these idea to learn how to undertake some other often used techniques.


    Shifting the bits in a register left and right.

    The two instructions used to shift bits left and right are SHL and SHR. Bits can be shifted from 1 to 32 places within the 32 bit registers.

    If we use a bit shifting technique to move the ON bit back and forth to alternate the coming on of the two bits we have been considering above, the data part of program would look like the listing shown in Program XXX. Here we have to wait after each shift to duplicate the effect in program XXX above.
    Code:
    dat
           org      0                   'start of the program storage locations
    toggle mov      dira,   #%11        'pin now sets lines 0 and 1 as outputs
           mov      time,   cnt         'sets the delay time to 10m cycles
           add      time,   #20         'adds 12 to the time count
           mov      outa,   #%01        'sets the two bits 
    :loop  waitcnt  time,   delay       'delay
           shl      outa,   #%1         'shift left one bit
           waitcnt  time,   delay       'delay
           shr      outa,   #%1         'shift back right one bit
           jmp      #:loop
    Program XXX
    Controlling off the LEDs by shifting the active bit left and right.




    Creating Subroutines/Methods

    PASM has the waitcnt command for creating pauses but we are going to ignore that in the immediate discussion.

    First let us see how we call a subroutine in PASM. We will make the wait a part of a subroutine and then call the subroutine whenever we need to wait. This needs to be done after each shift command. The complete code for this is
    Code:
    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    pub main
    cognew(@toggle, 0)
    
    dat
           org      0                   'start of the program storage locations
    toggle mov      dira,   #%11        'pin now sets lines 0 and 1 as outputs
           mov      time,   cnt         'sets the delay time to 10m cycles
           add      time,   #200        'adds 12 to the time count
           mov      outa,   #%01        'sets the two bits 
    :loop
           call     #clkdelay           'call the delay subroutine
           shl      outa,   #%1         'shift left one bit
           call     #clkdelay           'call the delay subroutine
           shr      outa,   #%1         'shift back right one bit
           jmp      #:loop
           
    clkdelay  waitcnt time, delay       'the delay subroutine
    clkdelay_ret    ret                 'return for delay subroutine
    
    delay      long 10_000_000          'delay cycles defined
    time       res  1                   'location for time
    Program XXX
    Places the waitcnt command in a subroutine.

    Often we need to be able to do something a fixed number of times and then do something else. In order to do this we need to learn how to set up counters. Let us use a counter that uses waits of one quarter (0.25 seconds) repeatedly to make up the delays we need in our programs from time to time. The wait period will need to run through 80_000_000_/4 cycles of an empty loop before exiting. We will then place this method in our blink routine to make sure it works.

    Again: our clock is running at 80 MHz, so one second takes 80_000_000 cycles and 0.25 seconds take 20_000_000 cycles. We also know that the average instruction takes 4 clock cycles. So our 0.25 second subroutine has to have 5_000_000 iterations through its loop.(Our counts are not exact because we are not counting every instruction but it will be close enough for what we are trying to learn at this time. We will learn to count exact cycles later on in the book)
    Code:
    con
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    pub main
    cognew(@toggle3, 0)
    
    dat
              org      0                   'start of the program storage locations
    toggle3   mov      dira,   #%11        'pin now sets lines 0 and 1 as outputs
              mov      outa,   #%01        'sets the two bits   
    :loop
              call     #clkdelay           'call the delay subroutine
              shl      outa,   #%1         'shift left one bit
              call     #clkdelay           'call the delay subroutine
              shr      outa,   #%1         'shift back right one bit 
              jmp      #:loop
           
    clkdelay  mov      time, deltime       'the delay subroutine, load deltime into toime
    take4                                  'imternal to subroutine flag
              sub      time, #1   wz       'sub 1 from time and set flag if 0
      if_nz  jmp       #take4              'if flag not 0 go back to take4
    clkdelay_ret     ret                   'return for delay subroutine
    
    deltime    long    5_000_000           'time of delay
    time       res     1                   'location for time
    Program XXX
    Subroutine creation and use for 0.25 second delay.


    Harprit

+ Reply to Thread

Similar Threads

  1. Book for absolute beginners. Propeller 101
    By Harprit in forum Propeller 1 Multicore Microcontroller
    Replies: 121
    Last Post: 06-27-2010, 10:37 PM
  2. Propeller Assembly Q&A
    By Paul Baker in forum Propeller 1 Multicore Microcontroller
    Replies: 68
    Last Post: 10-07-2009, 12:52 AM
  3. Propeller PLL for beginners
    By Loopy Byteloose in forum Propeller 1 Multicore Microcontroller
    Replies: 0
    Last Post: 02-14-2008, 08:22 PM
  4. How many beginners would like to see a Beginners Sub Section?
    By TonyA in forum Propeller 1 Multicore Microcontroller
    Replies: 54
    Last Post: 08-10-2007, 09:52 PM
  5. How many beginners would like to see a Beginners Sub Section Forum?
    By TonyA in forum Propeller 1 Multicore Microcontroller
    Replies: 1
    Last Post: 08-02-2007, 09:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts