Saturday, March 5, 2011

Temperature application code

So lets look back on what made the temperature application tick from the last post.

A series of define statements. All of the BIT# definitions come from the included msp430x20x2.h file included.
I couldn't fit the next bit of code into a screen shot so I'll have to copy paste it :
 while(1)
  {   
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);        // LPM0 with interrupts enabled
   
   
    /* Moving average filter out of 8 values to somewhat stabilize sampled ADC */
    tempMeasured[tempMeasuredPosition++] = ADC10MEM;
    if (tempMeasuredPosition == 8)
      tempMeasuredPosition = 0;
    tempAverage = 0;
    for (i = 0; i < 8; i++)
      tempAverage += tempMeasured[i];
    tempAverage >>= 3;                      // Divide by 8 to get average
   
    if ((--uartUpdateTimer == 0) || calibrateUpdate )
    {
      ConfigureTimerUart();
      if (calibrateUpdate)
      {
        TXByte = 248;                       // A character with high value, outside of temp range
        Transmit();
        calibrateUpdate = 0;
      }  
      TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );     
      Transmit();
      uartUpdateTimer = UART_UPDATE_INTERVAL;
      ConfigureTimerPwm();
    }
   
   
    tempDifference = tempAverage - tempCalibrated;
    if (tempDifference < -TEMP_THRESHOLD)
    {
      tempDifference = -tempDifference;
      tempPolarity = TEMP_COLD;
      LED_OUT &= ~ LED1;
    }
    else
    if (tempDifference > TEMP_THRESHOLD)
    {
      tempPolarity = TEMP_HOT;
      LED_OUT &= ~ LED0;
    }
    else
    {
      tempPolarity = TEMP_SAME;
      TACCTL0 &= ~CCIE;
      TACCTL1 &= ~CCIE;
      LED_OUT &= ~(LED0 + LED1);       
    }
   
    if (tempPolarity != TEMP_SAME)   
    {     
      tempDifference <<= 3;
      tempDifference += TIMER_PWM_OFFSET;     
      TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );
      TACCTL0 |= CCIE;
      TACCTL1 |= CCIE;     
    }  
  } 
}
What is above is the main while loop. (Hopefully the color selection doesn't make your eyes bleed).
What it does roughly is create and array :   tempMeasured[tempMeasuredPosition++] = ADC10MEM; the purpose of which is to hold previous temperatures that it measured it seems. From there it is just a matter of looping through and getting an average of the temperature change. I'm not too sure what lots of the gritty details do, but overall it simply checks to see if the temperature is more than the TEMP_THRESHOLD or less than it. If it is increasing it lights up the LED.

Its really straight forward but lots of the bit operations hold me up.That and not knowing what every variable and function is, and what they do. Going to have to go over some bit wise operations again pretty soon.

18 comments:

  1. Wow too bad i odnt understand it...

    ReplyDelete
  2. I'm a bit stupid, is that Python? :P

    ReplyDelete
  3. Im an unpracticed programmer, what language is this? C+?

    ReplyDelete
  4. u'll get hang on all the variables soon, there arent that many that actually work

    ReplyDelete
  5. its C
    and i understand it perfectly

    ReplyDelete
  6. couldn't understand anything, but interesting post

    ReplyDelete
  7. Yah, Im with many above me, clueless. But very cool. I remember them trying to teach us some rough code in HS, but that was sooo long ago and likely outdated.

    ReplyDelete
  8. Tony is right variables are something that you eventually get to hang out of after much trial and error...

    ReplyDelete
  9. A million kids in their parents basement just giggled.

    ReplyDelete
  10. wouw i feel such a geek that i understand this

    ReplyDelete
  11. grrr, code >=|
    Having a very, very basic understanding of code sucks sometimes.

    ReplyDelete
  12. seems pretty straight forward on what it does. Nice coding :P

    ReplyDelete
  13. Need a code to make women understandable...

    ReplyDelete
  14. ah C 1 of 2 reasons I no longer want to program

    ReplyDelete
  15. Haha, will look into this, don't really get variables yet.

    ReplyDelete
  16. Looks pretty intricate. I know a few languages, but C and C based languages, I've yet to start learning.

    ReplyDelete