HXX320中pwm的占空比设置
<p> HXX320和STM32有不同,要配置可定义的PWM,其完成是内部分硬件方式。只需对相关的硬件设定,其DSP将协同处理。</p><p> 首先,初始化完成后,确定相关的设定库。</p>
<p> </p>
<p> TIM_TimeBaseStructure.TIM_Period设为TIMx的时钟,除非APB1的时钟分频数设置为1,否则通用定时器TIMx的时钟是APB1时钟的2倍,这时的TIMx时钟为72MHz,用这个TIMx时钟72MHz除以(PSC+1),得到定时器每隔多少秒涨一次,这里给PSC赋7199,计算得定时器每隔0.0001秒涨一次,即此时频率为10KHz,再把这个值乘以(ARR+1)得出PWM频率。</p>
<p> </p>
<p> 此例,假如ARR值为0,即0.0001*(0+1),则输出PWM频率为10KHz,再假如输出频率为100Hz的PWM,则将ARR寄存器设置为99即可。如果想调整PWM占空比精度,则只需降低PSC寄存器的值即可。设为100TIM_SetCompare2(20),就是20%占空比了。</p>
<p> </p>
<p>相关代码下:</p>
<p> </p>
<p>void TIM_Configuration(void)//TIM初始化函数<br />
{<br />
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定时器初始化结构<br />
TIM_OCInitTypeDef TIM_OCInitStructure;//通道输出初始化结构</p>
<p> </p>
<p> //TIM1初始化<br />
TIM_DeInit(TIM2);<br />
TIM_TimeBaseStructure.TIM_Period = 719; //周期719<br />
TIM_TimeBaseStructure.TIM_Prescaler = 999; //时钟分频 <br />
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割点<br />
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//PWM模式<br />
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化<br />
// TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE); //打开中断,中断需要这行代码<br />
<br />
//TIM1通道初始化<br />
TIM_OCStructInit(& TIM_OCInitStructure); //默认参数<br />
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //工作状态,;//配置为PWM1模式<br />
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //设定为输出,需要PWM输出才需要这行代码<br />
TIM_OCInitStructure.TIM_Pulse = 800; //占空长度<br />
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //高电平<br />
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //使用P4脚<br />
TIM_Cmd(TIM2, ENABLE); //启动TIM1<br />
}</p>
<p>输出的信号周期为5ms,单个周期内高电平持续时间为0.7ms; 见图</p>
<p> </p>
<p> </p>
<p>占空比设置 效果看得见,挺好</p>
<p>HXX320是谁家的单片机?</p>
页:
[1]