PWM of Ti-rtos
The CC1310 has 4 timers,8 PWM channels, and the ti-rtos Its driver is written well, the reference needs to include the PWM.h header file.
is typically preceded by the task principal, or the main function is initialized.
BOARD_INITPWM ();
//...
Pwmhandle = Pwm_open ();
Pwm_open (Pwmhandle);
-------------------the above code only needs to be called once -----------------------------------
Pwm_setduty (); set duty ratio by app
Here on the basis of a key program, the realization of holding down the left button, the light gradually changed to the brightest, hold the right button, the light gradually darkened, until extinguished.
The code is as follows:
/**************************************************************************************************
Filename:keydemo3.c
Editor:tome @ newbit
Revised: $Date: 2016-8-11 11:20:02 +0800 $
Revision: $Revision: 00001 $
Description: Understand the use of Ti-rtos , the use of theEvent , It is used to synchronize tasks, or
Task-hwis, Swis, also uses a PWM
History:
Notes: To understand this part of the interface, you can read the TI document
1. Ti-rtos 2.20 User ' s Guide.pdf
2. Bios User Guide.pdf
Hardware platform 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]; stack space for this task, static assignment
/* Global memory storage for a pin_config table */
Static Pin_state buttonpinstate;
Pin_handle Ledpinhandle;
Pin_handle Buttonpinhandle;
creates a new Event, which is used to notify the task that the key has been pressed
Event_struct evtstruct;
Event_handle Evthandle;
/*
* Initial LED pin Configuration table
*-LEDs board_led0 are 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 is 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); the parameters that are required to create the task are set to the default values
Taskparams.stacksize = taskstacksize; Stack Space
Taskparams.stack = &keyTaskStack; Stack Address
to pass parameters to the BIOS and set up a control light task
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 Module Initialization
BOARD_INITPWM ();
The configuration of the PWM IO in the array 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;
the PWM channel used here has been defined as board_gled, see 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);
get operation handle of key IO
Buttonpinhandle = Pin_open (&buttonpinstate, buttonpintable);
if (!buttonpinhandle) {
System_abort ("Error initializing button pins\n");
}
/* Setup callback for button pins */
register the interrupt callback function of the key
if (PIN_REGISTERINTCB (Buttonpinhandle, &buttoncallbackfxn)! = 0) {
System_abort ("Error registering button callback function");
}
uint16_t keys = 0;
//
while (1)
{
if there are no keystrokes, the task will block here until a key is pressed
if (halkeyscan () = = 0)
{
Event_pend (evthandle,event_id_00, Event_id_none, bios_wait_forever);
Task Sleep - ms for anti-shake
/* debounce logic, only toggle if the button was still pushed (low) */
Task_sleep (20*100);
}
Read button
Keys = Halkeyscan ();
if (keys & 0x01)// left-click to increase duty duty
{
if (Duty < Pwmperiod)
Duty + = Dutyinc;
}
if (keys & 0x02)// right-click to reduce Duty
{
if (Duty >= dutyinc)
Duty-= Dutyinc;
}
Reset Duty Ratio
Pwm_setduty (pwm1, duty);
Sleep for a period of time, lengthen The visual effect of PWM changes
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
*
* callback function for @brief key interrupt
*
* @param pin_handle Handle, pin_id Pinid
*
* @return void
**************************************************************************************************/
void Buttoncallbackfxn (Pin_handle Handle, pin_id Pinid)
{
unblocking a task
Event_post (Evthandle, event_id_00);
}
/**************************************************************************************************
Copyright newbit Studio. All rights reserved.
**************************************************************************************************/
PWM of Ti-rtos