Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //*****************************************************************************
  2. //
  3. // File Name    : 'pumpkin_light.c'
  4. // Title        : Pumpkin light controller on WDS board
  5. // Author   : Greg Peek
  6. // Created  : 10/2810
  7. // Revised  :
  8. // Version  : 0
  9. // Target MCU   : TI MSP430G2231
  10. // Target board : TI Launchpad
  11. //
  12. //*****************************************************************************
  13.  
  14.  
  15. // code ported from AVR to MSP430
  16. // original code attribution:
  17. /*==============================================
  18. sleeplight.c
  19. Apple-style LED sleep light simulator.
  20. Written by Windell Oskay, http://www.evilmadscientist.com/
  21.  Copyright 2009 Windell H. Oskay
  22.  Distributed under the terms of the GNU General Public License, please see below.
  23.  An avr-gcc program for the Atmel ATTiny2313  
  24.  Version 1.0   Last Modified:  10/20/2010.  
  25.  
  26.  
  27. Changes:
  28.   modifications for porting from AVR to MSP430
  29.   software PWM replaces hardware to enable any pin(s) to be used
  30.   control of LED pattern moved to variables to enable easier experimentation in debugger
  31. ==============================================*/
  32.  
  33.  
  34. #include  "msp430g2231.h"
  35.  
  36. #define shortdelay()    asm("nop")
  37. #define PINS            1<<7| 1<<6    // P1.7 and P1.6 output
  38.  
  39.  
  40. void pwm(unsigned int width, unsigned int length, char on, char off)
  41. {
  42.         unsigned int i;
  43.  
  44.         P1OUT = off;
  45.         for (i=width; i<length; i++)
  46.           shortdelay();
  47.  
  48.         P1OUT = on;
  49.         for (i=0; i<width; i++)
  50.           shortdelay();
  51.        
  52.         // note we leave PWM with LED ON outside of this loop, for maximum brightness
  53.         // LED will never be totally off thanks to main loop overhead
  54. }
  55.  
  56. void main(void)
  57. {
  58.         WDTCTL = WDTPW + WDTHOLD;           // Stop WatchDog Timer
  59.        
  60.         P1DIR |= PINS;                      // set output pins
  61.         P1OUT |= 0x00;
  62.  
  63.        
  64.         // The following are the variables to change the LED pattern
  65.         // note everything is in variables to enable experimenting with pattern in debugger without recompiling.
  66.         //    Get the effect you like, then change these constants and do final recompile
  67.     unsigned int plateau = 20;          // number of steps at max brightness
  68.     unsigned int shift = 8;             // normalizing factor after squaring
  69.     unsigned int stepmax = 256;         // number of steps between min and max brightness
  70.         unsigned int length = 255;          // width of PWM cycle
  71.         unsigned int inhale = 10;           // number of PWM loops in a step during rising
  72.         unsigned int exhale = 12;           // number of PWM loops in a step during falling
  73.         unsigned int floor = 0;             // increases min brightness
  74.         char on = PINS;                     // P1OUT value for LED on
  75.         char off = 0x00;                    // P1OUT value for LED off
  76.    
  77.         // true variables
  78.     unsigned int step = 0;              // control loop variable
  79.         char phase = 1;                     // rising or falling brightness
  80.     unsigned int PWMon = 0;             // current LED PWM on time, calculated from step       
  81.    
  82.     while (1) { // start of main loop
  83.  
  84.     step++;
  85.    
  86.     if (step < stepmax) {
  87.        
  88.         if (phase)
  89.             PWMon = step;
  90.         else
  91.             PWMon = stepmax - step;
  92.        
  93.     }
  94.     else if (step < (stepmax + plateau))
  95.     {
  96.         if (phase)
  97.             PWMon = stepmax;
  98.         else
  99.             PWMon = 0;
  100.     }
  101.    
  102.     else {
  103.         step = 0;
  104.        
  105.         if (phase)
  106.             phase = 0;
  107.         else
  108.             phase = 1;
  109.        
  110.     }
  111.    
  112.     if (PWMon > 255) {
  113.         PWMon = 255;
  114.     }
  115.     PWMon = ((PWMon * PWMon) >> shift) + floor;
  116.          
  117.    
  118.    
  119.     // delay for timing:
  120.    
  121.     if (phase) {
  122.         unsigned int j = 0;
  123.         while (j < inhale)
  124.         {
  125.             pwm(PWMon,length, on, off);
  126.             j++;       
  127.         }      
  128.     }
  129.     else {
  130.         unsigned int j = 0;
  131.         while (j < exhale)
  132.         {
  133.             pwm(PWMon,length, on, off);
  134.             j++;       
  135.         }
  136.     }
  137.  
  138.         } // end  while(1)     
  139.  
  140. }