CC2530 PWM波形产生。

时间:2023-03-08 20:25:15

1、使用TIM3_CC1,相关联引脚P1_7

#define GPIOPWM() do{P1SEL |= 0x80;}while(0);
#define GPIOCLOSEPWM() do{P1SEL  &= ~0x80; P1_7 = 0;}while(0);

void GPIOInit(void)

{

  P1SEL  &= ~0x80;
   P1DIR  |= 0x80;
   P1_7 = 0;

}

void timer3Init(void)//产生PWM
{
    PERCFG |= 0x20;//选择定时器3的备用位置2
    P2SEL  |= 0x20;//设置定时器3优先

  T3CCTL1 = 0;
 
    T3CCTL0 = 0x10|0x04;
    T3CC0 = 254;//实际的定时器值,用于产生周期的设置
    T3CCTL1 = 0x18|0x04; // Compare mode 3
    T3CC1 = 1;
    T3CTL = 0xA0|0x10|0x02; // Div, Start, modulo

}

void GPIOCarrConfig(unsigned char carr1)//占空比配置

{

  unsigned char carr;
    carr = ~carr1;
    GPIOCLOSEPWM();
    if( carr > 0 )
    {
     T3CC1 = carr;
    }
    else
    {
       T3CC1 = 1;
    }
    GPIOPWM();

}

2.使用TIM1的不同通道产生不同的PWM信号,例如:配置P0_2、P0_3、P0_4、P0_5、P0_6分别配置不同的PWM信号等。

可参考TI官网工程师给顾客的答疑部分,网址如下:http://e2e.ti.com/support/low_power_rf/f/156/t/118346.aspx?pi36597=2

部分相关配置如下:

PERCFG &= ~0x40; // Select Timer 1 Alternative 0 location
PERCFG |= 0x03; // Move USART0 and USART1 to Alternative 2 location to allow all Timer 1 channels on P0
P2DIR = (P2DIR & ~0xC0) | 0x80; // Give priority to Timer 1
P0SEL |= 0x3C;  // Set P0_2-P0_5 to peripheral

T1CC0L = 0xff;   // PWM signal period
T1CC0H = 0x7f;

T1CCTL0 = 0x1c; // Channel 0 in compare mode, Set output on compare-up, clear on 0 (50% duty cycle)

T1CC1L = 0x78;  // PWM duty cycle, Channel 1 (P0_3)
T1CC1H = 0x10;

T1CCTL1 = 0x1c; // Channel 1 in compare mode, Set output on compare-up, clear on compare-down

T1CC2L = 0x78;  // PWM duty cycle, Channel 2 (P0_4)
T1CC2H = 0x10;

T1CCTL2 = 0x1c; // Channel 2 in compare mode, Set output on compare-up, clear on compare-down

T1CC3L = 0x78;  // PWM duty cycle, Channel 3 (P0_4)
T1CC3H = 0x10;

T1CCTL3 = 0x1c; // Channel 3 in compare mode, Set output on compare-up, clear on compare-down

T1CC4L = 0x78;  // PWM duty cycle, Channel 4 (P0_5)
T1CC4H = 0x10;

T1CCTL4 = 0x1c; // Channel 4 in compare mode, Set output on compare-up, clear on compare-down

T1CTL |= 0x0f; // divide with 128 and to do i up-down mode

注意:

on P0_2 you can only get 50% duty cycle unless you can use free-running mode, which will limit the available settings for the period. The period you are using now is 131 ms, provided that you use the maximum tick speed (32 MHz). If you change to free running mode, you have to choose between a period of 65.5 ms and 262 ms using the maximum tick speed. But if you use a tick speed of 16 MHz, you can get a period of 131 ms in free-running mode using a presacler of 32. See the CLKCONCMD register on how to change the tick speed.

If you want more concrete advice on this, please let me know what your requirements are for the period and the duty cycle.

A program can look something like this. Here, I have set up the same duty cycle on all channels except channel 0. To use different duty cycle for some of the channels, just change the value of T1CCnL/T1CCnH for those channels.