Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 21927

SPI Interrupts

$
0
0

I'm trying to write to a 16-bit DAC, which requires three bytes to be sent.  Right now, I use while loops in between the byte transfers to ensure completion, but I need to lower power consumption.  I'm trying to implement ISRs for them, but it does not work.  I enabled the TX interrupt, since the DAC does not transmit anything back.  The while loops are commented out.  When switching from sleep to while loops, the DAC is driven properly.  When using the sleep combined with the ISR, it does not work.

void initializeSPI_B0(void){
//*****************************************************************************
	/* USCI_B2 SPI Setup
	 * configure P1.5 as UCB2CLK
	 * configure P1.6 as UCB2SIMO
	 * configure P1.7 as \CS
	 */
	UCB0CTLW0 |= EUSCI_B_CTLW0_SWRST; // disable eUCSI_B0
	P1->SEL0 |= BIT6 + BIT5; // configure LSB of P1 MUX control signal
	P1->SEL1 &= ~(BIT6 + BIT5); // configure MSB of P1 MUX control signal
	P1->DIR |= BIT7 + BIT6 + BIT5; // configure 3 wire output with CS enable
	P1->OUT |= BIT7; // drive \CS high to disable SPI

	UCB0CTLW0 &= ~ (EUSCI_B_CTLW0_CKPL + // SCK must be low when \CS is pulled low
										 // clock state low when inactive
					EUSCI_B_CTLW0_SEVENBIT + // character length is 8 bits
					EUSCI_B_CTLW0_MODE_MASK); // 3-pin SPI

	UCB0CTLW0 |= 	EUSCI_B_CTLW0_CKPH + // data is captured on first edge
					EUSCI_B_CTLW0_MSB + // MSB first
					EUSCI_B_CTLW0_MST +  // SPI master mode
					EUSCI_B_CTLW0_SYNC + // synchronous mode
					EUSCI_B_CTLW0_UCSSEL_2; // SCK = SMCLK

	// no prescale due to f_max of LTC2602 = 50 MHz
	UCB0BRW = 0x00; // 48 MHz SCK

	UCB0IE &= ~EUSCI_B_IE_RXIE; // disable SPI interrupt
	UCB0IE |= EUSCI_B_IE_TXIE;
	UCB0IFG &= ~(EUSCI_B_IFG_TXIFG + EUSCI_B_IFG_RXIFG); // reset flags on startup
	NVIC->ISER[0] |= 1 << ((EUSCIB0_IRQn) & 31); // enable UART interrupt on NVIC level
	UCB0CTLW0 &= ~EUSCI_B_CTLW0_SWRST; // initialize eUCSI_B0 in SPI mode
//*****************************************************************************
}

// This function writes to DAC A using SPI to communicate with the LTC2602
void writeDAC16_A0(uint32_t value){
	P1->OUT &= ~BIT7; // drive \CS low to start SPI transmission
	UCB0TXBUF = (uint8_t) 0x30; // write to and update DAC A
	//while ((UCB0IFG & EUSCI_B_IFG_TXIFG) != EUSCI_B_IFG_TXIFG); // is tx buffer empty?
	__sleep();
	UCB0TXBUF = (value >> 8) & 0xFF; // transmit upper data byte
	//while ((UCB0IFG & EUSCI_B_IFG_TXIFG) != EUSCI_B_IFG_TXIFG); // is tx buffer empty?
	__sleep();
	UCB0TXBUF = value & 0xFF; // transmit lower data byte
	//while ((UCB0IFG & EUSCI_B_IFG_TXIFG) != EUSCI_B_IFG_TXIFG); // is tx buffer empty?
	__sleep();
	P1->OUT |= BIT7; // drive \CS high to stop SPI activity
}

void EUSCIB0_IRQHandler(void) {
	if ((UCB0IFG & EUSCI_B_IFG_TXIFG) == EUSCI_B_IFG_TXIFG) {
		P2->OUT |= BIT0;
		UCB0IFG &= ~EUSCI_B_IFG_TXIFG;
		SCB->SCR &= ~SCB_SCR_SLEEPONEXIT_Msk;
	}
	else {
		SCB->SCR |= SCB_SCR_SLEEPONEXIT_Msk;
	}

}


Viewing all articles
Browse latest Browse all 21927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>