b_mod_pwm
数据结构
#define PWM_HANDLER_CCR (0)
#define PWM_HANDLER_PERIOD (1)
//PWM回调函数,type: PWM_HANDLER_CCR or PWM_HANDLER_PERIOD
typedef void (*pPwmHandler)(uint8_t type);
typedef struct bSoftPwmStruct
{
uint32_t repeat; //指定重复次数,为0则一直重复
uint32_t tick; //用于计时
uint32_t period; //周期,单位ms
uint32_t ccr; //CCR,单位ms
pPwmHandler handler; //回调执行函数
uint32_t flag; //执行回调标志
struct bSoftPwmStruct *next;
} bSoftPwmStruct_t;
typedef bSoftPwmStruct_t bSoftPwmInstance_t;
// 创建PWM实例,指定PWM的参数
#define bPWM_INSTANCE(name, _period, _ccr, _repeat) \
bSoftPwmInstance_t name = {.period = _period, .ccr = _ccr, .repeat = _repeat};
接口介绍
//启动PWM,并指定回调
int bSoftPwmStart(bSoftPwmInstance_t *pPwmInstance, pPwmHandler handler);
int bSoftPwmStop(bSoftPwmInstance_t *pPwmInstance);
int bSoftPwmReset(bSoftPwmInstance_t *pPwmInstance);
int bSoftPwmSetPeriod(bSoftPwmInstance_t *pPwmInstance, uint32_t ms);
int bSoftPwmSetCcr(bSoftPwmInstance_t *pPwmInstance, uint32_t ms);
使用例子
bPWM_INSTANCE(led1_pwm, 20, 5, 0);
bPWM_INSTANCE(led2_pwm, 20, 18, 0);
void PwmHandler1(uint8_t type)
{
if(type == PWM_HANDLER_CCR)
{
bHalGpioWritePin(B_HAL_GPIOD, B_HAL_PIN7, 0);
}
else
{
bHalGpioWritePin(B_HAL_GPIOD, B_HAL_PIN7, 1);
}
}
void PwmHandler2(uint8_t type)
{
if(type == PWM_HANDLER_CCR)
{
bHalGpioWritePin(B_HAL_GPIOD, B_HAL_PIN3, 0);
}
else
{
bHalGpioWritePin(B_HAL_GPIOD, B_HAL_PIN3, 1);
}
}
int main()
{
...
bInit();
bSoftPwmStart(&led1_pwm, PwmHandler1);
bSoftPwmStart(&led2_pwm, PwmHandler2);
...
}