This lesson uses D/A conversion to make the LED lights on the Propeller Activity Board (original or WX version) brighter or dimmer.
In the lesson Blink a Light [1], the Propeller is programmed to output 3.3 V on an I/O pin to turn a light on, and to output 0 V to turn a light off. The Propeller can also be programmed to switch an I/O pin between 0 and 3.3 V very rapidly, in a pattern that delivers an average output voltage anywhere between 0 V and 3.3 V. These duty-modulation patterns, called duty-modulation signals, change the average "on" time for the LEDs which our eyes will interpret as different levels of brightness.
This activity is not compatible with the Propeller FLiP.
Although you can do this kind of duty-modulation D/A conversion for LEDs on any Propeller I/O pin, P26 and P27 have LEDs built into the Propeller Activity Board (original or WX version), so let’s use those.
On the Propeller Activity Board (original or WX version), P26 and P27 are also connected to filter and amplifier circuitry that converts the duty-modulated D/A signals into steady voltages. Those voltages are then available at the D/A sockets, D/A0 and D/A1. These sockets are useful for other circuits that might need need a specific steady voltage but draw a varying amount of current. We can use the board's A/D converter to measure the voltages we set on the D/A sockets.
Note: This lesson is designed for the Propeller Activity Board (original or WX version). On the Propeller Board of Education, you can use this D/A technique to control the brightness of LEDs. However, the Propeller Board of Education uses a different A/D converter chip, so trying to use the A/D sockets to measure the voltage at the D/A sockets will not work.
This example program will make P26 send a 2.5 V signal to D/A0, and P27 send a 1 V signal to D/A1. As a result, the P26 LED should be considerably brighter than P27.
The dac_ctr(26, 0, 194) call has three parameters: pin, channel, and dacVal. Pin is the Propeller I/O pin that you want to send the D/A signal, and this example uses 26.
The channel parameter allows you to select one of two counter modules (0 or 1) in a Propeller core (also called a cog) to generate the duty-modulation signal. This example uses channel 0.
The dacVal parameter allows you to set the voltage as the number of 256ths of 3.3 V. This example uses 194, so we'll expect D/A1 to send 194/256ths of 3.3 V. That's 2.5 V because 194/256 * 3.3 V ≈ 2.5 V.
Next, the dac_ctr(27, 1, 78) call sends a D/A signal with P27, channel 1, and D/A1 gets 78/256ths of 3.3 V ≈ 1 V.
Since we added wires to connect the D/A voltage outputs to the A/D voltage inputs, we need code to read and display the those voltages. A call to adc_volts(0) returns the measured D/A0 output, and adc_volts(1) returns D/A1’s output. Instead of storing those measurements in variables, the calls to adc_volts are nested right in the print statements, which can use their return values directly. So, print("A/D0 = %f\n", adc_volts(0)) sends the result of the adc_volts(0) call straight to the SimpleIDE Terminal. Likewise with print("A/D1 = %f\n", adc_volts(1)).
/* Set and Measure Volts.c Set D/A0 to 2.5 V and D/A1 to 1 V, and measure with A/D0 and A/D1. */ #include "simpletools.h" // Include simpletools #include "adcDCpropab.h" // Include adcDCpropab int main() // main function { adc_init(21, 20, 19, 18); // CS=21, SCL=20, DO=19, DI=18 dac_ctr(26, 0, 194); // D/A ch 0 -> 2.5 V to D/A 0 dac_ctr(27, 1, 78); // D/A ch 1 -> 1 V to D/A 1 print("A/D0 = %f\n", adc_volts(0)); // Display A/D0 volts print("A/D1 = %f\n", adc_volts(1)); // Display A/D1 volts }
The channel 0 and channel 1 parameters don't have anything to do with D/A0 and D/A1. D/A0 makes a steady voltage from P26 signals and D/A1 makes a steady voltage from P27 signals, no matter which channel you use.
The great thing about channels is that you can use them to move a D/A signal off one pin and onto another. For example, adding dac_ctr(9, 0, 194) after the rest of the statements would turn the P26 light off, and then turn a P9 light on (if you had an LED connected to P9, of course).
Let’s try making the voltages hold at D/A0 = 2.5 V and D/A1 = 1 V for 2 seconds, then change to D/A0 = 1 V and D/A1 = 2.5 V.
You can use variables and loops to gradually change the voltages, for a fade-out effect.
Links
[1] https://learn.parallax.com/propeller-c-simple-circuits/blink-light