CC3200-PWM

来源:互联网 发布:Windows VPN 编辑:程序博客网 时间:2024/06/05 02:12

在CC3200SDK-1.2.0上测试可用,下面是main函数的代码:

#include <stdio.h>#include "hw_types.h"#include "hw_ints.h"#include "hw_memmap.h"#include "hw_apps_rcm.h"#include "hw_common_reg.h"#include "interrupt.h"#include "rom.h"#include "rom_map.h"#include "timer.h"#include "utils.h"#include "prcm.h"#include "pinmux.h"#define APPLICATION_VERSION     "1.1.1"#define DBG_PRINT               Report#define TIMER_INTERVAL_RELOAD   40035                       /* =(255*157)  定时器初值计算 */#define DUTYCYCLE_GRANULARITY   157#if defined(ccs)extern void (* const g_pfnVectors[])(void);#endif#if defined(ewarm)extern uVectorEntry __vector_table;#endifvoid UpdateDutyCycle(unsigned long ulBase, unsigned long ulTimer,                     unsigned char ucLevel){    MAP_TimerMatchSet(ulBase,ulTimer,(ucLevel*DUTYCYCLE_GRANULARITY));}void SetupTimerPWMMode(unsigned long ulBase, unsigned long ulTimer,                       unsigned long ulConfig, unsigned char ucInvert){    //    //配置定时器在PWM模式                   PWM 的计数器是 16bit 的,分频器是 8bit,16加 8 = 24bit,计数器的 16bit是高位。    //    MAP_TimerConfigure(ulBase,ulConfig);                                //定时器基地址,定时器输出模式配置    MAP_TimerPrescaleSet(ulBase,ulTimer,0);                             //基地址,定时器选择,高八位分频寄存器0~255    //    //反向输出如果需要计时器    //    MAP_TimerControlLevel(ulBase,ulTimer,ucInvert);                     //控制输出电平         true表示低电平    MAP_TimerLoadSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);             //PWM周期          定时器初值255*157    MAP_TimerMatchSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);            //匹配值设置,输出电平0}void InitPWMModules(){    MAP_PRCMPeripheralClkEnable(PRCM_TIMERA2, PRCM_RUN_MODE_CLK);       //初始化定时器,为 PWM 输出    MAP_PRCMPeripheralClkEnable(PRCM_TIMERA3, PRCM_RUN_MODE_CLK);    SetupTimerPWMMode(TIMERA2_BASE, TIMER_B,                            //配置 TIMERA2 的 PWM 输出,驱动红色 LED            (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PWM), 1);    SetupTimerPWMMode(TIMERA3_BASE, TIMER_A,             (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM), 1);    SetupTimerPWMMode(TIMERA3_BASE, TIMER_B,             (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM), 1);    MAP_TimerEnable(TIMERA2_BASE,TIMER_B);                              ////使能相关的定时器,允许定时器    MAP_TimerEnable(TIMERA3_BASE,TIMER_A);    MAP_TimerEnable(TIMERA3_BASE,TIMER_B);}void DeInitPWMModules(){    MAP_TimerDisable(TIMERA2_BASE, TIMER_B);    MAP_TimerDisable(TIMERA3_BASE, TIMER_A);    MAP_TimerDisable(TIMERA3_BASE, TIMER_B);    MAP_PRCMPeripheralClkDisable(PRCM_TIMERA2, PRCM_RUN_MODE_CLK);    MAP_PRCMPeripheralClkDisable(PRCM_TIMERA3, PRCM_RUN_MODE_CLK);}static voidBoardInit(void){/* In case of TI-RTOS vector table is initialize by OS itself */#ifndef USE_TIRTOS  //  // Set vector table base  //#if defined(ccs)    MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);#endif#if defined(ewarm)    MAP_IntVTableBaseSet((unsigned long)&__vector_table);#endif#endif    //    // Enable Processor    //    MAP_IntMasterEnable();    MAP_IntEnable(FAULT_SYSTICK);    PRCMCC3200MCUInit();}void main(){    int iLoopCnt;    BoardInit();    PinMuxConfig();        InitPWMModules();    while(1)    {        //通过更改各路 PWM 占空比,从而改变各个LE灯的亮度        for(iLoopCnt = 0; iLoopCnt < 255; iLoopCnt++)        {            UpdateDutyCycle(TIMERA2_BASE, TIMER_B, iLoopCnt);        //更新PWM占空比          占空比=脉冲宽度/信号周期            UpdateDutyCycle(TIMERA3_BASE, TIMER_B, iLoopCnt);            UpdateDutyCycle(TIMERA3_BASE, TIMER_A, iLoopCnt);            MAP_UtilsDelay(800000);        }    }}
原创粉丝点击