Skip to content

Instantly share code, notes, and snippets.

@Robotonics Robotonics/main.c
Created Nov 17, 2012

Embed
What would you like to do?
uart debugging example for the msp430 launchpad using usci
/*
* Program that takes commands via Async Serial, which could be useful for debugging.
* LEDs are green when transmitting data, and red when receiving data.
*
* G2553 Pinout:
* 1.0 - red status led
* 1.1 - uart input
* 1.2 - uart output
* 1.6 - green status led
*
* On the MSP-EXP430G2 Rev 1.5 board "J3" jumpers for RXD and TXD *must* be horizontal!!
*
* No crystal is needed for this program!
*/
#include "main.h"
int rxbytes = 0; // total number of bytes received (view in debugger)
int txbytes = 0; // total number of bytes sent (view in debugger)
void main()
{
/* Clock Setup */
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Run at 1 MHz
DCOCTL = CALDCO_1MHZ; // Run at 1 MHz
/* USCI Setup */
P1SEL = BIT1 + BIT2; // Set pin modes to USCI_A0
P1SEL2 = BIT1 + BIT2; // Set pin modes to USCI_A0
P1DIR |= BIT2; // Set 1.2 to output
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1 MHz -> 9600
UCA0BR1 = 0; // 1 MHz -> 9600
UCA0MCTL = UCBRS1; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
/* LED Setup */
P1DIR |= (BIT0 | BIT6);
P1OUT &= ~(BIT0 | BIT6); // should be off already, but just be sure
/* Main program */
serial_log("Hello, master. Enter a command:\r\n");
__bis_SR_register(LPM0_bits + GIE);
}
/*
* Send a log message to to the serial terminal
*/
inline void serial_log(const char *str)
{
P1OUT |= BIT6; // green led
uint i;
uint len = strlen(str);
for (i = 0; i < len; i++) {
// wait for TXBUF to complete last send...
// UCA0TXIFG is high when UCA0TXBUF is empty
while (!(IFG2 & UCA0TXIFG));
UCA0TXBUF = str[i];
txbytes++;
}
P1OUT &= ~BIT6;
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR()
{
serial_log("Error! Unhandled interrupt ADC10_ISR!\r\n");
}
#pragma vector=COMPARATORA_VECTOR
__interrupt void COMPARATORA_ISR()
{
serial_log("Error! Unhandled interrupt COMPARATORA_ISR!\r\n");
}
#pragma vector=NMI_VECTOR
__interrupt void NMI_ISR()
{
serial_log("Error! Unhandled interrupt NMI_ISR!\r\n");
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR()
{
serial_log("Error! Unhandled interrupt PORT1_ISR!\r\n");
}
#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR()
{
serial_log("Error! Unhandled interrupt PORT2_ISR!\r\n");
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR()
{
serial_log("Error! Unhandled interrupt TIMER0_A0_ISR!\r\n");
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR()
{
serial_log("Error! Unhandled interrupt TIMER0_A1_ISR!\r\n");
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR()
{
serial_log("Error! Unhandled interrupt TIMER1_A0_ISR!\r\n");
}
#pragma vector=TIMER1_A1_VECTOR
__interrupt void TIMER1_A1_ISR()
{
serial_log("Error! Unhandled interrupt TIMER1_A1_ISR!\r\n");
}
/*
* Accept a command entered on the serial terminal
*/
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR()
{
#ifndef MAIN_H
#define MAIN_H
/* System headers */
#include <msp430.h>
#include <string.h>
/*
* Standard shortcuts preferred by humans
*/
typedef unsigned int uint;
#define TRUE 1
#define FALSE 0
// maximum length of a user-entered command
#define CMDLEN 12
/* Our global functions */
void serial_log(const char *str);
#endif /* MAIN_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.