16x84 assignment 1 : blinking

This is the most simple 16x84 circuit you can make: a blinking LED. The circuit is:

blink a LED circuit blink a LED foto

Note: the components of the basic circuit are not shown!

The accompanying program is:

   include 16c84_10
   include jlib
   pin_a0_direction = output 
   forever loop
      pin_a0 = high 
      delay_10mS( 50 )
      pin_a0 = low 
      delay_10mS( 50 )
   end loop

The first line indicates what kind of 16x84 you use and with which crystal frequency. Adapt this line accordingly when you use something else than a 16c84 with a 10 MHz crystal.

The second line indicates that you want to be able to use the things which are declared in the jlib library file. This library contains all kinds of delarations which you would not want to write yourself, for instance the various delay routines.

The first real line makes pin A0 an output. Without this line you could manipulate the pin to your heart's content without any observable effect on the outside.

The other lines create a loop which in this case will be repeated forever. First pin A0 is made high. Next a delay of 50 times 10 mS = 500 mS (half a second). Next the pin is made low and we wait again. Then the loop starts again.

The net effect is that the LED will blink with a frequency of 1 Herz (1 blink per second).

The delay_10mS lines can be changed to make the LED blink at a different frequnecy, or to change the on and off times. The number between braces must be a number in the range 0 .. 255. When that is not sufficient another delay procedure must be used, for instance delay_1mS or delay_100mS. The actual delay is always the number between braces multiplied by that delay indicated by the procedure name.

work blink1.jal
Build the circuit and program the 16x84.

work blink2.jal
Now change the program to make the LED blink with a frequency of 1/5 Hz (one blink each 5 seconds), so that the LED is off for 4 seconds and on for 1 second.

work blink3.jal
Use two extra LEDs and resistors (total 1 red, 1 orange and 1 green). Use the pins A0 (red), A1 (orange) and A2 (green). Make a dutch traffic light which cycles through these pahses:

The terms high and low are not very clear because high trurns a LED off. The following lines define light and dark.

   const bit light = low
   const bit dark  = high

work blink4.jal
Add the light and dark lines to your program and use these values instead of low and high.

work blink5.jal
The previous traffic lights were dutch. Now create an english traffic light which cycles through these phases:

work blink6.jal
Use 3 more LEDs and resistors. Use the additional pins B0 (red), B1 (orange) and B2 (green). Create two traffic lights on a crossing. First write down the complete cycle. Keep in mind that some drivers will pass a light which has just turned red, so there must be a small time where all lights are red.

The last program is not very clear. We can improve it by creating a procedure which is called once for each pahse in the cycle. Inside the procedure each light is turned on or off appropriately and an appropriate delay is called.

   const byte red    = 0
   const byte orange = 1
   const byte green  = 2

   procedure phase(
      byte in seconds,
      byte in main,
      byte in secondray
   ) is
      pin_a0 = ! ( main == red    ) 
      pin_a1 = ! ( main == orange ) 
      pin_a2 = ! ( main == green  ) 
      pin_b0 = ! ( secondary == red      ) 
      pin_b1 = ! ( secondary == orange   ) 
      pin_b2 = ! ( secondary == green    ) 
      delay_1S( seconds )      
   end procedure
The first part of the procedure declaration is the header. It defines how the procedure is called, in this case with 3 parameters, which indicate:

Inside the loop we put one call for each phase:

   forever loop
      phase( 1, red,    red    )
      phase( 5, green,  red    )
      phase( 1, orange, red    )
      phase( 1, red,    red    )
      phase( 3, red,    green  )
      phase( 1, red,    orange )
      phase( 1, red,    red    )
   end loop

work blink7.jal
Change your program as indicated.

work blink8.jal
Take two more LEDs (red and green) and connect thse to the pins B3 and B4. These LEDs will form the pedestrians lights (no orange light for pedestrians). Change the procedure to control these two additional LEDs. Change the main loop to give the pedestrians time to cross. You can choose for yourself whether you want to give green to the pedestrians after each read or only after both roads.

The traffic light system which we just made will always give green to the pedestrians, even when there are none. Most crossings have pushbuttons which must be pressed to schedule a green phase for pedestrians. Add a (normally open) pushbutton switch between pin A4 and ground, and add a 10k resistor between pin A4 and Vcc.

It is reassuring for a pedestrian to get some feedback from pushing the button, even we he can not cross immediately. Add an extra (yellow) LED and resistor on pin B5. This LED must go on when the button is pressed, and it must go off as soon as the pedestrians get a green light.

full traffic light circuit

Somewhere in the program we will have to detect whether the button is pressed. By itself that is no big deal. An IF statement can be used to execute a part of the program only when the condition holds, for instance only when pin B4 is low.

   if pin_a4 == off then
      pin_b5 = light 
   end if
This small piece of code already achieves that the yellow LED will light when the button is pushed. The only problem is that we must constanly execute this piece of code, otherwise a pushing of the button might be missed. Our previous programs used the delay_1S procedure which would cause the button to be ignored for the duration of the delay.

We gan solve this problem by subsitution our own delay procedure which looks at the button, delays a small time, looks at the button again etc. untill the desired total delay time has exprired. When we keep the small delay time small enough (e.g. 10 ms) the pedestrian will not notice that the button is not always serviced. We will create two procedures. The first one looks at the switch and delays for 10 ms, and repeats this 100 times for a total delay of 1 second.

   var bit pedestrians = no

   procedure switch is
      for 100 loop
         if pin_a4 == low then
            pin_b5 = light
            pedestrians = yes
         end if
         delay_10mS( 1 )
      end loop
   end procedure
The procedure contains a for loop which explicitly states how many times the loop must be executed, in this case 100 times. Within the loop there is the code which looks for a button press and a delay of 10 ms. When the button is pressed the acknowledge LED is lighted and the global flag pedestrians is set to true.
   procedure delay_seconds( byte in seconds ) is
      for seconds loop
         switch
      end loop
   end procedure

The second procedure is very simple: we want to delay a fixed number of seconds, so we just call our previous procedure that number of times.

Within the main loop we only have to put an IF statement around the pedestrians phases. Within this IF statement we must also clear the pedestrians flag and switch the acknowledgement LED off.

   pin_b5 = dark
   forever loop
      phase( 1, red,    red,     red )
      phase( 5, green,  red,     red )
      phase( 1, orange, red,     red )
      phase( 1, red,    red,     red )
      phase( 3, red,    green,   red )
      phase( 1, red,    orange,  red )
      phase( 1, red,    red,     red )
      if pdestrians then
         pin_b5 = uit
         phase( 10, red,  red,  green )  
         pedestrians = no
         phase( 10, red,  red,  red   )  
      end if
   end loop

work blink9.jal
Add the new hardware and modify the program.