This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DTC , ADC10 and TIMER_A1 issue

hI,

I am trying to change the duty cycle of the Timer_A1 by reading ADC value using DTC.

But i found that if i assign the source address directly to TA1CCR1, then duty cycle is changing as can be seen by led brightness.

if(ADC_flag)
{
         ADC_flag=0;

         ADC10SA = (unsigned int)&TA1CCR1;               // Transfer adc readings to an array
         ADC10CTL0 |= ENC + ADC10SC;                      // Start sampling
         __delay_cycles (48);

        // sequence of conversions complete
        ADC10CTL0 &= ~ENC;

}

void Timer_Init(void)
{

P2DIR |= BIT1|BIT2;                                   //set as output pin

P2SEL |= BIT1|BIT2;                                 //pin selected for pwm operations (P2.1/TA1.1) and (P2.2/TA1.1)

TA1CTL |= TASSEL_2 + MC_1;              //SMCLK and up mode count till ccr register

TA1CCR0 = 1600 - 1;                               //pwm frequency 10 khz; DCO = MCLK = SMCLK = 16 Mhz

TA1CCR1 = Duty; 

TA1CCTL1 |= OUTMOD_7;                          //set/reset mode

}

 

But if i write the code as below no change in the duty cycle is observed. 

 

unsigned int ADC_Readings;   // global variable

unsigned int Duty = 100;           // global variable

if(ADC_flag)
{
     ADC_flag=0;

    ADC10SA = (unsigned int)&ADC_Readings; // Transfer adc readings to an array

    ADC10CTL0 |= ENC + ADC10SC; // Start sampling
    __delay_cycles (48);

    // sequence of conversions complete
    ADC10CTL0 &= ~ENC;

   Duty = ADC_Readings;

}

void Timer_Init(void)
{

P2DIR |= BIT1|BIT2;                                   //set as output pin

P2SEL |= BIT1|BIT2;                                 //pin selected for pwm operations (P2.1/TA1.1) and (P2.2/TA1.1)

TA1CTL |= TASSEL_2 + MC_1;              //SMCLK and up mode count till ccr register

TA1CCR0 = 1600 - 1;                               //pwm frequency 10 khz; DCO = MCLK = SMCLK = 16 Mhz

TA1CCR1 = Duty; 

TA1CCTL1 |= OUTMOD_7;                          //set/reset mode

}

Thank you..!!

  • When is Timer_Init() called? I'd guess the code never updates TA1CCR1 again.
  • The whole code what is written above is inside while(1) loop but timer_init() is called before while(1) loop.

    i.e.

    void main()
    {
    timer_init();

    while(1)
    {
    unsigned int ADC_Readings; // global variable

    unsigned int Duty = 100; // global variable

    if(ADC_flag)
    {
    ADC_flag=0;

    ADC10SA = (unsigned int)&ADC_Readings; // Transfer adc readings to an array

    ADC10CTL0 |= ENC + ADC10SC; // Start sampling
    __delay_cycles (48);

    // sequence of conversions complete
    ADC10CTL0 &= ~ENC;

    Duty = ADC_Readings;

    }

    void Timer_Init(void)
    {

    P2DIR |= BIT1|BIT2; //set as output pin

    P2SEL |= BIT1|BIT2; //pin selected for pwm operations (P2.1/TA1.1) and (P2.2/TA1.1)

    TA1CTL |= TASSEL_2 + MC_1; //SMCLK and up mode count till ccr register

    TA1CCR0 = 1600 - 1; //pwm frequency 10 khz; DCO = MCLK = SMCLK = 16 Mhz

    TA1CCR1 = Duty;

    TA1CCTL1 |= OUTMOD_7; //set/reset mode

    }
    }
    }

    But anyway variable "Duty" is defined as a global variable and i am changing the duty value as Duty = ADC_Readings;
    so, it should update TA1CCR1 register right..!!
    Can you please provide the solution for it.

    Thank you.!
  • The variable definition in the while loop hides the global variable.

    Anyway, the CCR1 register is updated only when the line "TA1CCR1 = Duty; " is executed.
  • Yes, i got ... i have to update TA1CCR1 because timer_init() is called only once.

    Thank you for the reply.!!

**Attention** This is a public forum