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.

PWM Timer 4 output for CC2530

Other Parts Discussed in Thread: CC2530

I am working on  CC2530 PWM output to  cotrol the brightness of a LED  indicator. I don't get the result when I press the buton K1 or K2.  I debug  the code , it can response the K1 or K1  to run TIMER34_SET_PWM_PULSE_LENGTH(4,luminance). The following is my code:

//#include <ioCC2530.h>
#include "hal.h"


*****************************************/
BYTE counter = 0;

/*****************************************
//T4 c
*****************************************/

#define TIMER34_SET_CLOCK_DIVIDE(timer,val)        \
  do{                                             \
    T##timer##CTL &= ~0XE0;                               \
      (val==2) ? (T##timer##CTL|=0X20):                   \
      (val==4) ? (T##timer##CTL|=0x40):                   \
      (val==8) ? (T##timer##CTL|=0X60):                   \
      (val==16)? (T##timer##CTL|=0x80):                   \
      (val==32)? (T##timer##CTL|=0xa0):                   \
      (val==64) ? (T##timer##CTL|=0xc0):                  \
      (val==128) ? (T##timer##CTL|=0XE0):                 \
      (T##timer##CTL|=0X00);             /* 1 */          \
  }while(0)

//Macro for setting the mode of timer3 or 4
#define TIMER34_SET_MODE(timer,val)                \
  do{                                             \
    T##timer##CTL &= ~0X03;                               \
    (val==1)?(T##timer##CTL|=0X01):  /*DOWN            */ \
    (val==2)?(T##timer##CTL|=0X02):  /*Modulo          */ \
    (val==3)?(T##timer##CTL|=0X03):  /*UP / DOWN       */ \
    (T##timer##CTL|=0X00);           /*free runing */     \
  }while(0)


#define K1  P0_7
#define K2  P0_6
#define S1 0X01
#define S2 0x02
#define NOKEY 0xff

/*****************************************
//
*****************************************/
void InitKey(void)
{
  P0SEL &= ~0X30;
  P0DIR &= ~0X30;
  P0INP &= ~0X30;
  P2INP |= 0X20;

}
/*****************************************
//
*****************************************/
void delay(UINT16 val)
  {
    UINT16 jj;
    for(jj=0;jj<val;jj++);
    for(jj=0;jj<val;jj++);
    for(jj=0;jj<val;jj++);
    for(jj=0;jj<val;jj++);
    for(jj=0;jj<val;jj++);
}

 

/*****************************************
//
*****************************************/
void Init_T4_AND_LED(void)
{
    IO_FUNC_PORT_PIN(1,0,IO_FUNC_GIO);
    IO_DIR_PORT_PIN(1,0,IO_OUT);
    IO_FUNC_PORT_PIN(1,1,IO_FUNC_PERIPH);
    IO_PER_LOC_TIMER4_AT_PORT1_PIN01();

    TIMER34_INIT(4);                  //

    TIMER34_SET_MODE(4,3);

    TIMER34_PWM_CONFIG(4);//T4CCTL1 |= 0X24;

    //TIMER34_SET_CLOCK_DIVIDE(4,2);      //

    TIMER34_SET_PWM_PULSE_LENGTH(4,250);  //
    TIMER34_SET_MODE(4,3);                 //up&down
    TIMER4_RUN(1);

};

/*********************************************************************

*********************************************************************/
BYTE KeyScan(void)
{
  if(K1 == 0)
  {
    delay(200);
    if(K1 == 0)return(S1);
  }
  if(K2 ==0)
  {
    delay(200);
    if(K2 == 0)return(S2);
  }
  return(NOKEY);
}

void main(void)
{
  BYTE keyvalue;
  BYTE luminance=200;
    InitKey();
    Init_T4_AND_LED();                  //   
    while(1)                        //
    {
      keyvalue=KeyScan();
      if(keyvalue==S1)
      {
        if(luminance<=200)luminance+=50;
        TIMER34_SET_PWM_PULSE_LENGTH(4,luminance);
      }
      if(keyvalue==S2)
      {
        if(luminance>=50)luminance-=50;
        TIMER34_SET_PWM_PULSE_LENGTH(4,luminance);
      }
      delay(20000);
    }

}

I don't know why I can adjust the LED brightness.Can someone help me,please.

Thanks for reply 

  • I personally cannot afford the time to scour your pwm code, but I have used the h/w timers and they are very straight forward to use if you follow the documentation of the h/w registers in the cc253x user's guide.

    Note that the h/w timers require the system clock to be running - so you cannot enable low power sleep. You must not define POWER_SAVING or you must specifically invoke the OSAL PwrMgr to holdoff sleep while PWM-ing. Also, if your code requires ISR handlers, the global EA must be enabled as well as the specific ISR (e.g. the Timer4 overflow enable).

  • Thank you. The question is done after I modify the code  TIMER34_SET_MODE(4,3) to TIMER34_SET_MODE(4,0) following your advice.Thank you.

  • i have a 16 bit values from 20 to 30 and i have to use pwm to control the speed of a dc motor .. i am using cc2530dk kit .. the problem is i am not able to find the function for using the pwm and i have no idea ... any comments pl 

  • Hi Dhileep,

    The following sample code can send PWM to P1_4.

      PERCFG &= (~(0x20)); // Select Timer 3 Alternative 1 location
      P2SEL |=0x20;
      P2DIR |= 0xC0;  // Give priority to Timer 1 channel2-3
      P1SEL |= BV(4);  // Set P1_0 to peripheral, Timer 1,channel 2
      P1DIR |= BV(4);

      T3CTL &= ~0x10;             // Stop timer 3 (if it was running)
      T3CTL |= 0x04;              // Clear timer 3
      T3CTL &= ~0x08;             // Disable Timer 3 overflow interrupts
      T3CTL |= 0x03;              // Timer 3 mode = 3 - Up/Down

      T3CCTL1 &= ~0x40;           // Disable channel 0 interrupts
      T3CCTL1 |= 0x04;            // Ch0 mode = compare
      T3CCTL1 |= 0x10;            // Ch0 output compare mode = toggle on compare

      T3CTL &= ~0xE0;   // Clear Prescaler divider value
      T3CTL |= 0xA0;    //Set Prescaler divider value = Tick frequency /32
      T3CC0 = 128;      //Set ticks = 128

      // Start timer
      T3CTL |= 0x10;

  • thank you sir !! 

  • Hi Yikai,

    I am new to the cc2530 and registers. I want to generate the pwm signals of 4 khz at P1.3 to control the Buzzer. can u help me todo this plz
  • Hi Yikai,

    I am new to CC2530 and rgisters. i want to generate the PWM signal of 4 KHz at port P1.3 to contorl the buzzer connect to it. Can you suggest to do this plz
  • I have already provided example code in previous reply and you can revise it to serve your purpose. You will need to read CC2530 user guide to understand CC2530 registers.