163k views
4 votes
Write a program in C for your hardware that:

1) Starts with one of the two LEDs in the ON state.
2) On the first button press, turns off this LED.
3) On the second button press, turns on the second LED.
4) On the third button press, turns off the second LED.
5) On the fourth button press, turns on both LEDs AFTER roughly one second (don't worry about the exact timing, just make sure there is a noticeable delay by using the timers). NOTE: It would be a good idea to set a breakpoint inside a timer handler to test this. Also, you will need to toy with the timer prescaler registers to get a noticeable delay.
6) On the fifth button press, turns off both LEDs AFTER roughly one second.
7) On the next button press, starts in the initial state. Make sure that this process of button presses can be repeated (i.e. I can go through this cycle as many times as I want to.)

User Levirg
by
7.9k points

1 Answer

0 votes

Final answer:

To write a program in C for your hardware that implements the given requirements, you would need to use the GPIO pins of your microcontroller to control the state of the LEDs and the button interrupts to detect button presses.

Step-by-step explanation:

To write a program in C for your hardware that implements the given requirements, you would need to use the GPIO pins of your microcontroller to control the state of the LEDs and the button interrupts to detect button presses. Here is a high-level outline of the program:

  1. Initialize the GPIO pins for the LEDs as outputs and the button pin as an input with interrupt.
  2. Create a variable to keep track of the current state of the LEDs (ON or OFF).
  3. Create a variable to count the number of button presses.
  4. Implement the interrupt handler function for the button pin. In the handler, increment the button press count and perform the corresponding LED state change based on the count.
  5. Implement a delay function using timers to achieve the roughly one-second delay.
  6. In the main loop, continuously check for any changes in the button press count and perform the required actions based on the current count.
  7. Add a condition to reset the button count back to zero and turn off both LEDs when the count reaches the maximum value (six in this case).

This is just a general outline, and the actual implementation would depend on the specific microcontroller you are using and the programming environment. Be sure to refer to the datasheet and documentation of your microcontroller to understand how to configure the GPIO pins and interrupts.

User Abdul Aziz Barkat
by
7.8k points