[原创] 用LM3S811控制LED亮度

qwertyuiop11111   2011-11-3 14:04 楼主

#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_ints.h"
#include "interrupt.h"
#include "sysctl.h"
#include "gpio.h"
#include "pwm.h"

  unsigned long Sysclk = 12000000UL;
#define PB0_PWM2  GPIO_PIN_0                
#define PB1_PWM3  GPIO_PIN_1


** Function name:       main()
** Descriptions:        主函数
int  main (void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_1 |                                    /* SysCtlClockSet函数()系统时钟设置  采用主振荡器  外接6MHz晶振  不分频*/
                   SYSCTL_USE_OSC  |
                   SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);                        /*  使能PWM2和PWM3输出所在GPIO  */
    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);                          /*  使能PWM模块                 */
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);                                 /*  PWM时钟配置:不分频         */
    GPIOPinTypePWM(GPIO_PORTB_BASE,                                     /*  PB0和PB1配置为PWM功能       */
                     GPIO_PIN_0 | GPIO_PIN_1);
    PWMGenConfigure(PWM_BASE, PWM_GEN_1,                                /*  配置PWM发生器1:加减计数    */
                    PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, 60000);                        /*  设置PWM发生器1的周期        */
    PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, 3000);                        /*  设置PWM2输出的脉冲宽度      */
    PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, 3000);                        /*  设置PWM3输出的脉冲宽度      */
    PWMOutputState(PWM_BASE,                                            /*  使能PWM2和PWM3的输出        */
                   PWM_OUT_2_BIT | PWM_OUT_3_BIT,
                   true);
    PWMGenEnable(PWM_BASE, PWM_GEN_1);                                  /*  使能PWM发生器1,开始产生    */
                                                                        /*  PWM方波                     */
    PWMGenIntTrigEnable(PWM_BASE,                                       /*  使能PWM发生器1归零触发中断  */
                        PWM_GEN_1,
                        PWM_INT_CNT_ZERO);
    PWMIntEnable(PWM_BASE, PWM_GEN_1);                                  /*  使能PWM发生器1中断          */
    IntEnable(INT_PWM1);                                                /*  使能PWM1中断                */
    IntMasterEnable();                                                  /*  使能总中断                  */
    for (;;) {
    }
}


/*********************************************************************************************************
** Function name:       PWM_Generator_1_ISR()
** Descriptions:        PWM发生器1中断服务函数
** input parameters:    NONE
** output parameters:   NONE
** Returned value:      NONE
*********************************************************************************************************/
void  PWM_Generator_1_ISR (void)
{
    const unsigned long ulTab[10] =
    {
         3000,  9000, 15000, 21000, 27000,
        33000, 39000, 45000, 51000, 57000
    };
    static unsigned long n = 0;
    PWMGenIntClear(PWM_BASE,
                   PWM_GEN_1,
                   PWM_INT_CNT_ZERO);
    PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, ulTab[n]);                    /*  设置PWM2输出的周期          */
    PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, ulTab[9-n]);                  /*  设置PWM3输出的周期          */
    SysCtlDelay(1500* (Sysclk / 3000));
 n++;
    if ( n >= 10 ) {
        n = 0;
    }
}

回复评论 (1)

回复 楼主 qwertyuiop11111 的帖子

问个有关 PWM 的问题,
基于同一发生器的 PWM 两对不带死区的信号一定是左对齐的吗?
点赞  2011-11-3 20:59
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复