Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MSP430 SPI slave
#include "msp430g2553.h"
#include <legacymsp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1DIR = BIT0 + BIT6; // P1.0 and P1.6 are the red+green LEDs
P1OUT = BIT0 + BIT6; // All LEDs off
P1SEL = BIT1 + BIT2 + BIT4;
P1SEL2 = BIT1 + BIT2 + BIT4;
UCA0CTL1 = UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCCKPL + UCMSB + UCSYNC; // 3-pin, 8-bit SPI slave
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
__bis_SR_register(GIE); // Enter LPM4, enable interrupts
volatile unsigned int i = 0;
while(1) {
P1OUT ^= BIT6; // Toggle P1.6 output (green LED) using exclusive-OR
i = 50000; // Delay
do (i--);
while (i != 0);
}
return 0;
}
// Echo character
interrupt(USCIAB0RX_VECTOR) USCI0RX_ISR(void)
{
while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF;
P1OUT ^= BIT0; // Toggle P1.0 output (red LED) using exclusive-OR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment