This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Generating variable frequency in LPC11xx

Hi all!!

How to generate variable frequency in lpc11xx controller, currently i'm getting
fixed frequency on pin 27[CT16B0_MAT0] of 96.2hz, but I need to generate freq from
6hz to 20hz.

Below is the part of the code which I have compared:

void init_timer16(uint8_t timer_num, uint32_t TimerInterval)
{
  if ( timer_num == 0 )
  {

    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
    LPC_IOCON->PIO0_2           &= ~0x07;        /*  Timer0_16 I/O config */
    LPC_IOCON->PIO0_2           |= 0x02;             /* Timer0_16 CAP0 */
    LPC_IOCON->PIO0_8           &= ~0x07;
    LPC_IOCON->PIO0_8           |= 0x02;             /* Timer0_16 MAT0 */
    LPC_IOCON->PIO0_9           &= ~0x07;
    LPC_IOCON->PIO0_9           |= 0x02;             /* Timer0_16 MAT1 */
#ifdef __JTAG_DISABLED
    LPC_IOCON->JTAG_TCK_PIO0_10 &= ~0x07;
    LPC_IOCON->JTAG_TCK_PIO0_10 |= 0x03;             /* Timer0_16 MAT2 */
#endif

    timer16_0_counter = 0;
    LPC_TMR16B0->MR0 = TimerInterval;

    LPC_TMR16B0->MCR = 3;                            /* Interrupt and Reset on MR0 */

    /* Enable the TIMER0 Interrupt */
    NVIC_EnableIRQ(TIMER_16_0_IRQn);
  }
  else if ( timer_num == 1 )
  {

    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
    LPC_IOCON->PIO1_8           &= ~0x07;        /*  Timer1_16 I/O config */
    LPC_IOCON->PIO1_8           |= 0x01;             /* Timer1_16 CAP0 */
    LPC_IOCON->PIO1_9           &= ~0x07;
    LPC_IOCON->PIO1_9           |= 0x01;             /* Timer1_16 MAT0 */
    LPC_IOCON->PIO1_10          &= ~0x07;
    LPC_IOCON->PIO1_10          |= 0x02;             /* Timer1_16 MAT1 */

    timer16_1_counter = 0;
    LPC_TMR16B1->MR0 = TimerInterval;
    LPC_TMR16B1->MCR = 3;                            /* Interrupt and Reset on MR1 */

    /* Enable the TIMER1 Interrupt */
    NVIC_EnableIRQ(TIMER_16_1_IRQn);
  }
  return;
}

Kindly suggest how to process further.

Thank you.

Parents
  • /******************************************************************************
    ** Function name:               init_timer16PWM
    **
    ** Descriptions:                Initialize timer as PWM
    **
    ** parameters:                  timer number, period and match enable:
    **                                              match_enable[0] = PWM for MAT0
    **                                              match_enable[1] = PWM for MAT1
    **                                              match_enable[2] = PWM for MAT2
    **
    ** Returned value:      None
    **
    ******************************************************************************/
    void init_timer16PWM(uint8_t timer_num, uint32_t period, uint8_t match_enable, uint8_t cap_enabled)
    {
      disable_timer16(timer_num);
    
      if (timer_num == 1)
      {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
    
        /* Setup the external match register */
    //    LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
                    LPC_TMR16B0->EMR = (1<<0)|(1<<1)|(1<<3)|(1<<5) | (1<<7) | (1<<10) |(match_enable) ;
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        set_timer16_match(timer_num, match_enable, 0 );
    
        /* Enable the selected PWMs and enable Match3 */
        LPC_TMR16B1->PWMC = (1<<3)|(match_enable);
    
        /* Setup the match registers */
        /* set the period value to a global variable */
        timer16_1_period    = period;
        LPC_TMR16B1->MR3         = timer16_1_period;
        LPC_TMR16B1->MR0 = timer16_1_period/2;
        LPC_TMR16B1->MR1 = timer16_1_period/2;
        LPC_TMR16B1->MR2 = timer16_1_period/2;
    
        /* Set match control register */
        LPC_TMR16B1->MCR = 1<<10;// | 1<<9;                          /* Reset on MR3 */
    
        if (cap_enabled)
        {
              /* Use location 0 for test. */
                set_timer16_capture( timer_num, 0 );
          LPC_TMR16B1->IR = 0xF;                                                 /* clear interrupt flag */
    
          /* Set the capture control register */
          LPC_TMR16B1->CCR = 7;
    
        }
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_1_IRQn);
      }
      else
      {
         LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
    
    
        /* Setup the external match register */
    //    LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
                    LPC_TMR16B0->EMR = (1<<0)|(1<<1)|(1<<3)|(1<<5) | (1<<7) | (1<<10) |(match_enable);
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        set_timer16_match(timer_num,match_enable,0);
    
        /* Enable the selected PWMs and enable Match3 */
        LPC_TMR16B0->PWMC = (1<<0) | (1<<1) | (1<<3)|(match_enable);
    
        /* Setup the match registers */
        /* set the period value to a global variable */
                    LPC_TMR16B0->PR=timer16_0_period;
        timer16_0_period = period;
        //LPC_TMR16B0->MR3       = timer16_1_period;
        LPC_TMR16B0->MR0 = timer16_1_period/2;
        LPC_TMR16B0->MR1 = timer16_1_period/2;
        LPC_TMR16B0->MR2 = timer16_1_period;
                    LPC_TMR16B0->TC=1;// added now
    
    
    //              LPC_IOCON->PIO0_8       &= ~0x07;  //clear pin settings
    //  LPC_IOCON->PIO0_8       |= 0x02;        // set as Timer0_16 MAT0
    
    //  LPC_IOCON->PIO0_9  &= ~0x07;  //clear pin settings
    //  LPC_IOCON->PIO0_9  |= 0x02;        // set as Timer0_16 MAT0
    //  LPC_TMR16B0->PWMC       = (1<<3)        // set the Match3
    //                         | (1<<0)        // set PWM0
    //                         | (1<<1)        // set PWM1
    //                         ;
    
    //  LPC_TMR16B0->MR2 = 500;//timer16_0_period;  // set the period to 1us
    //  LPC_TMR16B0->MR0 = 1000;//timer16_0_period/2;    // 50% duty cycle
    //  LPC_TMR16B0->MR1 = 1000;//timer16_0_period/4;
    //
    
    
    
        /* Set match control register */
        LPC_TMR16B0->MCR = 1<<10;// | 1<<9;                          /* Reset on MR3 */
    
        LPC_TMR16B0->TCR=1;
    
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_0_IRQn);
      }
    
            }
    
    
    /******************************************************************************
    ** Function name:               pwm16_setMatch
    **
    ** Descriptions:                Set the pwm16 match values
    **
    ** parameters:                  timer number, match numner and the value
    **
    ** Returned value:              None
    **
    ******************************************************************************/
    void setMatch_timer16PWM(uint8_t timer_num, uint8_t match_nr, uint32_t value)
    {
      if(timer_num)
      {
        switch(match_nr)
        {
          case 0:
            LPC_TMR16B1->MR0 = value;
          break;
          case 1:
            LPC_TMR16B1->MR1 = value;
          break;
          case 2:
            LPC_TMR16B1->MR2 = value;
          break;
          case 3:
            LPC_TMR16B1->MR3 = value;
          break;
          default:
            break;
        }
      }
      else
      {
        switch(match_nr)
        {
          case 0:
            LPC_TMR16B0->MR0 = value;
                      break;
          case 1:
            LPC_TMR16B0->MR1 = value;
          break;
          case 2:
            LPC_TMR16B0->MR2 = value;
          break;
          case 3:
            LPC_TMR16B0->MR3 = value;
          break;
          default:
          break;
        }
      }
    }
    

