http://www.ti.com/lsds/ti/microcontroller/16-bit_msp430/msp430_software_landing.page
/******************************************************************************
* MSP430G2553 - USCI_A0, 9600 UART Echo ISR, DCO SMCLK
*
* Description: Echo a received character, RX ISR used. Normal mode is LPM0.
* USCI_A0 RX interrupt triggers TX Echo.
* Baud rate divider with 1MHz = 1MHz/9600 = ~104.2
* ACLK = n/a, MCLK = SMCLK = CALxxx_1MHZ = 1MHz
*
* MSP430G2553
* -----------------
* /|\| XIN|-
* | | |
* --|RST XOUT|-
* | |
* | P1.2/UCA0TXD|------------>
* | | 9600 - 8N1
* | P1.1/UCA0RXD|<------------
*
*******************************************************************************/
/*****************************************************************
* DEFINE
******************************************************************/
#include "msp430g2553.h"
#define BITIME (13 * 4) // 125 KHz /(13*4) = 2404 bits/sec
unsigned char bitCnt; // number of bits to transmit
unsigned int txByte; // transmit buffer with start/stop bits
/*****************************************************************
* GLOBAL
******************************************************************/
/*****************************************************************
* INTERRUPT
******************************************************************/
/*****************************************************************
* PROTOTYPE
******************************************************************/
void uart_putstr(char *str); // transmit string
void uart_transmit(unsigned char ch); // transmit a character
void led(void);//led signal
/****************************************************************
* MAIN
****************************************************************/
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
P1DIR |= BIT0 + BIT6;//Output direction LED1, LED2
P1DIR &= ~BIT3;//Input direction Push button
P1REN |= BIT3;
P1OUT |= BIT3;
//UART Config
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
while(1)
{
led();
uart_putstr("Hello world.\n\r");
if ((P1IN & BIT3) == 0)
{
uart_putstr("Button \n");
}
}
}
/*****************************************************************
* LED Signal
******************************************************************/
void led(void)
{
P1OUT |= BIT0;
P1OUT &= ~BIT6;
_delay_cycles(10000);
P1OUT &= ~BIT0;
P1OUT |= BIT6;
_delay_cycles(10000);
}
/*****************************************************************
* UART Timer_A interrupt
******************************************************************/
void timer_A_ISR(void)
{
CCR0 += BITIME; // Schedule next interrupt
if (bitCnt == 0)
{
CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
}
else
{
if (txByte & 0x02)
{
CCTL0 &= ~OUTMOD2; // TX Mark
}
else
{
CCTL0 |= OUTMOD2; // TX Space
}
txByte = txByte >> 1;
bitCnt--;
}
}
/*****************************************************************
* UART Transmit character
******************************************************************/
void uart_transmit(unsigned char ch)
{
bitCnt = 0xA; // Load Bit counter, 8data + ST/SP
txByte = (unsigned int)ch | 0x100; // Add mark stop bit to txByte
txByte = txByte << 1; // Add space start bit
CCR0 = TAR + BITIME; // Some time till first bit
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while (CCTL0 & CCIE); // Wait for ISR to complete TX
}
/*****************************************************************
* UART Transmit put string
******************************************************************/
void uart_putstr(char *str)
{
char *cp;
for (cp = str; *cp != '\0'; cp++)
{
uart_transmit(cp[0]);
}
}
/*****************************************************************
* UART receive character
******************************************************************/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
if (UCA0RXBUF == 'b') // 'b' received? <-> BIG just turn on LED1
{
IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
P1OUT &= ~BIT6;
P1OUT |= BIT0;
}
else
{
led();
}
if (UCA0RXBUF == 's') // 's' received? <-> SMALL just turn on LED2
{
IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
P1OUT &= ~BIT0;
P1OUT |= BIT6;
}
else
{
led();
}
}
/*****************************************************************
* END OF main.c
******************************************************************/
private void butA_KeyPress(object sender, KeyPressEventArgs e)
{
/*if (e.KeyChar == 'h')
{
butA.Focus();
butA.PerformClick();
}*/
int h = Com.ReadByte();
if (h == 'h')
{
butA.Focus();
butA.PerformClick();
}
}