Shop OBEX P1 Docs P2 Docs Learn Events
what's wrong with this code? — Parallax Forums

what's wrong with this code?

basicstampedebasicstampede Posts: 214
edited 2006-10-27 15:50 in General Discussion
Hello.· The program below compiles and programs OK into Javelin, but it doens't do what I expected.
What I expected is that it would read 8 switches connected to Port B and mirror that to Port A which is connected to 8 LEDs (I'm using INEX board).

Specifically questions are:

1.· How do I make all of PORTB as output using 1 statement?· Is there an equivalent of setInput?· Say setOutput?· I haven't seen it in the manual.

2.· For some reason, the variable inputswitch is always read as 0.· What's wrong?

3.· LEDs are never lit.· Even the CPU.writePin(CPU.pins[noparse][[/noparse]8], true);· doesn't work.

import stamp.core.*;
······· public class led_count_10_27_06
······· {
········ public static byte inputswitch;
········· public static void main()
········· {
··········· CPU.writePin(CPU.pins[noparse][[/noparse]8], true);· //how do I make all of PORTB as output·in 1 statement?
··········· CPU.writePin(CPU.pins[noparse][[/noparse]9], true);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]10], true);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]11], false);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]12], false);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]13], false);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]14], false);
··········· CPU.writePin(CPU.pins[noparse][[/noparse]15], false);
··········· CPU.delay (10000);
··········· while(true)
··········· {
·············· //read 8 input switches
·············· inputswitch = CPU.readPort(CPU.PORTB);
·············· System.out.println ("inputswitch = ");
·············· System.out.println (inputswitch);· //for some reason, this is always 0 regardless of input
·············· //write·to LED
···············CPU.writePort (CPU.PORTA, (byte) inputswitch);· //this·doesn't work
·············· CPU.writePort (CPU.PORTA, (byte) 0xFA);· //this doesn't work either...LEDs are not lit at all
············ }
········· }
······· }

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 13:09
    Check the file CPU.java in folder lib/stamp/core.
    There is a method setOutput, but may be defined private. Make it public.
    Make sure the file is not readonly or it will not save the new setting.

    To make all pins of a port an input, use
    CPU.setInput(CPU.pin0|CPU.pin1|CPU.pin2|CPU.pin3|CPU.pin4|CPU.pin5|CPU.pin6|CPU.pin7);
    To make all pins of a port an output, use
    CPU.setOutput(CPU.pin0|CPU.pin1|CPU.pin2|CPU.pin3|CPU.pin4|CPU.pin5|CPU.pin6|CPU.pin7);
    You use pin8 to 15 for port B.

    Note that CPU.writePort does not affect the pin directions.
    So use CPU.setOutput at least once, before using writePort.

    You make port B pins all low outputs, then read port B into inputswitch.
    So, inputswitch is always 0.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 13:16
    import stamp.core.*;
    ······· public class led_count_10_27_06
    ······· {
    ········ public static byte inputswitch;
    ········· public static void main()
    ········· {· //make port A pins all outputs (portA = leds)
    ··········· CPU.setOutput(CPU.pin0|CPU.pin1|CPU.pin2|CPU.pin3|CPU.pin4|CPU.pin5|CPU.pin6|CPU.pin7);
    ··········· while(true)
    ··········· {
    ·············· //read 8 input switches (portB = switches)
    ·············· inputswitch = CPU.readPort(CPU.PORTB);
    ·············· System.out.println ("inputswitch = ");
    ·············· System.out.println (inputswitch);
    ·············· //write·to LED
    ···············CPU.writePort (CPU.PORTA, (byte) inputswitch);
    ············ }
    ········· }
    ······· }

    That should do it.
    regards peter
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 13:55
    Peter, thanks.· That works.·Somehow, I disabled the System.out window.· How do I turn that back on?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 13:58
    Go to menu debug->messages
    That displays the ide messages window.
    Check the 'enabled'··option.

    regards peter
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 13:58
    Never mind.· I got it.· The display shows that inputswitch is a negative number.· How do I format it to binary?

    e.g. I want it to show 11001010, not a negative decimal number.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 14:03
    The easiest way is to use my Format class.
    To print a byte value as binary string you would use
    Format.printf("inputswitch = %08b\n",inputswitch);

    Just add import stamp.util.text.*; at the start
    You can find the Format class here:
    http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/

    regards peter
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 14:19
    Peter, after I get that file from the yahoo site, where do I have to store it so Javelin can find it?
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 14:21
    Peter, it is a pdf file and a manual (not code).· I've downloaded it, but I'm not sure how I can use it in Javelin code.
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 14:25
    OK.· I found the java file.· Please advise where I need to save this.· I put it somewhere and Javelin cannot find it.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 14:25
    The pdf is the manual for the Format class.
    The file you need is called Format.java
    You must place it on your harddisk in folder
    %path%\lib\stamp\util\text\
    If necessary, create the folders util and text first.
    %path% is your Javelin installation path.
    Default c:\program files\parallax inc\javelin stamp ide\
    Make sure to use lowercase letters for util and text.

    regards peter
  • basicstampedebasicstampede Posts: 214
    edited 2006-10-27 15:03
    I placed the·java file where you told me.· However, it still says "Error.· Type Format was not found."······

    import stamp.core.*;
    ······ import stamp.util.text.*;
    ······· public class led_count_10_27_06
    ······· {
    ········ public static byte inputswitch;
    ········· public static void main()
    ········· {
    ··········· CPU.setOutput(CPU.pin0|CPU.pin1|CPU.pin2|CPU.pin3|CPU.pin4|CPU.pin5|CPU.pin6|CPU.pin7);
    ··········· while(true)
    ··········· {
    ·············· //read 8 input switches
    ·············· inputswitch = CPU.readPort(CPU.PORTB);
    ·············· //System.out.println ("inputswitch = ");
    ·············· //System.out.println ((int)inputswitch);
    ·············· Format.printf("inputswitch = %08b\n",inputswitch);
    ·············· //write this to LED
    ·············· CPU.writePort (CPU.PORTA, inputswitch);
    ············ }
    ········· }
    ······· }
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-10-27 15:50
    The program looks good.
    Use windows explorer and navigate to your Javelin Stamp IDE folder
    Doubleclick folder lib
    Doubleclick folder stamp
    Doubleclick folder util, if no such folder create it using lowercase letters
    Enter folder util
    Doubleclick folder text, if no such folder create it using lowercase letters
    Enter folder text
    In that folder store file Format.java, as I spelled it here.

    Your program compiles succesfully on my pc.

    regards peter

    Post Edited (Peter Verkaik) : 10/27/2006 3:55:41 PM GMT
Sign In or Register to comment.