最近呢由于一个项目需要开发需要使用操作系统后面呢经过讨论使用了国产的RTT操作系统
使用RTT 这款实时操作系统。主要是因为RTT团队对公司有技术支持
当然还有一个最重要的原因 国产!!!!!!!!!!!!!
废话就讲这么多吧
实现功能:
关键词:STM32 F1单片机
HAL库
RTT 实现
PWM输出和采样
使用了操作系统以后一定要注意底层和应用层的区分尽量不要在应用层中去调用底层的函数。如果有太多的跨层操作在后面代码不方便查看十分不方便移植,为了提高自己的工作效率在编写代码的时候尽量做到写一次终生受用。
#include
#include "drv_gpio.h"
#include
/*************************************************
Function: SetHBridgePWMValue // 函数名称
Description: 设置H桥波特率 // 函数功能
Input: pwmx : pwm频道 // 输入参数
channel : pwm通道
Return: 无 // 返回说明
Others: 无 // 其它说明
period : 频率
pulse : 几分频
Output: 无 // 输出说明
*************************************************/
rt_err_t SetHBridgePWMValue(rt_uint32_t channel,char* pwmx,rt_uint32_t period,rt_uint32_t pulse) //ConfigurePwm_UserTab[]
{
//int len=sizeof(ConfigurePwm_UserTab)/sizeof(ConfigurePwm_UserTab[0]);
//rt_kprintf("%d \n",len);
rt_uint32_t pulse1;
rt_err_t result = RT_EOK;
rt_device_t pwm_device = rt_device_find(pwmx);
if(pwm_device != RT_NULL)
{
rt_device_open(pwm_device, RT_DEVICE_OFLAG_RDWR); //打开硬件
rt_kprintf("rt_device_open(pwm_device, RT_DEVICE_OFLAG_RDWR) \n");
}
struct rt_pwm_configuration configuration =
{
.channel = channel,
.period = period, /* 1ms ==> 1Khz */
.pulse = pulse /* 50% duty */
};
if(!(rt_device_write(pwm_device, channel, &configuration, sizeof(struct rt_pwm_configuration))))
{
rt_kprintf("write pwm channel %d: faild! \n", channel);
result = -RT_ERROR;
goto _exit;
}
if(rt_device_control(pwm_device, PWM_CMD_SET, &configuration))
{
rt_kprintf("rt_device_control PWM_CMD_SET channel %d: faild! \n", channel);
result = -RT_ERROR;
goto _exit;
}
if(rt_device_control(pwm_device, PWM_CMD_ENABLE, &configuration))
{
rt_kprintf("rt_device_control PWM_CMD_ENABLE channel %d: faild! \n", channel);
result = -RT_ERROR;
goto _exit;
}
if(rt_device_read(pwm_device,channel,&pulse1, sizeof(rt_uint32_t)) != sizeof(rt_uint32_t))
{
rt_kprintf("read pwm channel %d: faild! \n", channel);
result = -RT_ERROR;
goto _exit;
}
if(pulse == pulse1)
{
rt_kprintf("readback pwm channel %d: OK! \n", channel);
}
else
{
rt_kprintf("readback pwm channel %d: faild! \n", pulse1);
}
_exit:
return result;
}
这个封装函完全使用的RTT系统的应用层函数接口基本脱离了与底层的关系
在定义一个PWM输出时主要步骤有
1.rt_device_t pwm_device = rt_device_find(pwmx); 赋值对象
2.rt_device_open(pwm_device, RT_DEVICE_OFLAG_RDWR); //打开硬件
3. struct rt_pwm_configuration configuration =
{
.channel = channel,
.period = period, /* 1ms ==> 1Khz */
.pulse = pulse /* 50% duty */
} //配置参数
4.rt_device_write(pwm_device, channel, &configuration, sizeof(struct rt_pwm_configuration)) 参数写入
5.rt_device_control(pwm_device, PWM_CMD_SET, &configuration) SET命令执行
6.rt_device_control(pwm_device, PWM_CMD_ENABLE, &configuration) 打开使能
我的函数在后面加了一个read函数用来读取PWM的脉宽
用这个脉宽的读取数来和我的写入数值作对比达到一个校验是否配置成功的作用;