what's wrong with this code?
basicstampede
Posts: 214
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
············ }
········· }
······· }
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
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
······· 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
That displays the ide messages window.
Check the 'enabled'··option.
regards peter
e.g. I want it to show 11001010, not a negative decimal number.
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
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
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);
············ }
········· }
······· }
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