Usually, an MCU is considered a digital device. By default, its output voltage level can be either high or low and nothing in between. With the requirement to create an LED brightness control, the first idea that comes to mind is using a standard digital-to-analog converter, or design a controlled resistor network. Luckily, most modern MCUs have built-in pulse-width modulation (PWM), and this leads to the easiest and cheapest way to solve the problem.
According to our project requirements, the brightness of two LEDs has to be gradually changed from minimum to maximum and back, in the opposite phase and in a time of about several seconds. Also, the several seconds of delay between the ramps should be as shown in Figure 1. During Ramp 1 time, the PWM signal's pulse width is incremented for LED 1 and decremented for LED 2. For example, let's have the ramp time equal 2 seconds and consist of 128 up/down steps. Thus, each step lasts about 16 ms. Note that the pulse-width change should occur only once during the PWM period. Hence, the PWM period should also equal 16 ms.
The low-end 8-bit Motorola MC68HC908-QT2 flash MCU with built-in oscillator is used. Its oscillation frequency equals 12.8 MHz, together with the prescaler programmed to a selection of 1:64, and supplies a timer clock period of 0.02 ms. Then, to generate a PWM with a 16-ms period, the number of clocks to be loaded into Timer Counter Modulo Register (Tmod) should equal 16/0.02 = 800, or $0320 in hexadecimal.
The maximum pulse width (PWMAX) can be less than or equal to the PWM period. Consider PWmax to be equal to about 15 ms. To get this maximum value from zero, for 128 steps, each step should have a value of 15/128 = 0.117 ms. By rounding it to 0.12 ms, we get PWMAX = 15.36 ms , i.e., 96% of the PWM period. Thus, in each step, the PW should ramp up/down on 0.12-ms increments, equal to 0.12/0.02 = 6 timer clocks.
Any type of MCU with PWM and any timing consideration can be used to implement this technique. Resistors R1 and R2 should be chosen according to the LEDs used. Pin pA2 is used to activate (pA2 =1) or deactivate (pA2=0) the brightness control.