软件PWM 模块分享

前言

        做项目中遇到很多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算法使用;非常方便;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值