Thursday, 26 April 2012

Usnig Inputs

The next thing we need to learn about is how to use inputs in microprocessor control.

In real life there are a variety of different inputs we could test - switches, temperature sensors, light sensors, strain gauges (to measure how much something has been bent) etc.  In class we are going to start by using simple switches:






Using the connections given we can only use pins 0-3 without adding any extra wiring.

Now that we are dealing with both inputs and outputs we have to make sure we set up the DDR (Data Direction Register) correctly:

init: let dirs = %11110000     'Sets pins 7, 6, 5, 4 as outputs, and 3, 2, 1, 0 as inputs

To "test" an input, we must ask about its state, i.e. whether it is on or off.  In a flowchart we must do this using a rhombus box.  Each question must be YES/NO.

Here is a flowchart using inputs, for a toy train which should travel along the track until it reaches the end.  This system uses a start switch (pin 0) and an "end switch" (pin 1) as its inputs and a motor (pin 7) to drive the train.


This can be turned into a PBASIC program using "IF . . . THEN . . . "

init: let dirs = %11000000     'Sets pins 7 and 6 as outputs, and 5, 4, 3, 2, 1, 0 as inputs

main: if pin0 = 1 then train    'tests the start switch
          goto main                     'if the start switch is not pressed, go back to main

train: high 7                            'switches on the motor
         if pin1 = 0 then train     'tests to check if the motor has reached the end of the track
         low 7                             'switches off the motor.
     
         end

If the IF . . . THEN . . . statement is true, it will go to the label stated.  (following the then MUST be a label - another part of the program to go to).  If the statement is not true, it will skip to the next line of the program.  You can ask if the pin is '1' or '0' depending on what your flowchart says.

No comments:

Post a Comment