• Tag Archives Programming
  • MSP430: VLO and You!!

    So what is the VLO? The VLO is the very low- freq Oscillator, low power and low frequency(12KHz). So how does this change things, well now you can source your clocks from the DCO or the VLO instead of the 32Khz watch crystal. …….and now your asking yourself “Do I really need another clock source?” I say “yes!!”

    Now what can you do or how can i use the VLO? Its super easy to setup the VLO. In the Family user guide for the MSP430, Basic Clock Module+ will be your reference to set up the VLO.
    Registers that you will be concerned about are BCSCTL2, BCSCTL3 and the special functions registers.


    • BCSCTL3 needs LFXT1S_2 to be set

    BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO

    • BCSCTL2 needs to be set

    BCSCTL2 |= SELM_3 + SELS;                 // SMCLK  = MCLK = VLO = 12Khz

    • Clear OSC fault flag

    IFG1 &= ~OFIFG;                           // Clear OSCFault flag

    • Turn off DCO if not used

    _BIS_SR(SCG1 + SCG0);                // Stop DCO


    And that is all you need to do to setup your msp430 to use the VLO for SMCLK, AMCLK and MCLK @ 12KHz.  So now you need more control. so here are your VLO options

    BCSCTL1 –  controls XTS(not avalable on msp430x20xx), Divider for ACLK, and DCO range select.

    • DIVAx  – divide ACLK source by 1,2,4,8 or 00, 01, 10, 11(bit settings). Pretty much this will divide your Clock source by 1 – 8. This setting is just for ACLK which will be the crystal or VLO. (this only affects ACLK)
    • XTS, XT2OFF, and RSELx are not used for VLO but may be a good idea to read up on what they do.



    BCSCTL2  – controls MCLK divider, SMCLK divider MCLK and SMCLK clock source, each one is configurable.

    • SELMx – selects MCLK clock source, your choices are DCOCLK or LFXT1CLK/VLOCLK  just remember you cant use the VLO and the crystal
    • DIVMx – MCLK divider, divides the clock source by given amount, 1,2,4,8
    • SELS – select SMCLK source  DCOCLK or LFXT1CLK/VLOCLK
    • DIVSx – look another Divider, this one is for SMCLK
    • DCOR- not used – sets DCO resister to internal or external


    BCSCTL3 – XT and LFXT setting, only one setting is really used here.

    • LFXT1Sx – source of LFXT1,  00(0) for 32KHz crystal and  10(2) for VLO instead of the crystal
    • XT2Sx, XCAPx, XT2OF, LFXT1OF are not set or used for the VLO



    So now you have a basic understanding on how to setup your VLO, now let me give you a quick  VLO test


    /* VLO clock as main source instead of the DCO */
    #include  <msp430x20x3.h>
    void main(void)
    WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
    BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
    IFG1 &= ~OFIFG;                           // Clear OSCFault flag
    __bis_SR_register(SCG1 + SCG0);           // Stop DCO
    BCSCTL2 |= SELM_3;               // MCLK = LFXT1
    P1DIR |= 0x01;                            // Set P1.0 to output direction
    for (;;)
    volatile unsigned int i;
    P1OUT ^= 0x01;                          // Toggle P1.0 using exclusive-OR
    i = 50000;                              // Delay
    do (i--);
    while (i != 0);

    code for use with mspgcc


    Now that we have the basics down on the VLO, what are some of the uses for it? Really low power clock, or a 3rd clock source that’s a different frequency then the main, really slow ADC conversions, odd SDA16 filter frequency, a low frequency tone generator.


    What other uses can you find? Let me know in the comments 🙂

  • MSP430 RGB V0.1

    A different take on how to create a PWM signal. I decided to go the interrupt route using the WDT+ peripheral. I did this by setting it to the interval timer and putting all my counters and logic within the interrupt. The funny thing is  i have read that you shouldn’t put too much code into your interrupt, or something bad will happen.

    the resistors on the left, are 100 ohm resistors and the one on the right is a 470 ohm resistor to limit current for the RGB LED. connections on the uC that are used are pins 1.3, 1.4, 1.5 i have not decided which pins are what color, i will decide that on my final build.

    The test code will be improved this is just the compile and see if it works.- The best part , the code works lol .

    so let me know what you think on creating PWM with the interval timer instead of timer A.

    #include <msp430x20x2.h>
    #include <signal.h> //add for interrupt
    #define UP 0x00
    #define DOWN 0x01
    volatile  int millsecs = 0;
    volatile  int counter2 = 0;
    volatile  int led_red = 0;
    volatile  int led_green = 199;
    volatile  int led_blue = 199;
    volatile  int dir = UP;
    void main(void)
    WDTCTL = WDT_MDLY_0_064;
    P1DIR |= BIT5 + BIT4 + BIT3;
    P1OUT |= BIT5 + BIT4 + BIT3;
    IE1 |= WDTIE;
    }//end main
    interrupt(WDT_VECTOR) watchdog_timer(void)
    if (millsecs == 200) { millsecs= 0; P1OUT |= BIT0 + BIT6; ++counter2; }
    if (millsecs == led_red ) { P1OUT ^= BIT3; }
    if (millsecs == led_green) { P1OUT ^= BIT4; }
    if (millsecs == led_blue ) { P1OUT ^= BIT5; }
    if (counter2 == 25 ) { counter2 =0;
    switch (dir){
    case UP: ++led_red; --led_green; --led_blue;
    if (led_red == 199) {dir = DOWN;}
    case DOWN: --led_red; ++led_green; ++led_blue;
    if (led_red == 0) {dir = UP;}
    }//end switch
    }//end if
    }//end interrupt

    To change the Period length – change the 200 value to the period you want, you can also change it by setting the WDT interval to different intervals to change the period.
    to change the Duty cycle, change the led_[color] variable to anything between 0 and period length

    I am still working on trying to get it to cycle through all colors, and fade better when led_[color] is close to 0
    by far this project is not done, the board will be soldered up and pics will be presented. The end result should be code for WDT PWM, which could be used for servos, LEDs, or possibly communication, and i will have my flower pot back!

  • msp430- coding interrupts for mspgcc

    I am just an idiot, but thats my opinion. For those with MSPGCC compilers, we have a more difficult time finding code examples, and many of the code examples given either don’ explain very well (cryptic Coding, un-commented code) or they are for the CCS or IAR, and they dont use the same syntax as the MSPGCC compiler. So here is how to create an interrupt handler for non-PUC/POR interrupts.

    I will not go into interrupt vector masking, that is beyond me at this moment, but im not saying that i wont cover it later on, once i understand why you would want to mask it…..
    So lets start at what headers and other setup items you need before, creating the interrupt handler.
    First the signal.h has to be included into your code. #include<signal.h>
    this will give you access to the special function of
    interrupt(VECTOR ) service_routine (void) {/*interrupt code*/ }

    this is the same as #pragma vector=WDT_VECTOR
    __interrupt void watchdog_timer(void){ }

    These are the Defined vectors for interrupts right from the header files.(mspgcc headers)

    #define PORT1_VECTOR 4 /* 0xFFE4 Port 1 */
    #define PORT2_VECTOR 6 /* 0xFFE6 Port 2 */
    #define USI_VECTOR 8 /* 0xFFE8 USI */
    #define ADC10_VECTOR 10 /* 0xFFEA ADC10 */
    #define TIMERA1_VECTOR 16 /* 0xFFF0 Timer A CC1-2, TA */
    #define TIMERA0_VECTOR 18 /* 0xFFF2 Timer A CC0 */
    #define WDT_VECTOR 20 /* 0xFFF4 Watchdog Timer */
    #define NMI_VECTOR 28 /* 0xFFFC Non-maskable */

    all the interrupts should be self expainatory, vector = the source of the interrupt.

    since now we have all the basics we can now right a small program that uses interrupt, we will just create a small WDT interval timer.
    /*WDT interval timer- code based on msp430 examples*/
    #include<signal.h> //interrupt service routine
    #include <io.h> //usually included on msp430 header, but for sfr register access.
    void main(void) {
    WDTCTL = WDT_MDLY_32; //~30mS intervals
    P1DIR |=BIT1;
    IE1 |= WDTIE; //enable interrupt
    _BIS_SR(LPM0_bits + GIE); //not low power mode and enable interrupts
    }//end of main
    //interrupt service routine
    interrupt(WDT_VECTOR) watchdog_timer(void)
    P1OUT ^= BIT1;
    }//end of interrupt

    this should give you a good start on your Interrupts but there is still one thing that you may need. Changing the power modes when a interrupt is being serviced, the power mode will revert back to the power mode that it was in when the interrupt was called.
    There are 2 functions that we can use to clear or set power modes while in an interrupt.
    First one is to set the mode on exit of the routine, this is done by changing the copy of the status register that is saved to the stack. _BIS_SR_IRQ( ... )
    you would use this the same way you would use the _BIS_SR(…)

    The second one will clear the bits you select _BIC_SR_IRQ(...) same usage as the other, except it will just clear the bits not modify them.
    ***the use of _BIx_SR_IRQ() should only be used in an interrupt service request, the compiler will give you a warning but will produce the correct code if you use it anywhere else.***
    ****remember to enable Interrupts by using BIS_SR(GIE) or eint()****

    Edit 6-23-2011

    MSPGCC Uniarch branch of mspgcc has been released, It supports newer chips like the msp430G2453 (the newer 20pin DIPs) This is an initiative to unify the current branches of mspgcc. Interrupts for this version is slightly different. Once I test it or get confirmation from another user I will post the correct format for uniarch branch……but what would be better would be unify the branches so we don’t have so much confusion with these version discrepancies and nuances of the trees.

    As of right now uniarch is still being worked on and there and is not fully recommended unless you need support for the newer 20pin Dips (G2x53 G2x52).  Please don’t let my opinion dissuade your choice of compiler, mspgcc works great for me but uniarch may work better for you.


    Thank you Tissit for your Comment

    “In current gcc, you can (should) include msp430.c instead of the specific header and use the -m switches (in a Makefile) to tell the compiler which chip you’re using. It will find the right headers automatically. “

    If I forget something let me know and I will update

  • One Step Closer to Finishing My Eye

    ready to test
    ready to test

    My IR eye is finally built and is ready to test, and see if my eye really works or not, my first readings were pretty good, by using a TV remote i tested each photo transistor to make sure it was working correctly before i attached it to the servo.
    The whole assembly is partially made out of recycled materials, the x axis base is made from an old cd that i was going to toss, and the y axis mount was made from a shelf bracket (found it in one of my boxes), the main base is made out of an old cardboard box that was bound for the recycling.
    Finished connections
    Finished connections

    I will have a build page up as soon as i finish some of the code.
    With Video!

  • IR eye

    This project is the first step in my motion tracking. This Eye was built and adapted from an LRM post here .

    DSCN0219DSCN0221please dont laugh at my soldering job, its what i can manage with that kind of board, I like the ones that mimic a breadboard setup. With very little trouble i finished soldering in about 2 hours but had to go back a desolder one transistor and replace it and resolder one wire. After that i tested each IR sensor by themselves to make sure they all worked, each one gave a little different reading off the same IR source but that can be fixed with a little calibration in the software. But for now my goal is to hook it up to 2 servos and control the x and y for the motion tracking, eventually i would like to put this into an enclosure that looks like an eye.
    This is to make sure all 4 eyes are working
    const int sensepin0 = 0;
    const int sensepin1 = 1;
    const int sensepin2 = 2;
    const int sensepin3 = 3;
    int sensorValue0 = 0;
    int sensorValue1 = 0;
    int sensorValue2 = 0;
    int sensorValue3 = 0;
    void setup() {
    void loop() {
    sensorValue0 = analogRead(sensepin0);
    sensorValue1 = analogRead(sensepin1);
    sensorValue2 = analogRead(sensepin2);
    sensorValue3 = analogRead(sensepin3);
    Serial.print("sensor = ");
    Serial.print(" ");
    Serial.print(" ");
    Serial.print(" ");

    this will test all 4 sensors and print to the serial port.

    also this code is written for the arduino but can be adapted to other microprossecors.

    Here is a screenshot of the schematic as well

    ScreenshotNext i will mount it to the servo and start writing the logic for 1 of the axis, but my question is how to mount this to a servo with stuff wround the garage. I will answer this question soo.

    and this is why you should never place you MC on your keyboard…………..Screenshot-1that large stack to the left is about 30 print screen applications running. I placed my arduino on my print screen button for about 10 secs before I realized what was going on.


    • 4x 2n 2222 NPN transistors (generic NPN)
    • 4x 220 Ohm 1/4w resistors
    • 4x NPN photo transistors
    • a bunch of wire
    • Arduino

  • Hacking the Code

    Last Post i was talking about how to clean up a hacked web server, hopeful your server is clean now and you can get back to what you do best. But what was the hacker trying to do? What does that long string mean? I will tell you how to decode the string the safe way, and understand how it was constructed.

    The Example that I am going to use is from a real hacked website, the code that I will display is only partial but the construct will be complete. Any identifiable URLs will be changed to prevent identification and your safety.
    Lets start by identifying the code, this is what i had found at the very beginning of the page.
    <? /**/eval(base64_decode(aWYoZnVuY3Rpb25<–>KTt9fX0=)); ?>

    lets start with /**/, it looks innocent but its a way to hide the code, what the code usually used for is large blocks of comments instead of using // on each line you would start it with /* and  end with */ but what i noticed is that in bluefish and notepad++ when these are highlighted the whole line appears to be a comment, my guess is that other code scanners will look like one long comment  on that line and skip it. But this is just my conclusion and testing, very clever way to use comments.

    almost forgot the first thing to notice is the use of short tags(<?) that they use, on my personal server I have short tags disabled, I had learned that it should be disabled for security purposes, I don’t know if this holds true today. but the reason they use it is it helps hide it as a comment adding php to   it (<?php),makes it show up as php code not a comment, now its starting to unravel………

    evalEvaluate a string as PHP code

    very simple right, they need something to run there code, eval does this trick, you can even add html to the string. just another peice of the pie.

    base64_decode – it decodes base64 code, now what is base64? It was originally for email to send binary data in emails, and other uses like binary data in urls and other variables but newest use is hiding php code in a long string, the string was 2692 characters long. So what did that string have in it.

    Continue reading  Post ID 853

  • HalloweenCenter.net project

    I am Currently working on a Google Maps Integration For another Word Press Blog at Halloweencenter.net and hauntedmaps.com, Its Been a Fun project with some great challenges and a great learning experience,

    hopefully it will be completely finished sometime this week with no other issues at hand, but My personal Goal with this project is an easily integrated map with DB support for markers, directions and a unique way of administrating the inputted markers by the users. My idea for this to work is a Unique Link created when the site or marker was put into the DB, after that the person who created the the marker can now go to that link and update certain parts of the entry, like the description and other non critical Data. later this Week I will Post some of the Code and the Basics of how it works, and Hopeful create some reusable Code for your own project.

    but for right now i will be concentrating on getting my themes and my other WP content setup so it looks as Good as other WP sites

    I have to Give a Shout to Leo, he did give me some great inspiration to start up my own site..