Page 336 - ARM Based Microcontroller Projects Using MBED
P. 336

/*************************************************************************
                                                    SPI BUS SQUARE WAVE GENERATOR
                                                    =============================
                            This is an SPI bus project. An MCP4921 DAC is connected to SPI channel
                            2 of the Nucleo-F411RE development board. The program generates analog
                            square wave signal through the DAC with frequency of 1kHz (period=1ms)
                            and amplitude of 1V.
                            Author: Dogan Ibrahim
                            Date  : September 2018
                            File  : SPIDAC
                            **************************************************************************/
                            #include "mbed.h"

                            SPI dac( PC_3, PC_2, PC_7, PB_9);               // MOSI,MISO,SCLK,SSEL
                            DigitalOut CS(PC_0);                            // CS of the DAC chip

                            //
                            // This function sends 12-bit digital data to the MCP4921 DAC. The
                            // HIGH byte is sent first, followed by the LOW byte
                            //
                            void DAC(unsigned int value)
                            {
                                char temp;

                                CS = 0;                                     // Enable CS
                            //
                            // Send HIGH byte
                            //
                                temp = (value >> 8) & 0x0F;                 // Bits 8-11 in temp
                                temp |= 0x30;                               // Define DAC gain=1x
                                dac.write(temp);                            // Send HIGH byte
                            //
                            // Send LOW byte
                            //
                                temp = value;
                                dac.write(temp);                            // Send LOW byte
                                CS = 1;                                     // Disable CS
                            }
                            //
                            // Main Program
                            // Generate square wave with amplitude 1V, and period 1ms
                            //
                            int main()
                            {
                                unsigned int amplitude;

                                amplitude = 1000 * 4095 / 3300;             // 1V
                                CS = 1;                                     // Disable CS

                                while(1)
                                {
                                    DAC(amplitude);                         // Send 1V to DAC
                                    wait(0.0005);                           // Wait 0.5ms
                                    DAC(0);                                 // Send 0V to DAC
                                    wait(0.0005);                           // Wait 0.5ms
                                }
                            }

                 FIG. 12.7  Program listing.
   331   332   333   334   335   336   337   338   339   340   341