Hi Dennis, I am trying to add a simple feature to this code but I am not able to get the proper signal. Would you please help me with it and walk me through why what I do does not work. What I want to do is to change one of the counter values insides the interrupt handler function using 4 elements in an array such that the counter takes on a new value each time for 4 times and the repeats. I declare the counter values below in the highlighted lines: #include "msp430F2012.h" #include #define DUTY_CYCLE_BASE 4 //50 for 16Mhz #define COMPARE_VOLTAGE_MV 3000 #define CALC_FACTOR (((uint32_t) COMPARE_VOLTAGE_MV) * DUTY_CYCLE_BASE) uint32_t adc_result; uint16_t voltage_mV; char i=0; //index value for counter char counter_value_array[4] = {10, 20, 30, 40}; //counter values char counter_value; then inside the interrupt handler function, I have added an if loop to select each element of the the counter_value_array: // Timer 0 A1 interrupt service routine #pragma vector = TIMER0_A1_VECTOR __interrupt void Timer0_A1_ISR( void ) { switch( TA0IV ) { case 10: // Overflow - TA0IFG { // Update new CCR value TA0CCR1 = new_ccr_value; } } static uint8_t counter = 80; //1ms static uint8_t X = 0; static uint8_t Y = 0; TA0CTL &= ~TAIFG; // Clear interrupt flag if ( !(--counter) ) { counter_value= counter_value_array [i]; if (++i == 4){ i = 0; } if (!X && !Y ) { //TA0CCR1 = 60; //1V P1OUT = 0X0C; //set P1.0 on, P1.1 low counter = 4; //P1.0 high for 1ms //Flag to go to +1 state X = 0; Y = 1; } else if (!X && Y ) { //TA0CCR1 = 30; P1OUT = 0X00; //set P1.0 on, P1.1 low counter = 2; //P1.0 high for 1ms //Flag to go to +1 state X = 1; Y = 0; } else if (X && !Y ) { //TA0CCR1 = 30; P1OUT = 0X05; //set P1.0 on, P1.1 low counter = 4; //P1.0 high for 1ms //Flag to go to +1 state X = 1; Y = 1; } else if (X && Y ) { //TA0CCR1 = 30; P1OUT = 0X00; //set P1.0 on, P1.1 low counter = counter_value; //Flag to go to +1 state X = 0; Y = 0; } } } After adding the highlighted lines, the PWM stops working. Would you please help me figure out how to resolve this issue. Thank you very much, Below is the complete code for your reference: #include "msp430F2012.h" #include #define DUTY_CYCLE_BASE 4 //50 for 16Mhz #define COMPARE_VOLTAGE_MV 3000 #define CALC_FACTOR (((uint32_t) COMPARE_VOLTAGE_MV) * DUTY_CYCLE_BASE) uint32_t adc_result; uint16_t voltage_mV; char i=0; //index value for counter char counter_value_array[4] = {10, 20, 30, 40}; //counter values char counter_value; volatile uint16_t new_ccr_value; void main( void ) { // Stop watchdog time WDTCTL = (WDTPW | WDTHOLD); // Set range to 16MHz BCSCTL1 = CALBC1_1MHZ; // Set DCO step and modulation to 16MHz DCOCTL = CALDCO_1MHZ; P1SEL |= 0x40; // Set special function of P1.6 to timer module P1DIR |= 0x4D; // Set P1.6, P1.0, P1.1 to output direction P1OUT |= 0x00; // Set P1.0 ON, P1.1 OFF // PWM frequency of 40kHz (25us) TA0CCR0 = 25; //400 for 16 Mhz // Start with duty cycle base TA0CCR1 = DUTY_CYCLE_BASE; // Reset/set mode TA0CCTL1 = OUTMOD_7; // SMCLK, divider 1, up-mode, clear, overflow interrupt enabled TA0CTL = (TASSEL_2 | ID_0 | MC_1 | TACLR | TAIE); // Reference is Vref+ and Vss, 16 ADC10 clock cycles S&H, reference on, ADC on ADC10CTL0 = (SREF_1 | ADC10SHT_2 | REFON | ADC10ON); // Select internal resistor divider ADC10CTL1 = INCH_11; // Enable conversions ADC10CTL0 |= ENC; // Enable global interrupts __bis_SR_register( GIE ); // Endless loop - main program while( 1 ) { // Start ADC conversion ADC10CTL0 |= ADC10SC; // Wait for conversion to be completed while( ADC10CTL1 & ADC10BUSY ); // Copy ADC result adc_result = ADC10MEM; // Calculate voltage at 1:1 resistor divider (half of the real value) // Left shift multiplies result by 2 to get supply voltage voltage_mV = (((adc_result * 1500) / 1023) << 1); // Calculate new CCR value new_ccr_value = (CALC_FACTOR / voltage_mV); } } // Timer 0 A1 interrupt service routine #pragma vector = TIMER0_A1_VECTOR __interrupt void Timer0_A1_ISR( void ) { switch( TA0IV ) { case 10: // Overflow - TA0IFG { // Update new CCR value TA0CCR1 = new_ccr_value; } } static uint8_t counter = 80; //1ms static uint8_t X = 0; static uint8_t Y = 0; TA0CTL &= ~TAIFG; // Clear interrupt flag if ( !(--counter) ) { counter_value= counter_value_array [i]; if (++i == 4){ i = 0;} if (!X && !Y ) { //TA0CCR1 = 60; //1V P1OUT = 0X0C; //set P1.0 on, P1.1 low counter = 4; //P1.0 high for 1ms //Flag to go to +1 state X = 0; Y = 1; } else if (!X && Y ) { //TA0CCR1 = 30; P1OUT = 0X00; //set P1.0 on, P1.1 low counter = 2; //P1.0 high for 1ms //Flag to go to +1 state X = 1; Y = 0; } else if (X && !Y ) { //TA0CCR1 = 30; P1OUT = 0X05; //set P1.0 on, P1.1 low counter = 4; //P1.0 high for 1ms //Flag to go to +1 state X = 1; Y = 1; } else if (X && Y ) { //TA0CCR1 = 30; P1OUT = 0X00; //set P1.0 on, P1.1 low counter = counter_value; //P1.0 high for 1ms //Flag to go to +1 state X = 0; Y = 0; } } }
↧