CC2640如何输出4路相同频率不同占空比的PWM?急急急急
CC2640如何输出4路相同频率不同占空比的PWM?
CC2640如何输出4路相同频率不同占空比的PWM?
cc2540实现过。 cc2640目前还没有板子, 找一下官方代码和使用手册。
我自己好像搞定了,是这样的吗?
#include "board_pwm.h"
#include <ti/sysbios/family/arm/cc26xx/Power.h>
#include <ti/sysbios/family/arm/cc26xx/PowerCC2650.h>
#include <driverlib/timer.h>
#include <ti/drivers/pin/PINCC26XX.h>
#define PWM_WHITE IOID_25
#define PWM_RED IOID_27
#define PWM_GREEN IOID_7
#define PWM_BLUE IOID_0
PIN_Config PWMPinTable[] = { PWM_WHITE | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_INPUT_DIS | PIN_DRVSTR_MAX,
PWM_RED | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_INPUT_DIS | PIN_DRVSTR_MAX,
PWM_GREEN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_INPUT_DIS | PIN_DRVSTR_MAX,
PWM_BLUE | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_INPUT_DIS | PIN_DRVSTR_MAX,
PIN_TERMINATE };
static PIN_Handle pinHandle;
static PIN_State pinState;
static bool PWM_Init_Flag = false;
static uint16 pwm_freq = 1000;
//初始化CC2640的输出PWM的GPIO
void PWM_Board_Init()
{
pinHandle = PIN_open(&pinState, PWMPinTable);
}
//初始化白光,红光,绿光,蓝光PWM输出
void PWM_Init()
{
uint32_t load;
if(!PWM_Init_Flag)
{
PWM_Init_Flag =true;
PWM_Board_Init();
//RTOS: Enable peripheral domain and clocks for timer
Power_setDependency(PERIPH_GPT0);
Power_setDependency(PERIPH_GPT1);
// RTOS: Route pin to timer0A module
// IOC Port events 0-7 are mapped to GPT timers 0A,0B,1A,1B...3B
//PWM_WHITE: GPT0_BASE TIMEA
//PWM_RED: GPT0_BASE TIMEB
//PWM_GREEN: GPT1_BASE TIMEA
//PWM_BLUE: GPT1_BASE TIMEB
PINCC26XX_setMux(pinHandle, PWM_WHITE, IOC_PORT_MCU_PORT_EVENT0);
PINCC26XX_setMux(pinHandle, PWM_RED, IOC_PORT_MCU_PORT_EVENT1);
PINCC26XX_setMux(pinHandle, PWM_GREEN, IOC_PORT_MCU_PORT_EVENT2);
PINCC26XX_setMux(pinHandle, PWM_BLUE, IOC_PORT_MCU_PORT_EVENT3);
// Use GPT0/GPT1, Channel A (16 bit timer)
TimerConfigure(GPT0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM);
TimerConfigure(GPT1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM | TIMER_CFG_B_PWM);
TimerDisable(GPT0_BASE, TIMER_A);
TimerDisable(GPT0_BASE, TIMER_B);
TimerDisable(GPT1_BASE, TIMER_A);
TimerDisable(GPT1_BASE, TIMER_B);
load = (48000000 / pwm_freq);
// Timer A; freq 48 MHz / TM_LOAD_XXXXHZ
TimerLoadSet(GPT0_BASE, TIMER_A, load);
TimerMatchSet(GPT0_BASE, TIMER_A, (uint32_t)(load - 1) );
TimerLoadSet(GPT0_BASE, TIMER_B, load);
TimerMatchSet(GPT0_BASE, TIMER_B, (uint32_t)(load - 1));
TimerLoadSet(GPT1_BASE, TIMER_A, load);
TimerMatchSet(GPT1_BASE, TIMER_A, (uint32_t)(load - 1));
TimerLoadSet(GPT1_BASE, TIMER_B, load);
TimerMatchSet(GPT1_BASE, TIMER_B, (uint32_t)(load - 1));
TimerEnable(GPT0_BASE, TIMER_A);
TimerEnable(GPT0_BASE, TIMER_B);
TimerEnable(GPT1_BASE, TIMER_A);
TimerEnable(GPT1_BASE, TIMER_B);
}
}
void PWM_Set_Freq(uint16 freq)
{
uint32_t load;
pwm_freq = freq;
load = (48000000 / pwm_freq);
// Timer A; freq 48 MHz / TM_LOAD_XXXXHZ
TimerLoadSet(GPT0_BASE, TIMER_A, load);
TimerLoadSet(GPT0_BASE, TIMER_B, load);
TimerLoadSet(GPT1_BASE, TIMER_A, load);
TimerLoadSet(GPT1_BASE, TIMER_B, load);
}
uint16 PWM_Get_Freq()
{
return pwm_freq;
}
void PWM_Set_Duty_Ratio(uint16 white, uint16 red, uint16 green, uint16 blue)
{
uint32_t load;
load = (48000000 / pwm_freq);
//白光
if(white == 0)
{
TimerMatchSet(GPT0_BASE, TIMER_A, (uint32_t)(load - 1));
}
else if(white == 1000)
{
TimerMatchSet(GPT0_BASE, TIMER_A, (uint32_t)(1));
}
else
{
TimerMatchSet(GPT0_BASE, TIMER_A, (uint32_t)(load * (1000 - white) / 1000)); // 50 per cent duty cycle
}
//红光分量
if(red == 0)
{
TimerMatchSet(GPT0_BASE, TIMER_B, (uint32_t)(load - 1));
}
else if(red == 1000)
{
TimerMatchSet(GPT0_BASE, TIMER_B, (uint32_t)(1));
}
else
{
TimerMatchSet(GPT0_BASE, TIMER_B, (uint32_t)(load * (1000 - red) / 1000));
}
//绿光分量
if(green == 0)
{
TimerMatchSet(GPT1_BASE, TIMER_A, (uint32_t)(load - 1));
}
else if(green == 1000)
{
TimerMatchSet(GPT1_BASE, TIMER_A, (uint32_t)(1));
}
else
{
TimerMatchSet(GPT1_BASE, TIMER_A, (uint32_t)(load * (1000 - green) / 1000)); // 50 per cent duty cycle
}
//蓝光分量
if(blue == 0)
{
TimerMatchSet(GPT1_BASE, TIMER_B, (uint32_t)(load - 1));
}
else if(blue == 1000)
{
TimerMatchSet(GPT1_BASE, TIMER_B, (uint32_t)(1));
}
else
{
TimerMatchSet(GPT1_BASE, TIMER_B, (uint32_t)(load * (1000 - blue) / 1000)); // 50 per cent duty cycle
}
}
//单片调试测试用例
//例如862,代表86.2%的占空比
void Test_PWM()
{
PWM_Init();
//红光
PWM_Set_Duty_Ratio(0, 10, 0, 0);
PWM_Set_Duty_Ratio(0, 500, 0, 0);
PWM_Set_Duty_Ratio(0, 1000, 0, 0);
//绿光
PWM_Set_Duty_Ratio(0, 0, 10, 0);
PWM_Set_Duty_Ratio(0, 0, 500, 0);
PWM_Set_Duty_Ratio(0, 0, 1000, 0);
//蓝光
PWM_Set_Duty_Ratio(0, 0, 0, 10);
PWM_Set_Duty_Ratio(0, 0, 0, 500);
PWM_Set_Duty_Ratio(0, 0, 0, 1000);
//黄光
PWM_Set_Duty_Ratio(0, 500, 500, 0);
PWM_Set_Duty_Ratio(0, 1000, 1000, 0);
PWM_Set_Duty_Ratio(0, 1000, 500, 0);
PWM_Set_Duty_Ratio(0, 1000, 500, 500);
//青光
PWM_Set_Duty_Ratio(0, 0, 500, 500);
PWM_Set_Duty_Ratio(0, 0, 1000, 1000);
PWM_Set_Duty_Ratio(0, 0, 500, 1000);
PWM_Set_Duty_Ratio(0, 500, 500, 1000);
//紫光
PWM_Set_Duty_Ratio(0, 500, 0, 500);
PWM_Set_Duty_Ratio(0, 1000, 0, 1000);
PWM_Set_Duty_Ratio(0, 1000, 0, 500);
PWM_Set_Duty_Ratio(0, 1000, 500, 500);
//白光
PWM_Set_Duty_Ratio(10, 0, 0, 0);
PWM_Set_Duty_Ratio(500, 0, 0, 0);
PWM_Set_Duty_Ratio(1000, 0, 0, 0);
PWM_Set_Duty_Ratio(0, 0, 0, 0);
}
你好,请问你是在哪里找到的pwm频率设置的相关内容的?我现在在使用tirtos库函数,系统时钟是1MHz,我没办法使pwm时钟改变。可以麻烦你提供相关的文档给我吗?谢谢。