Wednesday, July 25, 2012

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
#define SER(X) ((X)?(SER_PORT|=SER_BIT):(SER_PORT&=~SER_BIT))
void simpleTx(unsigned char data)
{
    unsigned int i;
    unsigned char buf = data;
    SER(0);                                    //Start Bit
    __delay_cycles(54);
    for(i=0;i<8;i++)
    {
          SER((buf&1));
           __delay_cycles(108);
           buf>>=1;
    }
    SER(1);                                    //Stop Bit
    __delay_cycles(108);
}
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

  while(1)
  {
    int i;
    P1OUT |= 0x01;                          // Set P1.0 LED on
    __delay_cycles(5000);                   // Delay
    P1OUT &= ~0x01;                         // Reset P1.0 LED off
    //SERIAL TX
    simpleTx('S');
    simpleTx('r');
    simpleTx('i');
    _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,

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to Boseji's Blog

Popular Post

Blogger templates

Welcome

Creation by Electronics Hobbyists from Bharat(India) to encourage & help people. This blog hosts the numerous innovations, discoveries and projects we provide.
Our Research Wing Adhar Labs
Contact Form | Email Address
We Support Open Source Hardware Definition v1.0
Creative Commons License
All works on this blog are licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at forbharat.wordpress.com and m8051.blogspot.com.

Permissions beyond the scope of this license may be available at http://forbharat.wordpress.com/permissions/.
Thank you for all your support.

© A.D.H.A.R Labs Research 2008-2013 . Powered by Blogger.

Follow by Email

Followers

PCB Unit Converter

mils
mm

- Copyright © Electronics for Bharat -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -