Figure out, you conceive a powerful digital sensor and you want to use it with the LEGO RCX. You must transform the digital information to an analog signal. Generally this is done through a special IC. While such a solution is the easiest for engineers, amateurs may have difficulties in finding the optimal chip, or may be limited in budget. We already presented a crude 8-bit converter based on a simple resistor-net.
Here now two other types of converters.
The LEGO-Mindstorms project in an educational context rapidly shows its power around heuristic thinking. In team-discussions about solving a problem, several approaches appear in concurrence. The team must proceed to an evaluation, which solution is the best. In the case of deciding if the team should prefer a D/A-integrated circuit to another solution, different arguments must be weighted such as efficiency, speed, costs, simplicity, space needed on the PC-board, choice of micro-controller (if ever), number of micro-controller I/O's available, and so on. For example, sometimes you need only a rather slow conversion, but you want to spare some room on the board and you want to work with one of the cheapest PICs. In this case you'll use the following device.
1. Pulsing D/A converter (Integrator)
A PIC (16F84) is driven at 4 MHz. One I/O-pin is configured to be an output. The idea is to generate a pulse at a given duty x%. This signifies that the pin is set to 1 (=5V) during x% of the period and to 0 (=0V) during (100-x)% of the period. The PIC-pin is buffered through an operation amplifier which changes the impedance. Then the signal is led to an RC-cascade. The couple R1-C1 integrates the signal. This means that the capacitor C1 is charged during x% of the time then discharged during (100-x)% of the time.
Suppose the capacitor's voltage is zero initially. As the charge rate depends on the charge time, the initial voltage and the time constant T=RC, every cycle the capacitor is not completely discharged. To be precise only (100-x)% of the charge is lost.
Example: x=50%, the voltage after one cycle is either 1.25 or 3.25 V depending on the capacitor's initial state. This is half of the charge. (The timerate in the picture does not correspond to R1C1!! It is only an illustration of what is going on)
The R2-C2 / R3-C3 cascade filters the signal.
If we choose y as a value of the interval 0 .. 255 with x = y / 256, we'll divide one cycle into 256 segments. Thus we may transform any byte-number to a proportional voltage.
At 4MHz, the internal PIC program counter is incremented at 1 MHz. Deviding into 256 segments gives a frequency of 3.9kHz. As you can see in the following pictures, the voltage is stabilized around the duty-percentage after about 6ms. By choosing other values of R1, R2, R3, C1, C2 or C3, it is possible to have a quicker respons. You'll have to make a compromise between speed and signal-stability.
Note that the charging of a capacitor obeys to the formula:
The discharging has the formula:
Constantly driven, the capacitor is charged to 98% resp. discharged to 2% after 4t.
Note that you should test the RC-cascade with an oscilloscope or an electronic simulator on your PC.
Conclusion:
We get a direct proportional relationship between duty-percentage and the output voltage.
Here a (not too elegant) PIC test-program for a 50% duty pulse.
LIST P=16F84 ; PORTA EQU 5 PORTB EQU 6 TRISA EQU 85H TRISB EQU 86H STATUS EQU 3 INTCON EQU 0BH OPTREG EQU 81H TMR0 EQU 1 CY EQU 0 RP0 EQU 5 TOIF EQU 2 TOIE EQU 5 GIE EQU 7 #DEFINE BANK0 BCF STATUS,RP0 ;SELECT BANK 0 #DEFINE BANK1 BSF STATUS,RP0 ;SELECT BANK 1 ;******************************************************** ORG 0 GOTO START ORG 4 COMF PORTB,F ;INVERT PORTB BCF INTCON,TOIF ;CLEAR INTERRUPT FLAG RETFIE LOOP GOTO LOOP ;DO NOTHING FOREVER START BANK1 CLRF TRISA^80H ;PORTA ALL OUTPUTS CLRF TRISB^80H ;PORTB ALL OUTPUTS MOVLW B'00000001' ;TIMER0 PRESCALE 1:2 MOVWF OPTREG^80H BANK0 CLRF PORTA CLRF PORTB BSF INTCON,TOIE ;ALLOW TIMER INTERRUPT BSF INTCON,GIE ;ALLOW ALL INTERRUPTS CLRF TMR0 BCF INTCON,TOIF ;CLEAR INTERRUPT FLAG GOTO LOOP END |
2. Resistor-net
Trying to understand this astute resistor-net designed to transform a digital port-value to an analog signal is a very interesting didactical exercise. The trick is to combine resistors of only two different values R and 2R in such a way that the net finally gives the exact voltage-output which is proportional to the PIC output-value. The respons of this converter is very quick compared to the former example, but instead you need more than one PIC output-pin, according to the desired accuracy.
For better understanding here a three-bit example:
HINT: analyse the pictures in the order:
port_value=0, port_value=4, port_value=2, port_value=1
As you can see, the sub-systems of the nets are always parallel or serial resistors of values:
2R = R + R or R = 2R*2R/(2R+2R) or more complex combinations.
![]() |
|
|
|