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 finda 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.



2 commenti:

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...

Post a Comment