zoukankan      html  css  js  c++  java
  • TI-RTOS 之 PWM

    TI-RTOS PWM

    CC1310 4个定时器,8PWM通道,在TI-RTOS它的驱动是写好的,引用时需要包含 PWM.h头文件即可。

    一般是任务主体之前,或者主函数进行初始化。

    Board_initPWM();

    //...

    pwmhandle  = PWM_open();

    PWM_open(pwmhandle);

    -------------------以上代码只需要调用一次-----------------------------------

    PWM_setDuty(); // 按应用设置占空比

    这里在有按键程序的基础上,实现按住左键不放,灯逐渐变至最亮,按住右键不放,灯逐渐变暗,直到熄灭。

    代码如下:

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

    Filename:       keyDemo3.c

    Editor:         Tome @ newbit

    Revised:        $Date: 2016-8-11 11:20:02 +0800  $

    Revision:       $Revision: 00001 $

    Description:    了解 TI-RTOS的使用之,Event的使用,它用来同步任务,或者

                     Task - Hwis, Swis,同时也用到了PWM

    History:        

    Notes:          要了解到这部分的接口,可阅读TI文档

    1. TI-RTOS 2.20  User's Guide.pdf

    2. Bios User Guide.pdf

    硬件平台  CC1130_LAUNCHPAD Rev1.3

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

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

    // INCLUDES

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

    /* XDCtools Header files */

    #include <xdc/std.h>

    #include <xdc/runtime/System.h>

    /* BIOS Header files */

    #include <ti/sysbios/BIOS.h>

    #include <ti/sysbios/knl/Task.h>

    #include <ti/sysbios/knl/Event.h>

    /* TI-RTOS Header files */

    #include <ti/drivers/PIN.h>

    #include <ti/drivers/PWM.h>

    #include "Board.h"

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

    // TYPEDEF

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

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

    // CONSTANTS

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

    #define TASKSTACKSIZE     768

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

    // LOCAL VERIABLE

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

    Task_Struct keyTaskStruct;

    Char keyTaskStack[TASKSTACKSIZE];               // 本任务的栈空间,静态分配

    /* Global memory storage for a PIN_Config table */

    static PIN_State buttonPinState;

    PIN_Handle ledPinHandle;

    PIN_Handle buttonPinHandle;

    // 新建 Event, 它用来通知任务,按键已经按下

    Event_Struct evtStruct;

    Event_Handle evtHandle;

    /*

    * Initial LED pin configuration table

    *   - LEDs Board_LED0 is on.

    *   - LEDs Board_LED1 is off.

    */

    PIN_Config ledPinTable[] = {

      Board_LED0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,

      Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW  | PIN_PUSHPULL | PIN_DRVSTR_MAX,

      PIN_TERMINATE

    };

    /*

    * Application button pin configuration table:

    *   - Buttons interrupts are configured to trigger on falling edge.

    */

    PIN_Config buttonPinTable[] = {

      Board_BUTTON0  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,

      Board_BUTTON1  | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,

      PIN_TERMINATE

    };

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

    // FUNCTIONS DECLERATION

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

    Void keyFxn(UArg arg0, UArg arg1);

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId);

    uint_t halKeyScan(void);

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

    // FUNCTIONS

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

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

    * @fn      keyTaskAdd

    *

    * @brief   

    *

    * @param   void

    *

    * @return  void

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

    void keyTaskAdd(void)

    {

      Task_Params taskParams;

      

      /* Construct BIOS objects */

      Task_Params_init(&taskParams); // 创建任务所要的参数,都设置为默认值

      taskParams.stackSize = TASKSTACKSIZE; // 栈空间

      taskParams.stack = &keyTaskStack;     // 栈地址

      // bios 传递参数,建立控制灯的任务

      Task_construct(&keyTaskStruct, (Task_FuncPtr)keyFxn, &taskParams, NULL);

    }

    /*

    *  ======== keyFxn ========

    *  Task for this function is created statically. See keyTaskAdd().

    */

    Void keyFxn(UArg arg0, UArg arg1)

    {

      

      Event_Params evtParams;

      Event_Params_init(&evtParams);

      Event_construct(&evtStruct, &evtParams);

      

      evtHandle = Event_handle(&evtStruct);

      

      // PWM模块初始化

      Board_initPWM();

      

      // PWM IO的配置 在数组 PWM_config[]

      

      PWM_Handle pwm1;

      PWM_Params params;

      uint16_t   pwmPeriod = 3000;      // Period and duty in microseconds

      uint16_t   duty = 0;

      uint16_t   dutyInc = 100;

      

      // 这里使用的PWM通道 已经定义为 Board_GLED,详见 CC131X_LAUNCHXL.c

      PWM_Params_init(¶ms);

      params.dutyUnits = PWM_DUTY_US;

      params.dutyValue = 0;

      params.periodUnits = PWM_PERIOD_US;

      params.periodValue = pwmPeriod;

      pwm1 = PWM_open(Board_PWM1, ¶ms);

      

      if (pwm1 == NULL) {

        System_abort("Board_PWM0 did not open");

      }

      PWM_start(pwm1);  

      

      

      // 得到按键IO的操作句柄

      buttonPinHandle = PIN_open(&buttonPinState, buttonPinTable);

      if(!buttonPinHandle) {

        System_abort("Error initializing button pins ");

      }    

      

      /* Setup callback for button pins */

      // 注册按键的中断回调函数

      if (PIN_registerIntCb(buttonPinHandle, &buttonCallbackFxn) != 0) {

        System_abort("Error registering button callback function");

      }

      

      uint16_t keys = 0;

      //

      while(1)

      {    

        // 如果没有按键 任务将在这里阻塞,直到有键按下

        if ( halKeyScan() == 0 )   

        {

          Event_pend(evtHandle,Event_Id_00, Event_Id_NONE, BIOS_WAIT_FOREVER );

          // 任务休眠 20 ms 用于消抖

          /* Debounce logic, only toggle if the button is still pushed (low) */

          Task_sleep(20*100);

        }

        

        // 读按键

        keys = halKeyScan();

        if ( keys  & 0x01 )         // 左键  增加占空比

        {

          if ( duty < pwmPeriod )

            duty += dutyInc;

        }

        if ( keys & 0x02 )          // 右键  减小占空比

        {

          if ( duty >= dutyInc )

            duty -= dutyInc;

        }

        

        // 重新设置占空比

        PWM_setDuty(pwm1, duty);

        

        // 休眠一段时间,拉长PWM变化的视觉效果

        Task_sleep(2000);

      }

      

    }

    uint_t halKeyScan(void)

    {

      uint_t key = 0;

      

      if (  !PIN_getInputValue(Board_BUTTON0))

      {

        key |= 0x01;

      }

      

      if (  !PIN_getInputValue(Board_BUTTON1))

      {

        key |= 0x02;

      }

      return key;

    }

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

    * @fn      buttonCallbackFxn

    *

    * @brief   按键中断的回调函数

    *

    * @param   PIN_Handle handle, PIN_Id pinId

    *

    * @return  void

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

    void buttonCallbackFxn(PIN_Handle handle, PIN_Id pinId)

    {

      // 解除任务的阻塞

      Event_post(evtHandle, Event_Id_00 );

    }

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

    Copyright 2016 Newbit Studio. All rights reserved.

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

  • 相关阅读:
    [Angular] Using the platform agnostic Renderer & ElementRef
    [HTML5] Focus management using CSS, HTML, and JavaScript
    [TypeScript] Increase TypeScript's type safety with noImplicitAny
    [React] displayName for stateless component
    [Redux] Important things in Redux
    [HTML5] Using the tabindex attribute for keyboard accessibility
    [Angular] @ViewChild and template #refs to get Element Ref
    [Angular] @ViewChildren and QueryLists (ngAfterViewInit)
    [Angular] Difference between Providers and ViewProviders
    [Angular] Difference between ngAfterViewInit and ngAfterContentInit
  • 原文地址:https://www.cnblogs.com/newbit/p/ti_rtos_pwm.html
Copyright © 2011-2022 走看看