#include "msp430x20x2.h" #define HEADER_BYTES 3 #define BITS_PER_BYTE 8 #define BYTES_PER_BLOCK 9 unsigned byte_number=0; unsigned bit_number=0; unsigned char byte_stream[] = "GO BEARS!"; int main( void ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; /* Setup Ports P1.7 : TX Enable Input P1.0 : TX Serial Data Output */ P1OUT = 0; P1SEL &= ~BIT0; P1SEL = BIT7; P1DIR = 0x7f; /* Clock Setup CPU clock: 16MHz ADC Clock: 16MHz/8 = 2MHz */ BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; BCSCTL2 = SELM_0+DIVM_0+DIVS_3; /* ADC setup Repeating Single-Channel Sampling */ ADC10CTL1 = INCH_7+ADC10DIV_7+CONSEQ_2+ADC10SSEL_3; ADC10CTL0 = SREF_0+ADC10SHT_3+ADC10ON+ADC10IE; /* ADC Starts */ ADC10CTL0 |= ENC+ADC10SC+MSC; /* Wait for interruption and transmit */ while(1){ _BIS_SR(GIE); }; } #pragma vector = ADC10_VECTOR __interrupt void adc_isr(void) { if (ADC10MEM > 512) /* TX is enabled */ { switch (byte_number){ case 0: /* Header-high byte 2-byte long */ /* 1 is always sent */ P1OUT |= BIT0; /* Point to the next bit */ bit_number ++; if (bit_number == (BITS_PER_BYTE<<1)){/* This is the last bit */ /* wrap the bit number */ bit_number = 0; /* point to the next byte */ byte_number += 2; } break; case 2: /* Header-low byte */ /* 0 is always sent */ P1OUT &= ~BIT0; /* Point to the next bit */ bit_number ++; if (bit_number == BITS_PER_BYTE){/* This is the last bit */ /* wrap the bit number */ bit_number = 0; /* point to the next byte */ byte_number ++; } break; default: /* Information byte */ /* Wrap the bit number if a byte has been finished */ bit_number = bit_number % BITS_PER_BYTE; /* Transmit bit stream */ P1OUT = ((byte_stream[byte_number-HEADER_BYTES]<<(BITS_PER_BYTE-bit_number-1))&0x0080)>>(BITS_PER_BYTE-1); /* Point to the next bit */ bit_number ++; if (bit_number == BITS_PER_BYTE){/* This is the last bit */ /* wrap the bit number */ bit_number = 0; /* point to the next byte */ byte_number ++; if (byte_number == HEADER_BYTES+BYTES_PER_BLOCK){/* This is the last byte */ /* wrap the byte number */ byte_number = 0; } } break; } } }