1.硬件
AB32VG1一共有六组PWM发生器,因为官方给的手册不够全面,因此从代码中进行数量(pwm_config.h)及通道(drv_pwm.c)的分析
pwm_config.h 分析pwm发生器
#ifdef BSP_USING_T3_PWM // timer3 的pwm发生器
#ifndef T3_PWM_CONFIG
#define T3_PWM_CONFIG
{
.pwm_handle = TIM3_BASE,
.name = "t3pwm",
.channel = 0
}
#endif /* T3_PWM_CONFIG /
#endif / BSP_USING_T3_PWM /
#ifdef BSP_USING_T4_PWM // timer4 的pwm发生器
#ifndef T4_PWM_CONFIG
#define T4_PWM_CONFIG
{
.pwm_handle = TIM4_BASE,
.name = "t4pwm",
.channel = 0
}
#endif / T4_PWM_CONFIG /
#endif / BSP_USING_T4_PWM /
#ifdef BSP_USING_T5_PWM // timer5 的pwm发生器
#ifndef T5_PWM_CONFIG
#define T5_PWM_CONFIG
{
.pwm_handle = TIM5_BASE,
.name = "t5pwm",
.channel = 0
}
#endif / T5_PWM_CONFIG /
#endif / BSP_USING_T5_PWM /
#ifdef BSP_USING_LPWM0 // pwm 特殊发生器0
#ifndef LPWM0_CONFIG
#define LPWM0_CONFIG
{
.pwm_handle = PWM_BASE,
.name = "lpwm0",
.channel = 0
}
#endif / LPWM0_CONFIG /
#endif / BSP_USING_LPWM0 /
#ifdef BSP_USING_LPWM1 // pwm 特殊发生器1
#ifndef LPWM1_CONFIG
#define LPWM1_CONFIG
{
.pwm_handle = PWM_BASE,
.name = "lpwm1",
.channel = 0
}
#endif / LPWM1_CONFIG /
#endif / BSP_USING_LPWM1 /
#ifdef BSP_USING_LPWM2 // pwm 特殊发生器2
#ifndef LPWM2_CONFIG
#define LPWM2_CONFIG
{
.pwm_handle = PWM_BASE,
.name = "lpwm2",
.channel = 0
}
#endif / LPWM2_CONFIG /
#endif / BSP_USING_LPWM2 /
#ifdef BSP_USING_LPWM3 // pwm 特殊发生器3
#ifndef LPWM3_CONFIG
#define LPWM3_CONFIG
{
.pwm_handle = PWM_BASE,
.name = "lpwm3",
.channel = 0
}
#endif / LPWM3_CONFIG /
#endif / BSP_USING_LPWM3 */
static void pwm_get_channel(void)
{
#ifdef BSP_USING_T3_PWM0 // timer3 pwm 0通道 -》 PB0
ab32_pwm_obj[T3_PWM_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_T3_PWM1 // timer3 pwm 1通道
ab32_pwm_obj[T3_PWM_INDEX].channel |= 1 << 1;
#endif
#ifdef BSP_USING_T3_PWM2 // timer3 pwm 2通道
ab32_pwm_obj[T3_PWM_INDEX].channel |= 1 << 2;
#endif
#ifdef BSP_USING_T4_PWM0 // timer4 pwm 0通道
ab32_pwm_obj[T4_PWM_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_T4_PWM1 // timer4 pwm 1通道 -》 PA6
ab32_pwm_obj[T4_PWM_INDEX].channel |= 1 << 1;
#endif
#ifdef BSP_USING_T4_PWM2 // timer4 pwm 2通道
ab32_pwm_obj[T4_PWM_INDEX].channel |= 1 << 2;
#endif
#ifdef BSP_USING_T5_PWM0 // timer5 pwm 0通道 -》 PE1
ab32_pwm_obj[T5_PWM_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_T5_PWM1 // timer5 pwm 1通道
ab32_pwm_obj[T5_PWM_INDEX].channel |= 1 << 1;
#endif
#ifdef BSP_USING_T5_PWM2 // timer5 pwm 2通道
ab32_pwm_obj[T5_PWM_INDEX].channel |= 1 << 2;
#endif
#ifdef BSP_USING_LPWM0 // lpwm0 pwm 0通道 ——》 PE4(G1) G1,G2.G3之间相互互斥
ab32_pwm_obj[LPWM0_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_LPWM1 // lpwm1 pwm 0通道, -》 PA1(G3)
ab32_pwm_obj[LPWM1_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_LPWM2 // lpwm2 pwm 0通道,-》PE0(G2)/PA2(G3)
ab32_pwm_obj[LPWM2_INDEX].channel |= 1 << 0;
#endif
#ifdef BSP_USING_LPWM3 // lpwm3 pwm 0通道
ab32_pwm_obj[LPWM3_INDEX].channel |= 1 << 0;
#endif
}
板载RGB的三个引脚,都可以做PWM输出引脚用。因此本次采用PE1进行PWM实验。
2.软件
#define PWM_DEV_NAME "t5pwm" /* PWM设备名称 这个命名与官方例程不一样,要注意!!! /
#define PWM_DEV_CHANNEL 1 / PWM通道 */
struct rt_device_pwm pwm_dev; / PWM设备句柄 */
ALIGN(RT_ALIGN_SIZE)
static uint8_t PWM_Thread_Stack[1024];
static void PWM_Thread_Entry(void para);
static struct rt_thread pwm_thread;
rt_uint32_t period, pulse;
void Pwm_Init(void){
period = 1000; / 周期 /
pulse = 0; / PWM脉冲宽度值(0 - 1000) */
pwm_dev = (struct rt_device_pwm )rt_device_find(PWM_DEV_NAME);
RT_ASSERT(pwm_dev != RT_NULL);
/ 设置PWM周期和脉冲宽度 /
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/ 使能设备 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
}
static void PWM_Thread_Entry(void *para){
Pwm_Init();
while(1){
for (int var = 0; var < period; var += 100) {
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, var);
rt_thread_mdelay(1000);
}
}
}
int Pwm_Thread_Init(void){
rt_thread_init(&pwm_thread, "pwm_thread", PWM_Thread_Entry, RT_NULL,
&PWM_Thread_Stack[0], sizeof(PWM_Thread_Stack), 10, 10);
rt_thread_startup(&pwm_thread);
return 0;
}
INIT_APP_EXPORT(Pwm_Thread_Init);
本次实验可以看到板载RGB灯发出红光,且从亮到暗进行变换。
原作者:JiangYangJie
更多回帖