leekom 发表于 2024-8-14 19:56

HXX320中pwm的占空比设置

<p>&nbsp; &nbsp; &nbsp; &nbsp;HXX320和STM32有不同,要配置可定义的PWM,其完成是内部分硬件方式。只需对相关的硬件设定,其DSP将协同处理。</p>

<p>&nbsp; &nbsp; &nbsp; 首先,初始化完成后,确定相关的设定库。</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp;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>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp;此例,假如ARR值为0,即0.0001*(0+1),则输出PWM频率为10KHz,再假如输出频率为100Hz的PWM,则将ARR寄存器设置为99即可。如果想调整PWM占空比精度,则只需降低PSC寄存器的值即可。设为100TIM_SetCompare2(20),就是20%占空比了。</p>

<p>&nbsp;</p>

<p>相关代码下:</p>

<p>&nbsp;</p>

<p>void TIM_Configuration(void)//TIM初始化函数<br />
{<br />
&nbsp; TIM_TimeBaseInitTypeDef&nbsp; TIM_TimeBaseStructure;//定时器初始化结构<br />
&nbsp; TIM_OCInitTypeDef&nbsp; TIM_OCInitStructure;//通道输出初始化结构</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp;//TIM1初始化<br />
&nbsp; TIM_DeInit(TIM2);<br />
&nbsp; TIM_TimeBaseStructure.TIM_Period = 719;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //周期719<br />
&nbsp; TIM_TimeBaseStructure.TIM_Prescaler = 999;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //时钟分频&nbsp;&nbsp;<br />
&nbsp; TIM_TimeBaseStructure.TIM_ClockDivision = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//时钟分割点<br />
&nbsp; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//PWM模式<br />
&nbsp; TIM_TimeBaseInit(TIM2, &amp;TIM_TimeBaseStructure);&nbsp;&nbsp; //基本初始化<br />
&nbsp; // TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//打开中断,中断需要这行代码<br />
&nbsp;<br />
&nbsp; &nbsp;//TIM1通道初始化<br />
&nbsp; TIM_OCStructInit(&amp; TIM_OCInitStructure);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //默认参数<br />
&nbsp; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //工作状态,;//配置为PWM1模式<br />
&nbsp; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //设定为输出,需要PWM输出才需要这行代码<br />
&nbsp; TIM_OCInitStructure.TIM_Pulse = 800;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //占空长度<br />
&nbsp; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //高电平<br />
&nbsp; TIM_OC4Init(TIM2, &amp;TIM_OCInitStructure);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//使用P4脚<br />
&nbsp; TIM_Cmd(TIM2, ENABLE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//启动TIM1<br />
}</p>

<p>输出的信号周期为5ms,单个周期内高电平持续时间为0.7ms; 见图</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2024-8-15 07:28

<p>占空比设置 效果看得见,挺好</p>

nmg 发表于 2024-8-15 09:30

<p>HXX320是谁家的单片机?</p>
页: [1]
查看完整版本: HXX320中pwm的占空比设置