除了开启上述的宏外,还需在board.c 添加PWM参数和PWM设备,如下代码所示(同时最多可以定义3个PWM设备,设备列表的设备名称可以自定义)
PWM频率最高为:LSB时钟/2,当PWM频率设置接近LSB时钟/2时,占空比也无效,设置频率高则占空比小数点point_bit应当设置为0
以下代码PWM0是正反转PWM选择,PWM1是普通定时器PWM选择方法。
//----------------------------------定义PWM设备的参数------------------------------------------//
//例如:PWM0
PWM_PLATFORM_DATA_BEGIN(pwm_data0)
.port = 0,
.pwm_ch = PWMCH0_H | PWMCH0_L,//初始化可选多通道,如:PWMCH0_H | PWMCH0_L | PWMCH1_H ... | PWMCH7_H | PWMCH7_L | PWM_TIMER2_OPCH2 | PWM_TIMER3_OPCH3 ,
.freq = 1000,//频率
.duty = 50,//占空比
.point_bit = 0,//根据point_bit值调节占空比小数点精度位: 0<freq<=4K,point_bit=2;4K<freq<=40K,point_bit=1; freq>40K,point_bit=0;
PWM_PLATFORM_DATA_END()
//例如:PWM1
PWM_PLATFORM_DATA_BEGIN(pwm_data1)
.port = IO_PORTA_03,//选择定时器的TIMER PWM任意IO,pwm_ch加上PWM_TIMER3_OPCH3或PWM_TIMER2_OPCH2有效,只支持2个PWM,占用output_channel2/3,其他外设使用output_channel需留意
.pwm_ch = PWM_TIMER2_OPCH2,//初始化可选多通道,如:PWMCH0_H | PWMCH0_L | PWMCH1_H ... | PWMCH7_H | PWMCH7_L | PWM_TIMER2_OPCH2 | PWM_TIMER3_OPCH3 ,
.freq = 1000,//频率
.duty = 50,//占空比
.point_bit = 0,//根据point_bit值调节占空比小数点精度位: 0<freq<=4K,point_bit=2;4K<freq<=40K,point_bit=1; freq>40K,point_bit=0;
PWM_PLATFORM_DATA_END()
//----------------------------------设备列表添加PWM设备------------------------------------------//
REGISTER_DEVICES(device_table) = {
{"uart2", &uart_dev_ops, (void *)&uart2_data },
{"rtc", &rtc_dev_ops, NULL},
{ "pwm0", &pwm_dev_ops, (void *)&pwm_data0},//添加pwm0子设备,"pwm0"名称可自定义
{ "pwm1", &pwm_dev_ops, (void *)&pwm_data1},//添加pwm1子设备,"pwm1"名称可自定义
};
\