BDIs 0 Posted August 12, 2013 Share Posted August 12, 2013 I am working a simple code to allow the "brightness" of the onboard LED P1 tofollow a varying input voltage from 0-3.6v. I have the code to the the pointthat I can see the ADC10MEM register change state, I am hung up on translatingthe input to the output for the LED. any help would be greatly appreciated. Mycode below... #include <msp430.h> int main(void) { volatile unsigned int ADCval, pw, pi; WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer P1DIR |= 0x01; ADC10CTL0 = ADC10SHT_3 + ADC10ON; // Set sampling time, turn on ADC10 ADC10CTL1 = INCH_4 + ADC10DIV_7; //ADC10CTL0 |= ENC ; // Conversion enabled ADC10AE0 |= 0x04; // Enable interrupt for (; { ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start while (ADC10CTL1 & ADC10BUSY); // ADC10BUSY? while (!(ADC10CTL0 & ADC10IFG)); ADCval = ADC10MEM; pw = ADCval * 0x08; pi = 0x2200 - pw; TACCTL0 = pw; TACCTL1 = pi; } } Quote Link to post Share on other sites
RobG 1,892 Posted August 12, 2013 Share Posted August 12, 2013 You can use this as a starting point. Just change CCR0 and the way CCR1 is calculated from ADC10MEM and you are all set. For example: ... CCR0 = 1023; CCTL1 = OUTMOD_7; CCR1 = 0; ... __interrupt void ADC10_ISR(void) { CCR1 = ADC10MEM; } BDIs 1 Quote Link to post Share on other sites
BDIs 0 Posted August 13, 2013 Author Share Posted August 13, 2013 Rob, Thanks for your response. I have added to my code and seem to have moved forward a bit. I am unable to get the P1.0 to operate as expected, for some reason the P1.6 (green onboard LED) seems to work (unfortunately with a lot of flicker). I am not sure why the red LED (P1.0) is not responding. Any thoughts? #include <msp430g2553.h> /* * main.c */ int main(void) { unsigned int ADCval, pw, pi; //unsigned char is type of unsigned 8 bit variable. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer. ADC10CTL1 = INCH_4 + ADC10DIV_7; ADC10CTL0 = ADC10SHT_3 + ADC10ON; //Set sample and hold time, turn ADC On ADC10CTL0 |= ENC; ADC10AE0 |= 0X04; // Enable ADC for P1.4 while(1) // Infinite Loop { ADC10CTL0 |= ADC10SC; //Start conversion while (ADC10CTL1 & ADC10BUSY); while (!(ADC10CTL0 & ADC10IFG)); ADC10CTL0 &= ~(ADC10BUSY + ADC10IFG); //clear flags? ADCval = ADC10MEM; pw = ADCval * 8; pi = 0x2200 - pw; P1DIR |= BIT6; P1SEL |= BIT6; TACCR0 = pw; TACCTL1 = OUTMOD_3; TACCR1 = pi; TACTL = TASSEL_2 + MC_1; } } Quote Link to post Share on other sites
Tribes 10 Posted August 13, 2013 Share Posted August 13, 2013 I am not sure why the red LED (P1.0) is not responding. Any thoughts? P1DIR |= BIT6; P1SEL |= BIT6; Because in your code you only set P1.6 to output and activate the CCR output(in your case PWM signal) for it. Unfortunately P1.0 is not connected to a TimerA CCR, so you will have to toggle it manually using an interrupt routine to generate a PWM signal. Quote Link to post Share on other sites
BDIs 0 Posted August 13, 2013 Author Share Posted August 13, 2013 I had started with P1.0, with no luck so i went to P1.6 to see if that would work...I was trying to get confirmation that the main body of my code was ok. It appears that it is at least close as I can get close to the expected output on P1.6. It makes sense that i would not be getting the expected output on P1.0 if there is no TimerA connection to it. I am thinking a loop with the pw and pi variables turning on and off the P1.0 LED. Quote Link to post Share on other sites
Tribes 10 Posted August 14, 2013 Share Posted August 14, 2013 If you really want PWM on P1.0 I would rather generate the signal using interrupts generated by TimerA than a loop with delays. This is a much more elegant solution and allows you to use the low power modes of the MSP430. I am pretty sure you can find examples for software PWM on the forum GeekDoc 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.