/* * Init SPI on USCI UCB0 as UCA0 is used for UART (printf's) */ void spi_init(void) { #if _MCU_ == 2553 #if USE_RADIO /* * From TIs users manual * The recommended USCI initialization/re-configuration process is: */ /** 1. Set UCSWRST (BIS.B #UCSWRST,&UCxCTL1)*/ UCB0CTL1 = UCSWRST; /** 2. Initialize all USCI registers with UCSWRST=1 (including UCxCTL1)*/ UCB0CTL0 |= UCCKPH | UCMSB | UCMST | UCSYNC | UCMODE_0; UCB0CTL1 |= UCSSEL_2; /** 3. Configure ports*/ SPI_PORT(SEL) |= SPI_MISO | SPI_MOSI | SPI_SCL; SPI_PORT(SEL2) |= SPI_MISO | SPI_MOSI | SPI_SCL; /** 4. Clear UCSWRST via software (BIC.B #UCSWRST,&UCxCTL1)*/ UCB0CTL1 &= ~UCSWRST; /** 5. Enable interrupts (optional) via UCxRXIE and/or UCxTXIE*/ #endif /* USE_RADIO */ #endif /* _MCU_ == 2553 */ }
/** * For talking to the MCP4822 DAC */ void DAC_updateVoltage(uint16_t channel, uint16_t value) { // Validate channel selection if(channel <= 1) { // Cap value to 12 bits only if(value > 0x0FFF) { value = 0x0FFF; } char txBuf[2]; uint16_t cmd = 0x3000 | (value & 0x0FFF); if(channel) cmd |= 0x8000; txBuf[1] = cmd & 0x00FF; txBuf[0] = __swap_bytes(cmd) & 0x00FF; CLR_BITS(SPI_PORT(OUT),SSEL | nLDAC); // CS~ falling edge, hold nLDAC low SPI_send(txBuf, 2); SET_BITS(SPI_PORT(OUT),SSEL); // CS~ Rising edge. Because nLDAC is low, DAC output changes on rising CS~ edge. } }