205 of 307
ZBasic System Library 195 ZX Microcontroller Family PutDAC Type Subroutine Invocation PutDAC(pin, dacValue, dacAccumulator) PutDAC(pin, dacValue, dacAccumulator, cycles) Parameter Method Type Description pin ByVal Byte The output pin. dacValue ByVal numeric The desired output value. See discussion below. dacAccumulator ByRef Byte A value used in the DAC process. See the discussion below. cycles ByVal Byte The number of PWM cycles to perform. Discussion This routine creates a digital approximation of an analog signal on the specified pin using a pseudo-PWM technique. When called, the specified pin is made an output, a pulse train is generated having an average value equal to the dacValue parameter and then, after a fixed number of iterations, the pin is placed in the high impedance input state. If the output is filtered with a low pass filter, the voltage will, immediately after the process is completed, be at a level between zero and the processor voltage (usually +5 volts). However, the voltage will begin to decay at a rate dependent on the load presented to the filter. The voltage can be refreshed from time to time by calling PutDAC() again. The dacValue parameter may be specified by a Single value or an integral value. If a Single value is supplied, it should be in the range 0.0 to 1.0 corresponding to the output range of 0 to the processor voltage (usually +5 volts). If an integral value is supplied, it should be in the range of 0 to 255 corresponding to the same output voltage range as above. The dacAccumulator parameter is required to ensure continuity between successive calls to PutDAC(). The value of the parameter after the call should not be modified and the same parameter should be supplied on the next call. The initial value of the parameter is of no consequence. If your application uses PutDAC() to create an analog voltage on more than one pin at a time, a separate accumulator value must be used for each one. If the cycles parameter is not specified, a single PWM cycle is performed. Each cycle will generate a burst of pulses for about 200µS during which time interrupts will be disabled. At the end of each cycle, the pin is put in high impedance mode and interrupts are re-enabled. The process is then repeated if the cycle count is greater than one. A cycle count of zero causes no cycles to be performed. The selection of components for the required filter depends on several factors. A larger capacitor will allow the voltage to hold longer but also takes longer to bring up to the proper voltage. As a rule of thumb, the product of the resistance (in ohms) and the capacitance (in farads) should be on the order of the number of cycles times 50µS. For example, with a 100O resistor and a 1µF capacitor, the cycle count should probably be 2 in order to bring the capacitor up to the desired voltage level. For best results, you should probably follow the filter with a high impedance buffer such as a unity gain op amp circuit, an example of which is shown below. The op amp chosen is not particularly critical, nearly any will do the job.