software uart

Any one can explain this code…

#include<msp430x14x.h>

#include<stdio.h>

#define Bitime 55 //8Mhz/19200=104

#define RXD 0x04 // RXD on P2.3

#define TXD 0x02

#define DELTA 256 //TXD on P1.2

unsigned int RXTXData;

unsigned char BitCnt;

unsigned char i = 0;

void TX_Byte(void);

void Set_DCO(void);

static const char string = { “Hello World\r\n” };

void main()

{

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

BCSCTL1 |= DIVA_3; // ACLK = LFXT1CLK/8

TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear

Set_DCO(); // Set DCO

WDTCTL = WDT_MDLY_32; // WDT 16ms*8, ACLK, interval timer

IE1 |= WDTIE; // Enable WDT interrupt

CCTL0 = OUT; // TXD Idle as Mark

P1SEL = TXD; // P1.1/TA0 for TXD function

P1DIR = TXD; // TXD output on P1

P2SEL = RXD; // P2.2/TA0 as RXD input

P1DIR |= 0x08; // Power MAX3221

P1OUT |= 0x08; //

i=0;

for (;:wink:

{

_BIS_SR(LPM0_bits + GIE); // Enter LPM3

RXTXData = string*;*
TX_Byte(); // TX
i++;
if (i > sizeof string-1) i = 0;
}
}
void TX_Byte (void)
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
// printf(“%d”,RXTXData);
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL0 & CCIE ); // Wait for TX completion
}
void Set_DCO(void)
{
unsigned int Compare, Oldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
while (1)
{
while (!(CCIFG & CCTL2)); // Wait until capture occured
CCTL2 &= ~CCIFG; // Capture occured, clear flag
Compare = CCR2; // Get current captured SMCLK
Compare = Compare - Oldcapture; // SMCLK difference
Oldcapture = CCR2; // Save current captured SMCLK
if (DELTA == Compare) break; // If equal, leave “while(1)”
else if (DELTA < Compare) // DCO is too fast, slow it down
{
DCOCTL–;
if (DCOCTL == 0xFF)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3)))
BCSCTL1–; // Did DCO roll under?, Sel lower RSEL
}
}
else
{
DCOCTL++;
if (DCOCTL == 0x00)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x0F)))
BCSCTL1++; // Did DCO roll over? Sel higher RSEL
}
}
}
CCTL2 = 0; // Stop CCR2
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
CCR0 += Bitime; // Add Offset to CCR0
// TX
if ( BitCnt == 0)
CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt
else
{
CCTL0 |= OUTMOD2; // TX Space
if (RXTXData & 0x01)
CCTL0 &= ~ OUTMOD2; // TX Mark
RXTXData = RXTXData >> 1;
BitCnt --;
}
}
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer (void)
{
_BIC_SR_IRQ(LPM0_bits); // Clear LPM3 bits from 0(SR)
}

The code isn’t readable. Use the code tags.

Leon