前言
做项目中遇到很多IO类输出要类似PWM方式控制,网上找了一圈有这方面的轮子,但是和硬件耦合性太大,于是自己基于BabyOS造了一个纯软件的PWM模块;
code
/**
* \defgroup PWM_Exported_Defines
* \{
*/
#define bPWM_INSTANCE(name, _period, _ccr, _repeat) \
bSoftPwmInstance_t name = {.period = _period,.ccr = _ccr, .repeat = _repeat};
/**
* \}
*/
/**
* \defgroup PWM_Exported_Functions
* \{
*/
///< pPWMInstance \ref bPWM_INSTANCE
int bSoftPwmStart(bSoftPwmInstance_t *pPwmInstance, pPwmHandler handler1, pPwmHandler handler2);
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);
使用
void soft_pwm_test1()
{
b_log("soft_pwm_test1 \r\n");
}
void soft_pwm_test2()
{
b_log("soft_pwm_test2 \r\n");
}
bPWM_INSTANCE(pwm1,100,50,0);
bSoftPwmStart(&pwm1, soft_pwm_test1, soft_pwm_test2);
总结
这个模块可以用于IO 多实例化输出PWM,PWM可以设置输出次数;可以配合PID算法使用;非常方便;