bigbat 发表于 2021-2-10 12:02

【RISC-V MCU CH32V103测评】PWM测试

<p>CH32V103带有PWM输出功能,PWM输出功能在单机控制等应用中十分重要。但是实验结果让我很困惑。</p>

<p>参考例程C:\CH32V103EVT\EVT\EXAM\TIM\PWM_Output的内容,新建了一个项目TIM_PWM</p>

<p>主要初始化程序:</p>

<div aria-label="代码段 小部件" contenteditable="false" role="region" tabindex="-1">
<pre data-widget="codesnippet">
<code class="hljs language-cpp"><span class="hljs-comment">/*******************************************************************************
* Function Name: TIM1_PWMOut_Init
* Description    : Initializes TIM1 PWM output.
* Input          : arr: the period value.
*                  psc: the prescaler value.
*                  ccp: the pulse value.
* Return         : None
*******************************************************************************/</span>
<span class="hljs-keyword">void</span> TIM1_PWMOut_Init( u16 arr, u16 psc, u16 ccp )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &amp;GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &amp;GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &amp;GPIO_InitStructure);

    TIM_TimeBaseInitStructure.TIM_Period = arr;
    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit( TIM1, &amp;TIM_TimeBaseInitStructure);

<span class="hljs-preprocessor">#if (PWM_MODE == PWM_MODE1)</span>
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

<span class="hljs-preprocessor">#elif (PWM_MODE == PWM_MODE2)</span>
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

<span class="hljs-preprocessor">#endif</span>

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = ccp;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC1Init( TIM1, &amp;TIM_OCInitStructure );

    TIM_CtrlPWMOutputs(TIM1, ENABLE );
    TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );
    TIM_ARRPreloadConfig( TIM1, ENABLE );
    TIM_Cmd( TIM1, ENABLE );
}
</code></pre>
<img src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" /><span style="background: url(&quot;https://bbs.eeworld.com.cn/static/editor/plugins/widget/images/handle.png&quot;) rgba(220, 220, 220, 0.5); top: -15px; left: 0px; display: block;"><img height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15" /></span></div>

<p>程序中我加入了两只LED用来做程序指示,然后是PWM主角PA8,这些引脚都被设置为强上拉输出模式。</p>

<p>GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;<br />
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;</p>

<p>引脚的时钟是50MHZ,我不理解的是:<span style="color:#e74c3c;">我都把引脚设成PWM了或是什么其它的功能,你说这个时钟有什么用呢?</span></p>

<p>然后是定时器TIM1的设置,如果不知道为什么是TIM1请参考下图</p>

<p>TIM1在总线APB2上,PA8默认功能是TIM1_CH1(时钟TIM1的通道CH1),程序中选定了引脚PA8就确定了初始化参数。</p>

<p>TIM1_PWMOut_Init(100, 48000-1, 50 );的三个参数,分别是</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr: the period value.&nbsp; //计数基数<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; psc: the prescaler value. //分频系数<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ccp: the pulse value. //占空比</p>

<p>分频系数:48000-1,也就是1KHZ的计数基频,因为48MHZ的频率,每47999个脉冲计数翻转一次。</p>

<p>计数基数:这个是定时器的计数长度(计数基频的数量),注意:计数方向向上。</p>

<p>占空比:输出脉冲的计数时长。要小于计数基数的值,占空比 = ccp/psc</p>

<p>这是我测试的结果:</p>

<p>&nbsp;</p>

<p>看到默认参数的PWM的脉宽为50%的占空比的波形,频率为15HZ!</p>

<p>为什么呀?48M的主频,47999个脉冲计数时钟</p>

<p><span style="font-size:20px;"><span style="color:#e74c3c;">1K的基础时钟,100的计数值,1/1000*100 = 0.1s的周期,怎么是15HZ,有明白的同行给解释一下了</span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

littleshrimp 发表于 2021-4-20 11:58

<p>主频应该是配置成72MHz了</p>
页: [1]
查看完整版本: 【RISC-V MCU CH32V103测评】PWM测试