3.4 Program Flow

The figure below shows an overview of the program flow.

Figure 3-7. Program Flow

The Initialize routine sets up:

  • Configures CPU clock and prescaler
  • Configures I/O pins
  • Configures all the peripherals used
  • Configures the Event system
  • Configures the interrupts and Sleep mode
  • Fetches the last used color and intensity data from EEPROM and puts them into variables
  • Make sure the LEDs are turned OFF

The Color and Intensity buttons are connected to PB0 and PB1. When either of these buttons are pressed and held low, it will create a low-level interrupt to wake-up the CPU from standby sleep.

If the Color button is pressed, it will cause the CPU to wake-up and step through the different colors and display the color on the LEDs. When the button is released, the current color is stored to EEPROM, the Color variable is updated, the LEDs are turned OFF, and the CPU returns to standby sleep.

If the Intensity button is pressed, it will cause the CPU to wake-up and step through the different intensities and display the intensity of the color chosen on the LEDs. When the button is released, the current intensity is stored to EEPROM, Intensity variable is updated, the LEDs are turned OFF, and the CPU returns to standby sleep.

The system will remain in standby sleep as long as the logical expression setup in LUT0 is false and none of the buttons are pressed. Both the ambient light sensor and the PIR sensor need to provide a logical ‘1’ to LUT0 to wake the CPU. The PIR sensor will output a logical ‘1’ when there is movement in front of the sensor. The ambient light sensor is connected to the positive pin P0 on Analog Comparator 0 and the DAC provides the voltage to the negative input. The output voltage on the ambient light sensor will decrease when the light is reduced. By using the DAC on the negative input it is possible to adjust the level where the Analog Comparator 0 will trigger and output a logical ‘1’.

When both the AC0 and the PIR output a logical ‘1’ to the LUT0 inputs, the output of the AND gate configured in LUT0 will go from ‘0’ to ‘1’, thus creating a rising edge that will wake the CPU from standby sleep. The CPU will go into the correct interrupt routine and turn ON the LEDs and then go back to sleep. If the movement stops in front of the PIR or the ambient light sensor is exposed to more light, the Analog Comparator output will go from ‘1’ to ‘0’. The AND gate in LUT0 will go low, creating a falling edge on the pin connected to LUT0 output, the CPU will wake-up and turn OFF the LEDs, and then return to sleep.