In my project I need to control Led brightness.I am using MSP430G2432IN20 device.When one switch is pressed I have to increase the brightness.When another switch is pressed brightness has to be decreased.The increment and decrements of Led brightness should be step by step.How I can implement it.
#include <msp430.h> #define THRESHOLD0 0x0000 #define THRESHOLD1 0x0136 #define THRESHOLD2 0x026c #define THRESHOLD3 0x03a2 #define hystyrisis 0x005D #define TIM1 8 #define TIM2 62 #define VAR 1 int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; ADC10CTL1 = INCH_0; // Conversion code singed format, input A0 ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10AE0 |= 0x00; // P1.0 ADC option select // Set P1.0 LED on P2DIR |= 0x0F; // P1.0 output P2OUT |= 0x00; CCTL1 = CCIE + OUTMOD_7; // CCR1 interrupt enabled CCTL2 = CCIE + OUTMOD_7; // CCR2 interrupt enabled CCR1 = 40; //CCR2 = 30; CCR0 = 100; TACTL = TASSEL_2 + MC_1; // SMCLK, Contmode for (;;) { volatile unsigned long i; // volatile to prevent optimization ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit if((THRESHOLD3+0x003E) >= ADC10MEM && ADC10MEM > (THRESHOLD3-hystyrisis) & TACCR1>=TIM1) { CCR1 -= VAR; _delay_cycles(350000); } else if((THRESHOLD2+hystyrisis) >= ADC10MEM && ADC10MEM > (THRESHOLD2-hystyrisis) & TACCR1<=TIM2) { CCR1 += VAR; _delay_cycles(350000); } else if((THRESHOLD1+hystyrisis) >= ADC10MEM && ADC10MEM > (THRESHOLD1-hystyrisis) & TACCR2>TIM1) { CCR2 -= VAR; _delay_cycles(350000); } else if((THRESHOLD0+hystyrisis) >= ADC10MEM & TACCR2<TIM2) { CCR2 += VAR; _delay_cycles(350000); } else { _delay_cycles(0); } }} // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { _delay_cycles(1); __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } // Timer_A3 Interrupt Vector (TA0IV) handler #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer_A(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(TIMER0_A1_VECTOR))) Timer_A (void) #else #error Compiler not supported! #endif { switch( TA0IV ) { case 2: // CCR1 { P2OUT ^= 0x06; // Toggle P1.0 } break; case 4: { // P2OUT ^= 0x09; // Toggle P1. } break; case 10: break; // overflow not used }}