Shop OBEX P1 Docs P2 Docs Learn Events
VCCv7 code samples & how to — Parallax Forums

VCCv7 code samples & how to

JavalinJavalin Posts: 892
edited 2009-08-27 23:06 in Propeller 1
All,

I'm learning C on the propeller!· So I thought i'd share my wisdom as I adapt from SPIN & ASM!· If I like it (more than SPIN) I plan to purchase and use for a data-logging project re-write!

Part (0) - Structure!

1)· Names!·· C is case sensitive so "James", "JAMES" and "james" are all different and the compiler will throw an error about an unrecognised variable.· Best to be consistant then.

2)· All C programs start with a "main" function - much like the "start" in SPIN

3)· "#include" - include a header or definition file·- in this case "propeller.h"

4)· C structures (if, while, etc) all use { } to mark blocks and don't (though its very good practice) require indenting.· There are exceptions to the rule as always

5)· statements end with ";"

6)· Comments!· see Below

#include <propeller.h>

short myVariable1=0; [color=#008000][color=#008000]// a "Short" variable declaration and set to 0[/color][/color]

void main()
{
[color=#008000][color=#008000]     // this is a single line comment[/color][/color]

[color=#008000][color=#008000]    /* this is 
       a
       multi line (block) comment
     */[/color][/color]

     myVariable1=1;               [color=#008000][color=#008000]// set to 1[/color][/color]
     myVariable1=myVariable1+1;   [color=#008000][color=#008000]// add by 1[/color][/color]
     myVariable1++;               [color=#008000][color=#008000]// add by 1[/color][/color]
     myVariable1=myVariable1-1;   [color=#008000][color=#008000]// subtract by 1 (good spot Beau)[/color][/color]
     myVariable1--;               [color=#008000][color=#008000]// subtract by 1[/color][/color]
}


Thats it!

James

Post Edited (Javalin) : 8/26/2009 9:10:26 PM GMT
«1

Comments

  • JavalinJavalin Posts: 892
    edited 2009-08-26 20:12
    All,

    Part 1 - pins!

    First step - set the direction of the pin:
    InputPinMask = 1 << 10;    [color=#008000]// input pin mask[/color]     
    OutputPinMask = 1 << 9;    [color=green]// output pin mask[/color]
    
    DIRA |= OutputPinMask;     [color=#008000][color=#008000]// set pin to an Output[/color][/color]
    DIRA &= ~InputPinMask;     [color=#008000][color=#008000]// set pin to an Input[/color][/color]
    
    

    Second step - switch it on and off slowly:
    while (TRUE) [color=#008000][color=#008000]// loop forever - like "repeat" in SPIN[/color][/color]
    {
        OUTA |= OutputPinMask;       [color=#008000][color=#008000]// pin high [/color][/color]
        mSleep(1000/2);              [color=#008000][color=#008000]// wait 1/2 second[/color][/color]
        OUTA &= ~OutputPinMask;      [color=#008000][color=#008000]// pin Low [/color][/color]
        mSleep(1000/2);              [color=#008000][color=#008000]// wait 1/2 second[/color][/color]
    }
    
    

    Second and a half step - toggle it:
    OUTA ^= OutputPinMask;       [color=#008000][color=#008000]// toggle pin[/color][/color]
    
    

    Third step - test a pin and act on the finding:
    while (TRUE)       [color=#008000][color=#008000]// loop forever - like "repeat" in SPIN[/color][/color]
    {
        if (INA & inputpinMask)        [color=#008000][color=#008000]// test INA[/color][/color]
        { 
            OUTA |= OutputPinMask;     [color=#008000][color=#008000]// if pin 10 high - set pin 9 HIGH[/color][/color]
        }
        else
        {
            OUTA &= ~OutputPinMask;    [color=#008000][color=#008000]// otherwise - set pin 9 LOW[/color][/color]
        }
    }
    
    

    Fourth step·- putting it all together, PWM'ing all pins - example assumes pins 0-5 are connected to an LED:
    #include <propeller.h> 
     
    long  stack1[noparse][[/noparse]20];      [color=#008000][color=#008000]// a small stack[/color][/color]
    
    short ledsetting[noparse][[/noparse]32]; [color=#008000][color=#008000]// settings for the 32 pins[/color][/color]
    short pwm=0;          [color=#008000][color=#008000]// Counter - 0->255[/color][/color]
    short ledidx=0;       [color=#008000][color=#008000]// LED counter[/color][/color]
    
    
    void main()
    {
    [color=#008000][color=#008000]    // pwm setting range 0 (off) to 255 (full on)[/color][/color]
        ledsetting[noparse][[/noparse]0] = 0; [color=#008000][color=#008000]// off[/color][/color]
        ledsetting[noparse][[/noparse]1] = 25;
        ledsetting[noparse][[/noparse]2] = 50;
        ledsetting[noparse][[/noparse]3] = 75; [color=#008000][color=#008000]// fully bright[/color][/color]
        ledsetting[noparse][[/noparse]4] = 100; 
        ledsetting[noparse][[/noparse]5] = 255; [color=#008000][color=#008000]// full o[/color][/color]
    
    
    [color=#008000][color=#008000]    // set the first [color=green]LedIDXcnt[/color] pins to OUTPUT[/color][/color]
        DIRA ^= ((1 << LedIDXcnt) -1);
    
        while (TRUE) [color=#008000][color=#008000]// will we ever get out of here?[/color][/color]
        { 
    [color=#008000][color=#008000]        // if the "ledsetting" for pin "ledidx" is still above "pwm" then[/color][/color]
    [color=#008000][color=#008000]        // set the pin to HIGH [/color][/color]
            if (ledsetting[noparse][[/noparse]ledidx] > pwm)
            {
    [color=#008000][color=#008000]            // Pin High - make the mask dynamically[/color][/color]
                OUTA |= (1 << ledidx);
            } 
            else
            {
    [color=#008000][color=#008000]            // Pin Low - make the mask dynamically[/color][/color]
                OUTA &= ~ (1 << ledidx); 
            }
    
    [color=#008000][color=#008000]        // increment "pwm" and limit "pwm" counter to 0-255 [/color][/color]
            pwm++;
            pwm &= 255; 
    
    [color=#008000][color=#008000]        // limit to 0-32 (use 32-1 for the mask)[/color][/color]
            ledidx++; 
            ledidx &= 31;
         } 
    }
    
    

    Finally - this is a bit cleverer/nicer way of doing it (updated!):
    #include <propeller.h>
    
    short LedPWM[noparse][[/noparse]] = {10,20,30,40,50,60}; [color=#008000][color=#008000]// pwm settings for the I/O pins[/color][/color]
    short pwm=0;                          [color=#008000][color=#008000]// Counter - 0->255[/color][/color]
    short LedIDX=0;                       [color=#008000][color=#008000]// LED counter[/color][/color]
    [color=black]short [color=#000033]LedIDXcnt=6;                    [/color][color=green]// count of pins (led's!) to PWM.[/color][/color]
    
    void main(void)
    {
    [color=#008000][color=#008000]     // set the first X pins - set the DIRA register to output [/color][/color]
         DIRA ^= ((1 << LedIDXcnt) -1);
    
    [color=#008000][color=#008000]     // DIRA ^= 0b111111111; // or you can specify the binary [/color][/color]
    [color=#008000][color=#008000]                            // directly using "0b"
    
    [/color][/color][color=#008000][color=#008000]     // reset variables[/color][/color]
         pwm = 0;
         LedIDX = 0;
    
         while(TRUE) [color=#008000][color=#008000]// repeat always[/color][/color]
         { 
    [color=#008000][color=#008000]         // if the "ledsetting" for pin "ledidx" is still above "pwm" then[/color][/color]
    [color=#008000][color=#008000]         // set the pin to HIGH [/color][/color]
             if (LedPWM[noparse][[/noparse]LedIDX] > pwm)
             {
    [color=#008000][color=#008000]              // Pin HIGH - make the mask dynamically[/color][/color]
                  OUTA |= (1 << LedIDX);
             } 
             else
             {
    [color=#008000][color=#008000]             // Pin Low - make the mask dynamically[/color][/color]
                 OUTA &= ~ (1 << LedIDX); 
             }
    
    [color=#008000][color=#008000]         // limit to 0-255 [/color][/color]
             pwm++;
             pwm &= 255; 
    
    [color=#008000][color=#008000]         // limit to the size (no of items) of the "LedPWM" array[/color][/color]
             LedIDX++;
    [b]        if[/b] (LedIDX >= LedIDXcnt)
             {
                  LedIDX = 0;
             }
    
        } 
    }
    
    
    

    The example above will PWM as many (upto 32 of course) pins as you set "LedIDXcnt" to.· You can change the PWM values in arrays like:

    LedPWM[noparse][[/noparse]LedIDX] = 55;
    

    (Note :- the above examples have been updated following the comments/improvements in posts below!!)

    Trying to keep these examples fairly basic, I am sure it could be done "better" at the expense of read-ability!

    I think thats it for pins!

    James

    PS - is there a power-of operator in VCCv7?

    Post Edited (Javalin) : 8/27/2009 6:51:05 PM GMT
  • JavalinJavalin Posts: 892
    edited 2009-08-26 20:17
    Evening All,

    So we can now (correctly - thanks all) do pins and we know how to layout a C program!· Phew.· How about if we want to control program flow?· If you know SPIN, VB, etc this is easy - but with several gotcha's!!

    Part 2 - Controlling program flow:

    Firstly - conditional statements - IF!
    [color=green]// if - this returns FALSE (i doesn't equal 11)[/color]
    i=5;
    if (i == 11)          // test value of i with "==" 
    {
    [color=green]    // if-TRUE[/color]
    }
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    

    [color=#008000][color=#008000]// if (mistake)[/color][/color]
    [color=#008000]// this returns TRUE always![/color]
    i=5;
    if (i = 11)         [color=green]// "=" is an assigment operator - so i is set to 11 here.[/color]
    {
    [color=#008000][color=#008000]    // if-TRUE[/color][/color]
    } 
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    

    [color=#008000][color=#008000]// if (AND) - returns TRUE[/color][/color]
    i=5;
    j=7;
    if (i == 5 && j==7)         
    {
    [color=#008000][color=#008000]    // if-TRUE[/color][/color]
    } 
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    

    [color=#008000][color=#008000]// if (NOT) - returns TRUE[/color][/color]
    i=4;
    if (i != 5)            [color=green]// if i not equal to 5[/color] 
    {
    [color=#008000][color=#008000]    // if-TRUE[/color][/color]
    } 
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    

    [color=#008000][color=#008000]// if (OR) - returns TRUE[/color][/color]
    i=4;
    j=7;
    if (i==4 || j==7)     [color=green]// if i=4 or j=7[/color]
    {
    [color=#008000][color=#008000]    // if-TRUE[/color][/color]
    } 
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    

    [color=#008000][color=#008000]// if (greater than) - returns FALSE[/color][/color]
    i=4;
    j=9;
    if (i > j)             [color=green]// if i is bigger than j[/color]
    {
    [color=#008000][color=#008000]    // if-TRUE[/color][/color]
    } 
    else
    {
    [color=green]    // if-FALSE[/color]
    }
    
    


    Secondly - FOR Loops:

    Basic structure of a for loop is:
    for ([color=orange]start-condition[/color]; [color=red]continue-while-true-condition[/color]; [color=purple]loop-control[/color])
    {
      // typically: [color=orange]start-condition[/color] will be i=0
      //            the [color=#ff0000]continue-while-true-condition[/color] is an odd one - see the posts below - but the
      //                loop will continue whilst its TRUE - so i < 5
      //            [color=#800080]loop-control[/color][color=black] is what controls the loop - so i++ means the loop will go around[/color]
    [color=#000000]                    incrementing i until its above 5[/color]
    }
    

    so an example or two;
    [color=#008000][color=#008000]// for - result is value1=7.  Expecting value1=6?[/color][/color]
    [color=#008000][color=#008000]// Well [/color][/color][color=#008000]the loop goes around again when i=6, so its actually left at 7![/color]
    [color=black]value1 = 0;[/color]
    for(i=0; i<=6; i++)
    {
    [color=green]    // code here[/color]
        [color=black]value1++;[/color]
    }
    
    

    [color=#008000][color=#008000]// for - result is value1=6.  Ah expecting 12?[/color][/color]
    [color=#008000]// see that i is incremented by 2 every time![/color]
    [color=black]value1=0;[/color]
    for(i=0; i<12; i+=2)
    {
        value1++;
    }
    
    

    [color=#008000][color=#008000]// for mistake - result value1=80 (i=8)[/color][/color]
    [color=#008000]// the loop actually goes arround 4 times because i is incremented twice per loop[/color]
    value1=100; 
    for(i=0; i<=7; i++)
    {
        i++;              [color=#008000][color=#008000]// i is incremented again here![/color][/color]
        value1-=5;        [color=#008000][color=#008000]// lets subtract for a change[/color][/color]
    }
    
    

    [color=#008000][color=#008000]// for mistake 2 - result value1=0[/color][/color]
    [color=#008000]// what?  The [color=#ff0000]continue-while-true-condition[/color][/color][color=green] is not true when the loop starts![/color]
    [color=#000033]// [color=green]i doesn't equal 6![/color]
    value1=0;
    for(i=0; i==6; i++)
    {
        value1++;
    }[/color]
    
    

    Thirdly - the WHILE loop!

    The format of the loop is easy peesy!

    while ([color=red]condition-is-true[/color])
    {
    [color=green]   // run this code![/color]
    }
    

    and some examples!
    [color=#008000][color=#008000]// while loop - result i=11[/color][/color]
    i=0;       [color=#008000][color=#008000]// note that we have to initialize the control variable[/color][/color]
    [color=#008000][color=#008000]           // in a while loop - but not for a for loop[/color][/color]
    while (i<=10)
    {
        i++; 
    }
    [color=green]// again i=11 as we do the loop again when it equals 10![/color]
    
    

    [color=#008000][color=#008000]// while loop mistake 1 - result i=11.  The loop code never gets executed as the[/color][/color]
    [color=#008000]// [/color][color=#ff0000]condition-is-true [/color][color=green]is not true when we start the loop[/color]
    i=11; 
    while (i<=10)
    {
        i++;
    }
    
    

    [color=#008000][color=#008000]// while mistake 2 - never exists as i doesn't change!![/color][/color]
    i=1;
    while (i<=10);    [color=#008000]// the ; at the end of the while loop - ends the code-loop - so the[/color]
    [color=#008000]                  // { } block never runs![/color]
    {
        i+=5; 
    }
    
    

    Thats all campers.· I am not going into Continue and Goto elements for those that know these things.· I am also not going into complicated loops, nesting etc here.

    James

    Post Edited (Javalin) : 9/3/2009 8:11:50 PM GMT
  • JavalinJavalin Posts: 892
    edited 2009-08-26 20:18
    space1
  • JavalinJavalin Posts: 892
    edited 2009-08-26 20:18
    space2
  • jazzedjazzed Posts: 11,803
    edited 2009-08-26 20:20
    Nice Work!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-26 20:38
    Before you get acustomed to the "wrong" C-sytle, read this: <http://en.wikipedia.org/wiki/Indent_style&gt;
    Of course, there's only one 1TBS. smile.gif

    Albeit it is for C++, the book "C++ Programming Guidlines" by Dan Saks is quite good (as far as I remember).

    There are also styles for naming variables (I developed my own), but ICCV7Prop always ends in ints, as they are the most efficent ones.

    Oh, and forward declarations of functions are ... um ... rare. I avoid them.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • JavalinJavalin Posts: 892
    edited 2009-08-26 20:42
    Hi Nick,

    Always prefered what I now know is the "Allman" style - but as long as the programmer has one!!!

    >Oh, and forward declarations of functions are ... um ... rare. I avoid them.
    fair comment - but you get to keep the "main" at the top of the file!

    James
  • PavelPavel Posts: 43
    edited 2009-08-26 20:48
    Regarding setting pins high and low, the propeller.h header defines three macros that make this a bit more readable (SET, CLR and FLIP). Each macro takes the register to modify as its first argument and the pin numbers as its second argument. The macros expand to the same code as you shown, but are more concise. They also have the disadvantage that they operate on a single pin only (you have to make several calls to changes several pins). A few examples:

    
        SET (DIRA, 1); /* sets pin 1 to output */
        CLR (OUTA, 7); /* sets pin 7 to low
        FLIP (OUTA, 13); /* toggles pin 13 */
    
    
    



    propeller.h also defines constant TRUE that you can use instead of 1 (makes the code a bit more readable):

    while (TRUE)
    {
        /* we will never get outta here */
    }
    
    
  • NetHogNetHog Posts: 104
    edited 2009-08-26 21:17
    Nick Mueller said...
    Before you get acustomed to the "wrong" C-sytle, read this: <http://en.wikipedia.org/wiki/Indent_style&gt;
    Of course, there's only one 1TBS. smile.gif

    Albeit it is for C++, the book "C++ Programming Guidlines" by Dan Saks is quite good (as far as I remember).

    There are also styles for naming variables (I developed my own), but ICCV7Prop always ends in ints, as they are the most efficent ones.

    Oh, and forward declarations of functions are ... um ... rare. I avoid them.


    Nick

    His indentation is fine.
    The "Allman style" (as described in the wiki) is as dominant for C/C++ as the K&R or 1TBS.

    Strangely after going on about style, you then suggest to avoid forward declarations of functions (a bad style practice).

    Function/Variable naming:

    I tend to prefer Java/C# "camelCaseStyle" or "PascalCaseStyle".·


    Post Edited (NetHog) : 8/26/2009 9:22:43 PM GMT
  • JamesxJamesx Posts: 132
    edited 2009-08-26 22:17
    James

    This is so cool.
    I've gotten moderately comfortably with propellers, spin and asm. I'm on the verge of learning C, and your simple introduction to C on the propeller is a strong nudge in that direction.

    You got any more simple teaching examples?

    Jim C.
  • jazzedjazzed Posts: 11,803
    edited 2009-08-26 22:17
    Talking C style is like sacking cow manure. It's best to avoid it unless someone pays you a lot to do it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-08-26 23:00
    Steve (jazzed) said...
    Talking C style is like sacking cow manure. It's best to avoid it
    LOL --- good thing you nipped that very smelly subject in the bud before it blossomed into a really big cow pie· skull.gif

    > unless someone pays you a lot to do it

    just in case... I charge $250 per bag to sack raw syntax. lol.gif

    @Javalin: nice explainer --- this is good stuff for folks just starting out. When I learned C (too long ago :-|) there were few 'baby step' examples. So all of the 'header' stuff and code block layout was a bit daunting. Keep at it !

    - H

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


    Post Edited (CounterRotatingProps) : 8/26/2009 11:07:52 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-08-26 23:09
    Nipped? Nah, it's probably just composting [noparse]:)[/noparse] ... and it always returns in some other form [noparse]:)[/noparse]

    Regardless, it's nice to see people get excited about C [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • JavalinJavalin Posts: 892
    edited 2009-08-27 07:25
    >You got any more simple teaching examples?
    yup - more comming. Watch this space!

    >Regardless, it's nice to see people get excited about C [noparse]:)[/noparse]
    indeed - lets leave the style questions for another thread on another day. As long as there is some!

    Cheers all - glad its of help

    James
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 07:26
    > The "Allman style" (as described in the wiki) is as dominant ...

    That's the style for people who get paid by LOC. smile.gif

    > Talking C style is like sacking cow manure. It's best to avoid it unless someone pays you a lot to do it.

    Or you are working in a team and have to read / modify files of others.
    It won't take long until you ...
    * Get kicked because your addition looks completely different then the rest of the file
    * Get kicked because you made a put which resulted in 800 lines changed (of 900 lines code) with the comment "reformatted".

    > fair comment - but you get to keep the "main" at the top of the file!

    What!? Writing top down, you get the maximum of forwards.

    Talking 'bout style. This is hard to read:
    for(LedIDX=0;LedIDX<sizeof(LedPWM);LedIDX++)
    


    This is easy to read:
    for (LedIDX = 0; LedIDX < sizeof(LedPWM); LedIDX++)
    


    Funny to squeeze out every blank possible and then waste lines by putting the lCurly at the next line.

    and a
    // set the first 6 pins to OUTPUT
        DIRA ^= 64 -1;
    
    


    actually meant to mean something different:
      DIRA ^= 0b111111;
    
    


    You were thinking in bit-patterns, not decimals!

    And about idioms in programming languages:
    while (TRUE)
    is mostly written as
    for (;[noparse];)[/noparse]


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 8/27/2009 7:38:08 AM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 08:11
    was that:
        pwm=2;
        for(LedIDX=0;LedIDX<sizeof(LedPWM);LedIDX++)
        {
            // *2, *2, *2 ... etc.
            pwm *= 2;
         }
         DIRA ^= pwm -1;
    
    



    meant to mean this:
      pwm = 2;
      pwm << sizeof(LedPWM);
      DIRA ^= pwm - 1;
    
    



    Don't use globals (like for pwm) if you don't need them. And don't use pwm as a counter, it only confuses things. Especially if you initialize pwm with 0 firsthand (it's zero anyhow in your example) and then set it to 2.

    void main(void) {
      short pwm;
      short i;
      ...
    }
    
    



    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 8/27/2009 8:19:47 AM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 08:15
    This:
             // limit to the size (no of items) of the "LedPWM" array
             if (LedIDX <= sizeof(LedPWM))
             {
                 LedIDX++; // increment
             }
             else
             {
                 LedIDX = 0; // back to zero
             }
    
    


    will fail.

    Index is already to big in the comparison and gets too big by two in the LedIDX++!
      LedIDX++;
      if (LedIDX >= sizeof(LedPWM))
        LedIDX = 0;
    
    



    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 8/27/2009 8:22:58 AM GMT
  • JavalinJavalin Posts: 892
    edited 2009-08-27 08:36
    Nick - grateful for the input - I'll adjust the examples later.

    Oh - the forum software has eaten some of the formatting - i.e. the for {} you mentioned - i'll correct that too - it should be spaced as you suggest:
    for (i=0; i<6; i++)
    {
        // code
    }
    
    

    Oh and, whats the equality operator in C?· I tried "=" and "==" in the for loop as;
    for (i=0; i=5; i++)
    {
        // code
    }
    

    for (i=0; i==5; i++)
    

    and it didn't appear to work.

    >was that meant to mean this:
      pwm = 2;
      pwm << sizeof(LedPWM);
      DIRA ^= pwm - 1;
    
    

    Yes - thats much nicer.

    James

    Post Edited (Javalin) : 8/27/2009 2:39:45 PM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 08:52
    > for (i=0; i==5; i++)
    > and it didn't appear to work.

    No, this will always fail.
    The for loop is maybe a bit funny in C. Left side initializes, middle checks for am-I-still-within-range and right side modifies the index.
    When you write ...; i == 5; ... comparison fails and then the for loop does nothing.

    You can do multiple things in the 3 fields in a for-statement. Like:
    for (i = 0, p = 10; i <= whatEver; i++, p--)
    Or you can ommit one to all of them like:
    for ( ; i < 10; )

    And yes, equality is '=='


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 8/27/2009 9:03:13 AM GMT
  • Agent420Agent420 Posts: 439
    edited 2009-08-27 12:10
    For quite a while I was put off by the apparent complexity of C.· But really it is not that difficult to learn and I have come to appreciate it's balance between high-level ideology and low-level ability; it's a great one step up from assembly where it is easy to understand while not losing much of the pure assembly speed.

    My take is that Spin is very much C like, so I would think that anybody who grasps Spin should be able to easily migrate to C.· Actually, if Spin were compiled like C is implemented it wouldn't be so bad (much faster), but it may be that the memory limitations of the Prop were the primary factor in making it interpreted.· I think that to some degree those same limitations work against any high level language implemented on the Propeller.·



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • JavalinJavalin Posts: 892
    edited 2009-08-27 12:55
    >anybody who grasps Spin should be able to easily migrate to C
    thats the basis of this post - it assumes the reader is familar with programming generally, SPIN and of course the Propeller.

    James
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 14:11
    > My take is that Spin is very much C like, ...

    Be warned! I'll shoot the next one who says that! smile.gif

    I don't say that because I'm a C-fan (I'm not) or a SPIN-fan (not at all). I say this, because SPIN lacks a lot of key-elements of C.
    Maybe because it's a higher-level language that makes you think they are alike. But then, you could have chosen any language.

    And C isn't complex per se, it can be complex. But that only depends on the coder.

    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 8/27/2009 3:08:07 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-08-27 14:26
    Nick Mueller said...
    This:
             // limit to the size (no of items) of the "LedPWM" array
             if (LedIDX <= sizeof(LedPWM))
             {
                 LedIDX++; // increment
             }
             else
             {
                 LedIDX = 0; // back to zero
             }
    
    


    will fail.

    True, but the reason it will fail is that sizeof() is misunderstood and not very well explained. The function gives the number of bytes used by the variable or type being tested, not the number of items the array. You should define the size of the array in a variable where the array is defined as: "LedPWMlen = sizeof(LedPWM)/2;" because LedPWM is a short array. If you want the index to remain in the valid range, you should use "if(LedIDX < LedPWMlen) LedIDX++;" using "<=" will allow the index to go 1 too far. C arrays use "0 offset" indices, not "1 offset".

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Agent420Agent420 Posts: 439
    edited 2009-08-27 14:28
    My take is that Spin is very much C like devil.gif

    Obviously there are differences, but things like assignment operators, bitwise operations, address pointers, pre/post operations such as inc/dec set/clr· make Spin more similar to C than other hl languages such as Basic, which is probably the biggest alternative hl language for the Propeller audience.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 14:33
    > True, but the reason it will fail is that sizeof() is ...

    I missed that bug. So it will fail for THREE reasons.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • jazzedjazzed Posts: 11,803
    edited 2009-08-27 14:42
    I would be proud to have you review my code if we got paid for it. You did a great job seeing those errors.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Agent420Agent420 Posts: 439
    edited 2009-08-27 14:59
    Given the option, I think I would prefer to program the Prop with C, not only because it is faster than Spin, but also because it is a common language that my other platforms like the avr also uses and no doubt future various controllers will tend to have C as a language option, so it pays to be versed in it and you might have some common code base that can be reused.

    For the time being though, I think that the lack Spin / C compatibility plus the smaller number of C objects available might be something to consider.


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


    Post Edited (Agent420) : 8/27/2009 3:09:46 PM GMT
  • jazzedjazzed Posts: 11,803
    edited 2009-08-27 15:11
    Agent420, Parallax has offered in the forum to give hardware they sell to people that want to develop a C application / library for it. See http://forums.parallax.com/showthread.php?p=791242

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-08-27 15:14
    > My take is that Spin is very much C like

    You're dead by now! wink.gif

    structures?
    unions?
    Includes? (together with #defines #ifdefs etc)
    Type-defs?
    Type-checking?
    Type-aware pointers?
    function-pointers?
    casting? (not that this is elegant, but it helps at low-level)
    sizeof?
    Macros?
    Alloc / free? (OK, not a language-element, but a must for even the most simple set of libraries).


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Agent420Agent420 Posts: 439
    edited 2009-08-27 15:31
    jazzed said...
    Parallax has offered in the forum to give hardware they sell to people that want to develop a C application / library for it. See http://forums.parallax.com/showthread.php?p=791242
    This might be a good time to experiment with coding the 4 color 64 x 48 VGA tile object I've been playing with in C cool.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.