消えゆく世界と流れる未来に最後の灯を since 2006/4/3
#include <msp430.h>
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
// GPIO setup
P1DIR |= BIT0;
P1OUT &= ~BIT0;
P2DIR |= BIT0; // P2.0 output, P2.1 input (interruption)
P2OUT &= ~(BIT0 | BIT1);
P2IES &= ~BIT1; // Low to High
P2IE |= BIT1;
P2IFG &= BIT1;
PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high
// Clock setup
CSCTL3 |= SELREF__REFOCLK; // Set REFO as FLL reference source
CSCTL0 = 0; // clear DCO and MOD registers
CSCTL1 &= ~(DCORSEL_7); // Clear DCO frequency select bits first
CSCTL1 |= DCORSEL_3; // Set DCO = 8MHz
CSCTL2 = FLLD_0 + 243; // DCODIV = 8MHz
__delay_cycles(3);
__bic_SR_register(SCG0); // enable FLL
while (CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)); // Poll until FLL is locked
CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
// Configure UART
P1SEL0 |= BIT4 | BIT5; // set 2-UART pin as second function
UCA0CTLW0 |= UCSWRST;
UCA0CTLW0 |= UCSSEL__SMCLK;
UCA0BR0 = 52; // 52=8,000,000/16/9600 (9600 baud/s)
UCA0BR1 = 0x00;
UCA0MCTLW = 0x4900 | UCOS16 | UCBRF_1;
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE;
// Configure SPI
P1SEL0 |= BIT1 | BIT2 | BIT3;
UCB0CTLW0 |= UCSWRST; // **Put state machine in reset**
UCB0CTLW0 |= UCMST | UCSYNC | UCCKPL | UCMSB; // 3-pin, 8-bit SPI master, Clock polarity high, MSB
UCB0CTLW0 |= UCSSEL__SMCLK; // SMCLK
UCB0BR0 = 0x01; // /2,fBitClock = fBRCLK/(UCBRx+1).
UCB0BR1 = 0;
UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
// Sleep until interruption
__bis_SR_register(LPM3_bits | GIE); // Enter LPM3, interrupts enabled
}
// UART Received -> Be SPI Master -> Send SPI to Slave
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void) {
P2OUT |= BIT0;
P1OUT |= BIT0;
__delay_cycles(100); // Possibly optimized regarding board setup
UCB0CTLW0 |= UCSWRST; // **Put state machine in reset**
UCB0CTLW0 |= UCMST; // Master
UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
UCB0IE &= UCRXIE;
while (!(UCB0IFG & UCTXIFG));
UCB0TXBUF = UCA0RXBUF;
}
// SPI Received -> Send UART to PC
#pragma vector=USCI_B0_VECTOR
__interrupt void USCI_B0_ISR(void) {
while (!(UCA0IFG & UCTXIFG));
UCA0TXBUF = UCB0RXBUF;
P1OUT &= ~BIT0;
}
// Receive P2.1 L to H -> Be SPI Slave -> Wait SPI
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void) {
P2OUT &= ~BIT0;
UCB0CTLW0 |= UCSWRST; // **Put state machine in reset**
UCB0CTLW0 &= ~UCMST; // Slave
UCB0CTLW0 &= ~UCSWRST; // **Initialize USCI state machine**
UCB0IE |= UCRXIE;
P2IFG &= ~BIT1;
}