Shop OBEX P1 Docs P2 Docs Learn Events
Why doesn't this work? — Parallax Forums

Why doesn't this work?

basicstampedebasicstampede Posts: 214
edited 2004-09-15 19:19 in General Discussion
import stamp.core.*;
···· public class ex3 {
······ public static void main() {
·········· CPU.writePort (CPU.PORTA, (byte)0xF0);
····· }
··· }

I expect the above program to turn on 4 LEDs, but nothing turns on.

When I change it to CPU.writePin (CPU.pins[noparse][[/noparse]4], true); it works for all pins 0-7.

What am I doing wrong?· Do I have to set PORTA as output first?· How do I do that?· I don't see it anywhere in the manual.

Thanks.

Comments

  • basicstampedebasicstampede Posts: 214
    edited 2004-09-15 16:51
    OK.· I figured it out.· You have to actually set each pin as output by using a

    CPU.writePin (CPU.pins[noparse][[/noparse]i], false); statement!!!

    Is there an easier way to set PORTA to output (instead of individually writing a "false"·to each pin?)

    I've modified the program to mimic StampWorks Exercise 3 in Java and this now works.

    Note:· If i is declared as byte, then it loops forever.



    import stamp.core.*;

    ···· public class ex3 {

    ······ public static void main() {
    ········ int i;
    ········ int MinCount = 0;
    ········ int MaxCount = 255;
    ········ int DelayTime = 1000;
    ········ //byte j;

    ·········· for (i=0; i<8; i++){
    ············ CPU.writePin (CPU.pins[noparse][[/noparse]i], false); //set all pins·in CPU.PORTA as output &·low
    ·········· }

    ·········· for (i=0; i<=255; i++){
    ·········· CPU.writePort (CPU.PORTA, (byte)i);······ //if i was declared as byte, it loops forever
    ·········· CPU.delay (DelayTime);
    ·········· }
    ····· }
    ··· }
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-09-15 18:44
    The CPU class has a public method setInput that you use to make pins inputs

    ·· /**
    ·· * Make a pin an input.
    ·· * The specified pin will be converted to an input. More than one pin can
    ·· * be specified using the + operator, as long as all pins are on the same
    ·· * port.
    ·· *
    ·· * @param portPin the pin to make into an input.
    ·· */
    · public native static void setInput(int portPin);

    There is also an undocumented public internal method setOutput

    · /**
    ·· * Internal method. Do not use.
    ·· */
    · public native static void setOutput(int portPin);

    I guess it takes the same argument as setInput so you would write

    setOutput(CPU.pin0+CPU.piin1+----+CPU.pin7) to make portA all outputs.



    regards peter
  • basicstampedebasicstampede Posts: 214
    edited 2004-09-15 19:08
    Thanks.·

    CPU.setOutput (CPU.pin0 + ...+ CPU.pin7) works and so does

    CPU.setOutput (CPU.pins[noparse][[/noparse]0] + CPU.pins[noparse][[/noparse]1] +....CPU.pins[noparse][[/noparse]7]).

    So I wonder again why

    ····· final static int LEDpin = CPU.pin0;······works

    but not

    ······ static int LEDpin = CPU.pins[noparse][[/noparse]0]; and final static int LEDpin = CPU.pins[noparse][[/noparse]0];

    Anyone knows?··
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-09-15 19:19
    final static int LEDpin = CPU.pin0;··works
    because CPU.pin0 itself is a final static int.

    static int LEDpin = CPU.pins[noparse][[/noparse]0]; should work
    because LEDpin gets it value at runtime.
    Please check that again and compare the
    assigned value against CPU.pin0

    Alternatively, you may put the assignment at the beginning of your main()
    like
    static int LEDpin;
    static void main() {
    · LEDpin = CPU.pins[noparse][[/noparse]0];
    · //other code
    }
    because the moment main() is executed you know that all classes
    are initialized regarding globals.

    regards peter
    ·
Sign In or Register to comment.