Reply
  • /******************************************************************************
    ** Function name:               init_timer16PWM
    **
    ** Descriptions:                Initialize timer as PWM
    **
    ** parameters:                  timer number, period and match enable:
    **                                              match_enable[0] = PWM for MAT0
    **                                              match_enable[1] = PWM for MAT1
    **                                              match_enable[2] = PWM for MAT2
    **
    ** Returned value:      None
    **
    ******************************************************************************/
    void init_timer16PWM(uint8_t timer_num, uint32_t period, uint8_t match_enable, uint8_t cap_enabled)
    {
      disable_timer16(timer_num);
    
      if (timer_num == 1)
      {
        /* Some of the I/O pins need to be clearfully planned if
        you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
        LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
    
        /* Setup the external match register */
    //    LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
                    LPC_TMR16B0->EMR = (1<<0)|(1<<1)|(1<<3)|(1<<5) | (1<<7) | (1<<10) |(match_enable) ;
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        set_timer16_match(timer_num, match_enable, 0 );
    
        /* Enable the selected PWMs and enable Match3 */
        LPC_TMR16B1->PWMC = (1<<3)|(match_enable);
    
        /* Setup the match registers */
        /* set the period value to a global variable */
        timer16_1_period    = period;
        LPC_TMR16B1->MR3         = timer16_1_period;
        LPC_TMR16B1->MR0 = timer16_1_period/2;
        LPC_TMR16B1->MR1 = timer16_1_period/2;
        LPC_TMR16B1->MR2 = timer16_1_period/2;
    
        /* Set match control register */
        LPC_TMR16B1->MCR = 1<<10;// | 1<<9;                          /* Reset on MR3 */
    
        if (cap_enabled)
        {
              /* Use location 0 for test. */
                set_timer16_capture( timer_num, 0 );
          LPC_TMR16B1->IR = 0xF;                                                 /* clear interrupt flag */
    
          /* Set the capture control register */
          LPC_TMR16B1->CCR = 7;
    
        }
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_1_IRQn);
      }
      else
      {
         LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
    
    
        /* Setup the external match register */
    //    LPC_TMR16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
                    LPC_TMR16B0->EMR = (1<<0)|(1<<1)|(1<<3)|(1<<5) | (1<<7) | (1<<10) |(match_enable);
        /* Setup the outputs */
        /* If match0 is enabled, set the output */
        set_timer16_match(timer_num,match_enable,0);
    
        /* Enable the selected PWMs and enable Match3 */
        LPC_TMR16B0->PWMC = (1<<0) | (1<<1) | (1<<3)|(match_enable);
    
        /* Setup the match registers */
        /* set the period value to a global variable */
                    LPC_TMR16B0->PR=timer16_0_period;
        timer16_0_period = period;
        //LPC_TMR16B0->MR3       = timer16_1_period;
        LPC_TMR16B0->MR0 = timer16_1_period/2;
        LPC_TMR16B0->MR1 = timer16_1_period/2;
        LPC_TMR16B0->MR2 = timer16_1_period;
                    LPC_TMR16B0->TC=1;// added now
    
    
    //              LPC_IOCON->PIO0_8       &= ~0x07;  //clear pin settings
    //  LPC_IOCON->PIO0_8       |= 0x02;        // set as Timer0_16 MAT0
    
    //  LPC_IOCON->PIO0_9  &= ~0x07;  //clear pin settings
    //  LPC_IOCON->PIO0_9  |= 0x02;        // set as Timer0_16 MAT0
    //  LPC_TMR16B0->PWMC       = (1<<3)        // set the Match3
    //                         | (1<<0)        // set PWM0
    //                         | (1<<1)        // set PWM1
    //                         ;
    
    //  LPC_TMR16B0->MR2 = 500;//timer16_0_period;  // set the period to 1us
    //  LPC_TMR16B0->MR0 = 1000;//timer16_0_period/2;    // 50% duty cycle
    //  LPC_TMR16B0->MR1 = 1000;//timer16_0_period/4;
    //
    
    
    
        /* Set match control register */
        LPC_TMR16B0->MCR = 1<<10;// | 1<<9;                          /* Reset on MR3 */
    
        LPC_TMR16B0->TCR=1;
    
        /* Enable the TIMER1 Interrupt */
        NVIC_EnableIRQ(TIMER_16_0_IRQn);
      }
    
            }
    
    
    /******************************************************************************
    ** Function name:               pwm16_setMatch
    **
    ** Descriptions:                Set the pwm16 match values
    **
    ** parameters:                  timer number, match numner and the value
    **
    ** Returned value:              None
    **
    ******************************************************************************/
    void setMatch_timer16PWM(uint8_t timer_num, uint8_t match_nr, uint32_t value)
    {
      if(timer_num)
      {
        switch(match_nr)
        {
          case 0:
            LPC_TMR16B1->MR0 = value;
          break;
          case 1:
            LPC_TMR16B1->MR1 = value;
          break;
          case 2:
            LPC_TMR16B1->MR2 = value;
          break;
          case 3:
            LPC_TMR16B1->MR3 = value;
          break;
          default:
            break;
        }
      }
      else
      {
        switch(match_nr)
        {
          case 0:
            LPC_TMR16B0->MR0 = value;
                      break;
          case 1:
            LPC_TMR16B0->MR1 = value;
          break;
          case 2:
            LPC_TMR16B0->MR2 = value;
          break;
          case 3:
            LPC_TMR16B0->MR3 = value;
          break;
          default:
          break;
        }
      }
    }
    

Children
No data