Sei sulla pagina 1di 2

Input

On pages 27 and 27 of the PicAxe manual they discuss switches and how to wire them.
The diagram shows a 1K resistor between one leg of the switch and the PicAxe pin. This
is important because the current flow must be very low. The other leg of the switch is
connected through a 10K resistor to either +5 or 0.

In the diagram the PicAxe pin is connected through both


resistors to 0 VDC. This is referred to as pulling the pin
low, and the PicAxe will see the pin as LOW. When the
switch is pressed the +5 VDC will be seen by the PicAxe
and the pin will be pulled HI.
And yes, when the switch is pressed there is a short
circuit between the +5 and 0 but it is through a 10 K
resistor so the current will be very small and not a
problem.
So, get two of each 1K resistors, 10K resistors and momentary switches. P&P has
bunches of unused switches in the electronics cabinet across from the control panel in the
layout area. And build a circuit that will run the program shown in:
http://pandprr.com/newpandprr/PicAxe_files/PWM.pdf
Print that page for reference in this next discussion. Read the program line by line
(including the comments) to see how it operates. If you can follow every step of the
program, ignore the next section.
The first couple lines are familiar to you already. They get the motor running.
Top: is the designation line main: that we have used. It is a place to which the
program can be directed.
Gosub sends the program to a block of commands called Flash, and when the word
return is encountered the program will return to the end of the line that sent it on its
way. Sort of a round trip ticket.
Examine Flash. It simply blinks the LED attached to pin 0 once and then goes back.
The program returns and drops through to the next line, where questions are asked
about the status of the switches (plural!). Remember that the switched were wired to be
pulled low when they are not pressed which yields a LOW state for the pin.

First if the switch on pin 3 is AND the switch on pin 1 are both pressed they will read
HIGH on both and the statement is TRUE, so the command on the rest of the line is
executed. i.e. Then Cease which means goto the Cease sub routine and do whatever it
says, but dont come back! The command goto is understood in this line and can be
omitted. (Programming 204 .... ;-)
When we look at cease we can see that it just turns off the PWM at pin 2 by making the
period 0. Remember that the period is the size of the frame in which the duty is carried
out. No frame, no power to the motor. And then it sends the program back to Top.
Thats not quite like starting over because it doesnt include the original motor start that
happened with the first run of the program, so the motor remains stopped.
The next two lines look for a single button push and react accordingly. If no buttons are
pushed the program falls thru to the next command which is GoTo Top
AS you can see the program jumps around a LOT! Every time the LED flashes the
program has run all the switch checks and acted on the instructions.
New terms are:
Goto and Gosub. These terms direct the program to a block of code that does a specific
task, like blink an LED, increase the speed of a motor, etc.
Goto is a one-way-trip and once the instructions have been carried out the operation
continues to the next set of instructions. If there are none, the program stops!
Gosub directs the program to a routine that ends in the command return, which send the
program back to the line that sent it there, and the program continues.
IF ... THEN is used to test to see if something is true. If the answer is true action
described after the word THEN is performed. If the answer is false the instructions after
THEN and ignored and the program drops through to the next line.
AND between IFs means that all of the test results must be true for the THEN to be
executed. (There can be multiple ANDs between IF and THEN).
OR could also be used in place of AND to test for either of two conditions. Again,
multiple ORs can be between IF and THEN.

Potrebbero piacerti anche