概述
本章配置GD32F303輸出PWM,同時(shí)使用TIM測(cè)量PWM頻率和正占空比。 查閱手冊(cè)可以得知,PB11為定時(shí)器1的通道3,讓其輸出PWM,PA6為定時(shí)器2的通道0,讓作為TIM定時(shí)器輸入。 需要GD樣片的可以加群申請(qǐng):615061293 。
生成例程
這里準(zhǔn)備了自己繪制的開發(fā)板進(jìn)行驗(yàn)證。
管腳圖如下所示。
keil配置
microlib 進(jìn)行了高度優(yōu)化以使代碼變得很小。 它的功能比缺省 C 庫(kù)少,并且根本不具備某些 ISO C 特性。 某些庫(kù)函數(shù)的運(yùn)行速度也比較慢,如果要使用printf(),必須開啟。
使能串口
/* 使能GPI0A,用PA9、PA10為串口 */
rcu_periph_clock_enable(RCU_GPIOA);
/*使能串口0的時(shí)鐘 */
rcu_periph_clock_enable(RCU_USART0);
/*配置USARTx_Tx(PA9)為復(fù)用推挽輸出*/
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/*配置USARTx_RxPA9)為浮空輸入 */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART 配置 */
usart_deinit(USART0);//重置串口0
usart_baudrate_set(USART0, 115200U);//設(shè)置串口0的波特率為115200
usart_word_length_set(USART0, USART_WL_8BIT); // 幀數(shù)據(jù)字長(zhǎng)
usart_stop_bit_set(USART0, USART_STB_1BIT); // 停止位1位
usart_parity_config(USART0, USART_PM_NONE); // 無奇偶校驗(yàn)位
usart_receive_config(USART0, USART_RECEIVE_ENABLE);//使能接收器
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);//使能發(fā)送器
usart_enable(USART0);//使能USART
串口重定向
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t)ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
串口重定向后就可以使用printf進(jìn)行打印。
占空比與頻率計(jì)算
占空比=(t1-t0)/(t2-t0) 頻率=(t2-t0)/時(shí)鐘頻率= =(t2-t0)/(120M/(psc+1))
周期需要2個(gè)上升沿去判斷,設(shè)定第一個(gè)上升沿time_flag由0->1,下降沿time_dowm_flag由0->1,此時(shí)就知道正占空比時(shí)間,當(dāng)在產(chǎn)生上升沿時(shí)候,就可以計(jì)算出周期使用的時(shí)間。
GPIO初始化
/*!
rief configure the GPIO ports
param[in] none
param[out] none
etval none
*/
void gpio_configuration(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
/*configure PA6 (TIMER2 CH0) as alternate function*/
gpio_init(GPIOA,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
//TIMER1-CH3
gpio_pin_remap_config(GPIO_TIMER1_PARTIAL_REMAP1, ENABLE);
gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_11);
}
開啟中斷
/*!
rief configure the nested vectored interrupt controller
param[in] none
param[out] none
etval none
*/
void nvic_configuration(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER2_IRQn, 1, 1);
}
TIM1輸出PWM初始化
PWM頻率計(jì)算如下所示。
void timer1_config(void)
{
/* -----------------------------------------------------------------------
TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
TIMER1CLK = SystemCoreClock / 120 = 1MHz
TIMER1 channel0 duty cycle = (4000/ 16000)* 100 = 25%
TIMER1 channel1 duty cycle = (8000/ 16000)* 100 = 50%
TIMER1 channel2 duty cycle = (12000/ 16000)* 100 = 75%
----------------------------------------------------------------------- */
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
/* TIMER1 configuration */
timer_initpara.prescaler = 119;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 1000;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1,&timer_initpara);
/* CH0,CH1 and CH2 configuration in PWM mode */
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
/* CH3 configuration in PWM mode0,duty cycle 50% */
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,500);
timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER1);
/* auto-reload preload enable */
timer_enable(TIMER1);
}
TIM2輸入捕獲設(shè)置
void timer2_config(void)
{
/* TIMER2 configuration: input capture mode -------------------
the external signal is connected to TIMER2 CH0 pin (PB4)
the rising edge is used as active edge
the TIMER2 CH0CV is used to compute the frequency value
------------------------------------------------------------ */
timer_ic_parameter_struct timer_icinitpara;
timer_parameter_struct timer_initpara;
//開啟定時(shí)器時(shí)鐘
rcu_periph_clock_enable(RCU_TIMER2);
timer_deinit(TIMER2);
/* TIMER2 configuration */
timer_initpara.prescaler = 120-1;//定時(shí)器的時(shí)鐘頻率是120MHz,預(yù)分頻120-1
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;//對(duì)齊模式
timer_initpara.counterdirection = TIMER_COUNTER_UP;//向上計(jì)數(shù)
timer_initpara.period = 65535;//重載值
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;//不分頻
timer_initpara.repetitioncounter = 0;//重復(fù)計(jì)數(shù)
timer_init(TIMER2,&timer_initpara);
/* TIMER2 configuration */
/* TIMER2 CH0 input capture configuration */
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;//捕獲極性,上升沿捕獲
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;//通道輸入模式選擇
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;//分頻
timer_icinitpara.icfilter = 0x0;//濾波
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER2);//自動(dòng)重載使能
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);//CH0 通道中斷清除
/* channel 0 interrupt enable */
timer_interrupt_enable(TIMER2,TIMER_INT_CH0);//CH0 通道中斷使能
/* TIMER2 counter enable */
timer_enable(TIMER2);
}
中斷
#define IR_IN1 gpio_input_bit_get (GPIOA, GPIO_PIN_6)
uint8_t time_up_flag=0;//上升沿標(biāo)志位
uint8_t time_dowm_flag=0;//下降沿標(biāo)志位
uint32_t time_up_num=0;//上升沿計(jì)數(shù)
uint32_t time_dowm_num=0;//下降沿計(jì)數(shù)
float time_frequency;//頻率
float time_duty;//占空比
void TIMER2_IRQHandler(void)
{
timer_ic_parameter_struct timer_icinitpara;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x0;
if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_CH0)){
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);
if(IR_IN1&&time_up_flag==0)//第一次上升
{
time_up_flag=1;
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING; //設(shè)置為下降沿
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
timer_counter_value_config(TIMER2 , 0); // 計(jì)數(shù)清零,從頭開始計(jì)
}
else if(IR_IN1==0&&time_dowm_flag==0)//下降
{
time_dowm_num = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0)+1; // 讀取捕獲計(jì)數(shù),這個(gè)時(shí)間即為上升沿持續(xù)的時(shí)間
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING; //設(shè)置為上升沿
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
time_dowm_flag=1;
}
else if(IR_IN1&&time_dowm_flag==1)//第二次之后上升
{
time_up_num = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_0)+1;; // 讀取捕獲計(jì)數(shù),這個(gè)時(shí)間即為上升沿持續(xù)的時(shí)間
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING; //設(shè)置為下降沿
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
time_dowm_flag=0;
timer_counter_value_config(TIMER2 , 0); // 計(jì)數(shù)清零,從頭開始計(jì)
}
}
初始化
gpio_configuration();
nvic_configuration();
timer1_config();
timer2_config();
主程序
while (1)
{
time_frequency=1000000/time_up_num;//頻率
time_duty = (float)time_dowm_num/(float)time_up_num;//占空比
printf("
time_frequency=%.2f,time_duty=%.2f",time_frequency,time_duty*100) ;
delay_1ms(1000);
}
測(cè)試結(jié)果
當(dāng)輸出1k頻率,50%正占空比。
審核編輯:湯梓紅
-
PWM
+關(guān)注
關(guān)注
114文章
5118瀏覽量
213157 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3231瀏覽量
114325 -
Tim
+關(guān)注
關(guān)注
0文章
81瀏覽量
17856 -
固件庫(kù)
+關(guān)注
關(guān)注
2文章
97瀏覽量
14918 -
gd32f303
+關(guān)注
關(guān)注
4文章
38瀏覽量
3668
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論