Table of Contents

Exercises

The goal is to write a program which is able to perform tasks described below.

Warm up exercise

For beginners

  1. This simulates manually operated traffic light at pedestrian crossing. Until no button is pressed a green LED is lit for the cars. After pressing a random button the green LED will start to blink for 3 seconds, this is followed by lighting the yellow LED for 3 seconds and red for 10 seconds, after this sequence a green LED is lit again.
  2. This counts how many times the button is pressed. Only the full release of the button will complete the pressing. The result is displayed in binary code on LEDs. Maximum result for three LEDs is 7(23-1). Green marks 1. bit, yellow 2. bit and red 3. bit.
  3. By pressing switch S1, LED1 and LED3 are lit; pressing S2 yellow LED is lit, pressing S3 all LEDs are switched off. This operation must be done by directly changing the values of corresponding registers (without using the library of the HomeLab).
  4. This counts how many times the button is pressed. The result is displayed by blinking of the LEDs. After each pressing the number of blinking is increased by one. A random button can be selected. In order for the LED to blink a sub function must be used, with parameters set on the number of blinks.
  5. When pushing button S1, red LED will blink “SOS” in Morse. By pressing S2, yellow LED blinks “CQD” and by pressing S3, green will blink “OK”.

For advanced

  1. For each of the three buttons corresponds one LED which lights up when the button is pressed. For turning on or off, registers must be used and only one assigning operation (hint: use bit shifts).
  2. For each of the three buttons corresponds one LED which lights up when the button is pressed. For turning on or off, registers must be used and only one assigning operation (hint: use bit shifts).
  3. There is one button for each LED. The controller blinks LEDs randomly and the user has to repeat the same sequence. The sequence of blinks gets longer – one random LED is added with a new round. The sequence is controlled after each user. The time gap between each entry is two seconds. In case of an incorrect entry all LEDs blink three times. (The number of correct entries by the user may be displayed on a LCD screen).
  4. The program measures time for reaction. A random LED is lit and the user has to press the corresponding button as fast as possible. The time when the LED is lit is random, but not less than 100 ms. the result is displayed on a LCD screen in milliseconds. The value of the buttons cannot be read with filtering function for the flickering, since this will cause an extra delay.

Questions

  1. What is the difference between operations”=” and”==” ? Give two examples to verify the statement.
  2. What is the difference between operations “|” and “||”? Give two examples to verify the statement.
  3. Write an expression using the equation “x = x + 1” twelve times.
  4. How an endless loop is written in C-programming language? Provide two different examples.
  5. What type of a variable do you choose in C–language to present positive values between 7 and 154?
  6. Witch register determines the direction of a port? Provide an example of configuring settings of ports’ input and output.
  7. What are the methods for eliminating the bouncing of the contacts and the false connections caused by flickering? Provide examples of solutions using both hardware and software.
  8. What is bit shift? Give a practical example with an explanation.
  9. Why are pull-up resistors used in switches? How is their resistance determined?
  10. Calculate the resistance for regulating LED current, the voltage is 5 V, forward voltage of the LED is 2,7 V and the current is 30 mA.