This blog has permanently moved to

Contact Form | Email Address

© 2018 All rights reserved by Boseji

Thank you for all your support.

Wednesday, July 25, 2012

Easy Serial Transmitter on MSP430G Series

We have tried out a few experiments on implementing Software Serial communication in MSP430G series microcontrollers. Trying to make the easiest of the code we implemented a blocking serial sender on MSP430. This can be used even in the smallest of the MSP430 and easily send out information that can be logged into a PC serial terminal.

For use with low frequency of the controller we need some thing that could use less number of cycles but still be fast enough with widest error tolerance in terms of baud rate. We found that 9600 BAUD was the best to use since +/-5% tolerance is good enough. In the MSP430G series microcontrollers the default frequency setting 1s about 1MHz even with or without 32.768MHz crystal as confirmed from datasheet. So we took this as the base.
Here is the Math workout:

MCU Clock Frequency = 1MHz
Serial BAUD rate = 9600BAUD = 9600Hz

Bit Time (@ 9600) = 104.16667 uS(Micro Seconds)
Now for the CPU to generate 9600 BAUD using the Toggle of Pins = 1MHz*104.1667uS

Then Count for NOPs for Bit Time = 104 Counts (Approx)
We know that Start Bit is Half Bit time , So Start Bit Time = 54 Counts

Here is a program that uses P1.1 of the MSP430 Launchpad board:

#include <msp430g2231.h>

#define SER_PORT P1OUT
#define SER_BIT     BIT1
void simpleTx(unsigned char data)
    unsigned int i;
    unsigned char buf = data;
    SER(0);                                    //Start Bit
    SER(1);                                    //Stop Bit
void main(void)
  BCSCTL1 |= DIVA_1;                        // ACLK/2
  WDTCTL = WDT_ADLY_1000;                   // WDT 1s/2 interval timer
  IE1 |= WDTIE;                             // Enable WDT interrupt
  P1DIR = 0xFF;                             // All P1.x outputs
  P1OUT = 0;                                // All P1.x reset
  P2DIR = 0xFF;                             // All P2.x outputs
  P2OUT = 0;                                // All P2.x reset

    int i;
    P1OUT |= 0x01;                          // Set P1.0 LED on
    __delay_cycles(5000);                   // Delay
    P1OUT &= ~0x01;                         // Reset P1.0 LED off
    _BIS_SR(LPM3_bits + GIE);               // Enter LPM3

#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer (void)
    _BIC_SR_IRQ(LPM3_bits);                 // Clear LPM3 bits from 0(SR)

This program would print the string 'Sri' on the terminal every 2 Seconds and also Pulse an LED on P1.0.
Although this program uses the software delay loops and are power hungry but are easy to use tools for debugging some code bugs if needed. Considering the size of the code need its only a few lines. And it gives the default 9600BAUD serial so basic binary coded messages can still be sent at various parts of the program where time critically is low.

We though this might be a handy tool that can easily be used in any micro to perform debugging even if you dont have an exclusive UART available. We would be trying to extend it beyond this and try to see if we can further reduce the cycles and make it faster, may be some thing like 1-Wire Debugging that Freescale controllers come built in with. Let us know your suggestions and ideas on how we can make this better,

No comments:

Post a Comment