#include "msp430x20x3.h" //Header for MSP430F2013, located at //C:\Programme\IAR Embedded Workbench\430\inc //Function prototypes void spi_init(void); //Initialization of SPI void v_out(void); //Set output voltage of the d/a-converter //Main function void main(void) { WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer to avoid timeout reset spi_init(); //Initialize SPI v_out(); //Set output voltage } //SPI initialising function void spi_init(void) { P1DIR |= BIT0; //Configure P1.0 (=BIT0) as output: LED P1DIR |= BIT1; //Configure P1.1 (=BIT1) as output: CS2 for a/d-converter AD7787 P1DIR |= BIT2; //Configure P1.2 (=BIT2) as output: CS3 for d/a-converter MAX5530 P1OUT &= ~BIT0; //Switch off the LED (low-level) P1OUT |= BIT1; //Set P1.1 (=CS2) on high-level to disable the AD7787 P1OUT |= BIT2; //Set P1.2 (=CS3) on high-level to disable the MAX5530 USICTL0 |= USISWRST; //Software reset; stop USI USICTL0 |= USIPE7 + USIPE6 + USIPE5 + USIMST + USIOE; //Write to USI Control Register 0: // Activate ports P1.7 (MISO), P1.6 (MOSI), P1.5 (SCLK) for SPI // USIMST: USI Master Select; If bit is set (as here), then MSP430 is master // USIOE: USI Output Enable USICTL0 &= ~USILSB; //Delete USILSB-bit to use MSB-first-mode USICKCTL = USISSEL_2 + USIDIV_2; // SPI clock rate: SCLK = SMCLK /4 USICTL0 &= ~USISWRST; //USI released for operation: Delete "Software reset"-bit USICNT = 16; //Load USI Bit Counter Register (Family user's guide chapter 10.2): // USICNTx-Bits value: 16; in order to send 16 bit of data (16-Bit-converter) // all other bits of this register are deleted // USIIFGCC is one of these bits. Therefore USIIFG is set, if USICNTx reach zero USICNT |= USI16B; //Set USI16B-bit of USICNT-register: enable 16-bit-shift-register mode in order // to send 16 bit of data. So USISRL and USISRH shift registeres are used // other bits are not modified } void v_out(void) { int voltage; voltage = 0x0800; //Set output voltage: 0800hex = 2048dec = 1.25 V P1OUT &= ~BIT2; //Enable MAX5530 (set CS3 on low-level) USISR = 0xF000 + voltage; //Control bits: 1111bin = Fhex; input data: voltage P1OUT |= BIT2; //Disable MAX5530 (set CS3 on high-level) }