|
View:
New views
1 Messages
—
Rating Filter:
Alert me
|
|
|
running pwm on the msp430f2012Hi all,
I am new to the msp430, and I have looked at many examples, but I cannot seem to get the pwm to work. All of the rest seems to work fine, but this sticky point is really throwing me for a loop. Here is my setup: I have p1.0 & p1.1 as ADC10 Inputs. P1.5 is my pwm output, regulated by the values I read from p1.1. Here is the code: Any suggestions? /*************************************************************** Code explanation: The Led module has inputs and outputs to control when and howmuch the Led is lit. The inputs are: Automated dimming control,Manual dimming control/Brightness limiter, and the Motion Sensor trigger. The automated signal must be inverted, that is to say,0V at the input means maximum brightness, and max voltage means minimum brightness. The limiter sets the maximum brightness thatcan be output on the led. The output is a pwm, and the duty cycle controls the actual brightness of the Led. ***************************************************************/#include "msp430.h"#include "FPLed.h" /*************************************************************** ****** Global Variables ******* ***************************************************************/// Flags:unsigned char fgADCReady; // Conversion data readyunsigned char fgMotionTrigger; // 1 = motion detectedint Ticks;int Seconds;int Minutes;int Hours; int main(void){int AvgMaxDim;int AvgAutoDim;int AutoDimPct;int MaxDimPct;int PwmPct; // int DebounceMotion;int temp; // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; // Set background timer interrupt to every .5 mS on the watchdog timer. WDTCTL = WDT_MDLY_0_5; // Enable wd timer interrupt IE1 += WDTIE;/*************************************************************** ADC Channels: A0 - Dimming Max brightness A1 - Automatic Dimming Signal ***************************************************************/ fgADCReady = 0; // ADC data is not ready// DebounceMotion = 0; PwmPct = 0; AvgMaxDim = 0; AvgAutoDim = 0; ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10CTL1 = INCH_0 + CONSEQ_0; // input A0 & A1 ADC10AE0 = 0x01; // PA.0,1 Analog channel enable /*************************************************************** I/O pins: inputs: P1-2 - High = Automatic dimming control P1-6 - Motion Signal, Led on when high ***************************************************************//*************************************************************** PWM output: P2.1 - Led drive signal ***************************************************************/ // Set P1.0 as input, P1.5 as output, & P1. as A/D in P1DIR = 0x00C0; // Set inputs = 0, output = 1 P1OUT = 0; // Set outputs to zero P1SEL = 0x0020; // P1.5 is TA0 CCR0 = 33; // Set PWM period CCTL0 = OUTMOD_7; // Set CCR1 mode Set/Reset CCR1 = 17; // Set PWM duty cycle TACTL = TASSEL_2 + MC_1; // Use SMClk in up mode // Background operations // Read ADC0 for dimming limiter // Read ADC1 for Led control signal // Check inputs fgMotionTrigger = ((P1IN & 0x40) >> 6); // read to see if we detect motion // Enable interrupts ADC10CTL0 |= ADC10SC + ENC; __bis_SR_register(GIE); /*************************************************************** ***** AvgMaxDim & AvgAutoDim - What they are and what they do * ***** AvgMaxDim is the manual dimmer, and also the maximum * ***** the Automatic lighting controls can brighten the lights * ***** When the Automatic lighting control is greater than * ***** AvgMaxDim, the value of AvgAutoDim is replaced by the * ***** value of AvgMaxDim. * ***** AvgAutoDim is the value read from the Automatic light * ***** control circuitry. It is 0 for maximum, and 0x3ff for * ***** minimum light intensity. * ***************************************************************/ do { if(fgADCReady) { if(ADC10CTL1 == INCH_0) { ADC10MEM &= 0x3FF; temp = AvgMaxDim >> 4; AvgMaxDim -= temp; if(AvgMaxDim < 0) AvgMaxDim = 0; AvgMaxDim += (ADC10MEM >> 4); if((AvgMaxDim & 0x3ff) < ADC_NOISE_FLOOR) { MaxDimPct = 0; // LED off } else if((AvgMaxDim & 0x3ff) < TEN_PCT) { MaxDimPct = TEN_PCT << 1; // Five percent or so } else if((AvgMaxDim & 0x3ff) < TWENTY_PCT) { MaxDimPct = TEN_PCT; } else if((AvgMaxDim & 0x3ff) < THIRTY_PCT) { MaxDimPct = TWENTY_PCT; } else if((AvgMaxDim & 0x3ff) < FORTY_PCT) { MaxDimPct = THIRTY_PCT; } else if((AvgMaxDim & 0x3ff) < FIFTY_PCT) { MaxDimPct = FORTY_PCT; } else if((AvgMaxDim & 0x3ff) < SIXTY_PCT) { MaxDimPct = FIFTY_PCT; } else if((AvgMaxDim & 0x3ff) < SEVENTY_PCT) { MaxDimPct = SIXTY_PCT; } else if((AvgMaxDim & 0x3ff) < EIGHTY_PCT) { MaxDimPct = SEVENTY_PCT; } else if((AvgMaxDim & 0x3ff) < NINETY_PCT) { MaxDimPct = EIGHTY_PCT; } else if((AvgMaxDim & 0x3ff) < (HUNDRED_PCT - 20)) { MaxDimPct = NINETY_PCT; } else { MaxDimPct = HUNDRED_PCT; } ADC10CTL0 &= ~ENC; // clear encode so we can modify the channel ADC10CTL1 = INCH_1; ADC10AE0 = 0x02; // PA.0,1 Analog channel enable ADC10CTL0 |= ENC; } else if(ADC10CTL1 == INCH_1) { ADC10MEM &= 0x3FF; temp = AvgAutoDim >> 4; AvgAutoDim -= temp; if(AvgAutoDim < 0) AvgAutoDim = 0; AvgAutoDim += (ADC10MEM >> 4); if((AvgAutoDim & 0x3ff) < ADC_NOISE_FLOOR) { AutoDimPct = 0; // LED off } else if((AvgAutoDim & 0x3ff) < TEN_PCT) { AutoDimPct = TEN_PCT << 1; // Five percent or so } else if((AvgAutoDim & 0x3ff) < TWENTY_PCT) { AutoDimPct = TEN_PCT; } else if((AvgAutoDim & 0x3ff) < THIRTY_PCT) { AutoDimPct = TWENTY_PCT; } else if((AvgAutoDim & 0x3ff) < FORTY_PCT) { AutoDimPct = THIRTY_PCT; } else if((AvgAutoDim & 0x3ff) < FIFTY_PCT) { AutoDimPct = FORTY_PCT; } else if((AvgAutoDim & 0x3ff) < SIXTY_PCT) { AutoDimPct = FIFTY_PCT; } else if((AvgAutoDim & 0x3ff) < SEVENTY_PCT) { AutoDimPct = SIXTY_PCT; } else if((AvgAutoDim & 0x3ff) < EIGHTY_PCT) { AutoDimPct = SEVENTY_PCT; } else if((AvgAutoDim & 0x3ff) < NINETY_PCT) { AutoDimPct = EIGHTY_PCT; } else if((AvgAutoDim & 0x3ff) < (HUNDRED_PCT - 40)) { AutoDimPct = NINETY_PCT; } else { AutoDimPct = HUNDRED_PCT; } ADC10CTL0 &= ~ENC; ADC10CTL1 = INCH_0; ADC10AE0 = 0x01; // PA.0,1 Analog channel enable ADC10CTL0 |= ENC; } ADC10MEM = 0; fgADCReady = 0; // turn off ready flag ADC10CTL0 |= ADC10SC + ENC; // select the dimming percentage used for the pwm if(AutoDimPct <= TEN_PCT) { PwmPct = MaxDimPct; } else if(AutoDimPct > MaxDimPct) { PwmPct = MaxDimPct; } else { PwmPct = AutoDimPct; } CCR1 = PwmPct; // reset pwm value }/* // read inputs fgMotionTrigger = ((P1IN & 0x40) >> 6); // read to see if we detect motion if(fgMotionTrigger == 0) { DebounceMotion++; } else { DebounceMotion = 0; } if((DebounceMotion & 0x000f) == 0x000f) { DebounceMotion = 0x000f; }*/ } while (1); } #pragma vector=ADC10_VECTOR __interrupt void ADC_ISR(void){ fgADCReady = 1;} // Interval timer vector on the watchdog timer#pragma vector=WDT_VECTOR __interrupt void BackgroundTimer_ISR(void){ // increment our seconds counter if(Ticks < TICS2SECS) { Ticks++; } else if(Ticks == TICS2SECS) { Seconds++; Ticks = 0; } else { Ticks = 0; } if(Seconds >= SECS2MINS) { Minutes++; Seconds = 0;// P1OUT ^= 0x01; } if(Minutes >= MINS2HRS) { Hours++; Minutes = 0; }}Robert McPherson mailto:n7tzg@... _________________________________________________________________ Keep your kids safer online with Windows Live Family Safety. http://www.windowslive.com/family_safety/overview.html?ocid=TXT_TAGLM_WL_family_safety_072008 [Non-text portions of this message have been removed] |
| Free Forum Powered by Nabble | Forum Help |