Have you ever wondered how to generate a sine wave using PIC microcontroller. In this post I’ll show you a simple circuit design that can generate arbitrary wave forms including sine by using MCU and PCF8591 D/A converter. PCF8591 was briefly mentioned in PIC Development Board post as a possible upgrade. It’s a 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. We are going to configure it as D/A converter controlled by PIC16F876A.
The interface is not too complicated. Send valid address followed by configuration word, wait for acknowledge. Continue sending data to be converted. See attached code at the bottom of the page for more details. The simplest wave to generate is DC. Just output 8 bit number representing desired voltage value to a digital to analog IC using I2C communication protocol and you’re done. Triangle wave is more complex to produce but still not too complicated. By sending an increasing binary number from 0 to 255 you can produce linearly rising wave. When you reach 255 change the direction by decreasing the number from 255 back to 0 to complete falling wave part and repeat in a loop. Highest number sent in one cycle will determine the max signal amplitude. Sine requires more advanced software and can be generated by using LUT-Look Up Table with predefined wave values stored in PIC memory. Retrieve valid data from the sine table, normalize if needed and send it to D/A converter, increase table index and repeat till you’ve completed one cycle. If you’ll look at the software files this is exactly how the code behaves. Enter 010 using DIP Switch to produce DC, 001 to generate Triangle wave and 000 for Sine wave.
Currently the only way to change the amplitude of the signal is to modify the code, no real time amplitude control is implemented, also the frequency range is relatively low a few KHz at best due to slow I2C clock of PCF8591. With faster Digital to Analog converters higher frequencies can be reached. Click here for Assembly/hex files. For PIC16F877A SPI interface you may also look at Digital Power Supply post.