Close

Setting up the SPI

A project log for 1K Challange Laser

Draw the hackaday logo with a laser with less than 1K of data.

cyrille-gindreauCyrille Gindreau 12/09/2016 at 19:230 Comments

In order to use the DAC, we needed to initialize the SPI on this device as well as create a method to write the DAC values. The module uses an MCP4921 DAC IC which is at 12bit resolution. The voltage conditioners in the modules will scale the min input voltage as zero and the max output voltage as 4095. In this case 0 is -12V and 4095 is 12V making 2048 0V.

Being on the SPI Bus each DAC module is activated using the slave select PIN. We Defined these as SSX and SSY

MSP430 SPI Initialization

WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	PM5CTL0 &= ~LOCKLPM5;//sets to 12 mh

	CSCTL1 |= DCORSEL_6;//sets clockspeed

	P1DIR |= BIT0 + SSX + SSY;//p1 output
	P1OUT |= BIT0;//set bit 0

	///////////////
	///SPI Int//////
	////////////////
	P5SEL0 |= BIT1 + BIT2;
	UCB0CTLW0 |= UCSWRST;                     // **Put state machine in reset**
	UCB0CTLW0 |= UCMST+UCSYNC+UCCKPL+UCMSB;   // 3-pin, 8-bit SPI master MSB
	UCB0CTLW0 |= UCSSEL_2;                    // SMCLK
	UCB0BR0 |= 0x01;						  // CLK / 1
	UCB0BR1 = 0;
	UCB0CTL1 &= ~UCSWRST;

WriteMCP Routine

void writeMCP492x(uint16_t data,uint8_t ss) {
  // Take the top 4 bits of config and the top 4 valid bits (data is actually a 12 bit number)
  //and OR them together
  uint8_t top_msg = (0x30 & 0xF0) | (0x0F & (data >> 8));

  // Take the bottom octet of data
  uint8_t lower_msg = (data & 0x00FF);

  // Select our DAC, Active LOW
  SSOUT &= ~ss;

  // Send first 8 bits
  UCB0TXBUF = top_msg;
  while (UCBUSY & UCB0STAT);

  // Send second 8 bits
  UCB0TXBUF = lower_msg;
  while (UCBUSY & UCB0STAT);
  //Deselect DAC
  SSOUT |= ss;
}

To run faster we also set the internal clock to run at 16Mhz.

[clock lines]

CSCTL1 |= DCORSEL_6; //sets clockspeed

In the main loop after initilization, we create another loop to shine the laser directly at it's origin.
while(1){
    writeMCP492x(2048, SSX);
    writeMCP492x(2048, SSY);
}

Discussions