MSP430 LaunchPad Tutorial - Bonus- UART Transmission

Here's a little bonus, as someone requested: I'll just give the code with some basic explanations, so you don't have to deeply understand it but just insert the snippet into your project.




It comes from TI's MSP430F20xx esamples and it has been readjusted to be used with the Launchpad.
This snippet is particularly useful if you need to send data from sensors to a serial bluetooth module.

#include  "msp430g2231.h"

#define     TXD                   BIT1         // TXD on P1.1

//   Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz
#define     Bitime_5              20       // ~ 0.5 bit length + small adjustment
#define     Bitime                52

unsigned char BitCnt;      //Bit Counter
unsigned int  TXByte;      //Data to send

void ConfigureTimerUart(void);
void Transmit(void);

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  BCSCTL1 = CALBC1_1MHZ;                    // Set clock range
  DCOCTL = CALDCO_1MHZ;
  P1SEL |= TXD;                             // Select I/O pins for UART
  P1DIR |= TXD;
  __enable_interrupt();                     

  while(1)
  {
      ConfigureTimerUart();
      TXByte = 0x4B;           //K
      Transmit();
  }
}

void ConfigureTimerUart(void)
{
    CCTL0 = OUT;                               // TXD Idle as Mark
    TACTL = TASSEL_2 + MC_2 + ID_3;            // SMCLK/8, continuous mode
}

// Function Transmits Character from TXByte
void Transmit()
{
  BitCnt = 0xA;                             // Load Bit counter, 8data + ST/SP
  while (CCR0 != TAR)                       // Prevent async capture
    CCR0 = TAR;                             // Current state of TA counter
  CCR0 += Bitime;                          // Some time till first bit
  TXByte |= 0x100;                          // Add mark stop bit to TXByte
  TXByte = TXByte << 1;               // Add space start bit
  CCTL0 =  CCIS0 + OUTMOD0 + CCIE;          // TXD = mark = idle
  while ( CCTL0 & CCIE );               // Wait for TX completion
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
    CCR0 += Bitime;                           // Add Offset to CCR0
    if (CCTL0 & CCIS0)                    // TX on CCI0B?
    {
      if ( BitCnt == 0)
        CCTL0 &= ~ CCIE;                  // All bits TXed, disable interrupt
      else
      {
        CCTL0 |=  OUTMOD2;                    // TX Space
        if (TXByte & 0x01)
        CCTL0 &= ~ OUTMOD2;               // TX Mark
        TXByte = TXByte >> 1;
        BitCnt --;
      }
    }
}

The program transmits the data stored in the TXByte variable on the serial line at 2400 baud.
It's pretty well commented, you shouldn't find so much trouble understanding it.

Instead of placing the transmission function in the main loop, you could update TXByte and transmit it in a port interrupt for example.

12 comments:

Anonymous said...

how can you convert a flot number into ascii ? because if you want to send numbers over UART communication to an hyperterminal you need that information in ASCII.

Thank You

Enrico Garante said...

I suggest not using hyperterminal but rather Realterm (download here http://realterm.sourceforge.net/), you can display data as ascii/hex/int etc...

Karla said...

I don't receive a K , I receive an " i" that is the same bits but inverted , why is that???

Enrico Garante said...

Check if your serial connection is ok, or if the terminal has some options setted

Anonymous said...

Hi Enrico, I have a problem with the communication between the pc and the msp...
I've tried with the realterm and it worked properly once, but now I recieve bad data, like "f8f8x" in the terminal, instead of "K". Which could be the reason? Thanks in advance.

Enrico Garante said...

If it worked before, maybe there is a problem with your serial cable. Try another one, or try using Putty http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Jason said...

I'm quite new to the MSP430, but in the first few lines, shouldn't BitCnt be an int and TxByte be a char?

Enrico Garante said...

BitCnt can be a char, it's only used to count how many bits we have left to transmit and anyway it starts from 0xA (10 decimal).
TxByte can also be a char, as an int it will only transmit the first 8 bits(plus start and stop) as setted in BitCnt.

Hari Nagarajan said...

hi man!
suppose i have a hexa value of '1DC'...how do i send it in the uart program??? its trvial but pls help me!!

Hari Nagarajan said...

what is the TXByte?what does it do? what should we add there?

Enrico Garante said...

To transmit more than 8 bits (0x1DC is 476 and it's over 8 bits) you should change BitCnt to 0x12 (18, 16 bits for data plus start/stop) and set TXByte=0x1DC (it's the data to send)

Anonymous said...

is it really necessary to solder the crystal oscillator to the board to use uart?

Post a Comment