//*****************************************************************************
//
// File Name : 'pumpkin_light.c'
// Title : Pumpkin light controller on WDS board
// Author : Greg Peek
// Created : 10/2810
// Revised :
// Version : 0
// Target MCU : TI MSP430G2231
// Target board : TI Launchpad
//
//*****************************************************************************
// code ported from AVR to MSP430
// original code attribution:
/*==============================================
sleeplight.c
Apple-style LED sleep light simulator.
Written by Windell Oskay, http://www.evilmadscientist.com/
Copyright 2009 Windell H. Oskay
Distributed under the terms of the GNU General Public License, please see below.
An avr-gcc program for the Atmel ATTiny2313
Version 1.0 Last Modified: 10/20/2010.
Changes:
modifications for porting from AVR to MSP430
software PWM replaces hardware to enable any pin(s) to be used
control of LED pattern moved to variables to enable easier experimentation in debugger
==============================================*/
#include "msp430g2231.h"
#define shortdelay() asm("nop")
#define PINS 1<<7| 1<<6 // P1.7 and P1.6 output
void pwm(unsigned int width, unsigned int length, char on, char off)
{
unsigned int i;
P1OUT = off;
for (i=width; i<length; i++)
shortdelay();
P1OUT = on;
for (i=0; i<width; i++)
shortdelay();
// note we leave PWM with LED ON outside of this loop, for maximum brightness
// LED will never be totally off thanks to main loop overhead
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WatchDog Timer
P1DIR |= PINS; // set output pins
P1OUT |= 0x00;
// The following are the variables to change the LED pattern
// note everything is in variables to enable experimenting with pattern in debugger without recompiling.
// Get the effect you like, then change these constants and do final recompile
unsigned int plateau = 20; // number of steps at max brightness
unsigned int shift = 8; // normalizing factor after squaring
unsigned int stepmax = 256; // number of steps between min and max brightness
unsigned int length = 255; // width of PWM cycle
unsigned int inhale = 10; // number of PWM loops in a step during rising
unsigned int exhale = 12; // number of PWM loops in a step during falling
unsigned int floor = 0; // increases min brightness
char on = PINS; // P1OUT value for LED on
char off = 0x00; // P1OUT value for LED off
// true variables
unsigned int step = 0; // control loop variable
char phase = 1; // rising or falling brightness
unsigned int PWMon = 0; // current LED PWM on time, calculated from step
while (1) { // start of main loop
step++;
if (step < stepmax) {
if (phase)
PWMon = step;
else
PWMon = stepmax - step;
}
else if (step < (stepmax + plateau))
{
if (phase)
PWMon = stepmax;
else
PWMon = 0;
}
else {
step = 0;
if (phase)
phase = 0;
else
phase = 1;
}
if (PWMon > 255) {
PWMon = 255;
}
PWMon = ((PWMon * PWMon) >> shift) + floor;
// delay for timing:
if (phase) {
unsigned int j = 0;
while (j < inhale)
{
pwm(PWMon,length, on, off);
j++;
}
}
else {
unsigned int j = 0;
while (j < exhale)
{
pwm(PWMon,length, on, off);
j++;
}
}
} // end while(1)
}