關於stm32定時器的理解

2021-09-22 13:47:00 字數 3774 閱讀 8709

tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high;

表面意思是輸出控制極性為高,但是意思是定時器輸入0,不反相,輸出0;

輸出控制極性為低,定時器輸入0,反相輸出1;

cc1e控制開關。

#define tim_ocpolarity_high                ((uint16_t)0x0000)

#define tim_ocpolarity_low                 ((uint16_t)0x0002)

定時器通道寫法: tim_oc2 意思是定時器channel 2 ,tim_oc1意思是定時器通道1.

//小波linux

pwm占空比:

tim_setcompare1(tim2,led1pwmval);  

tim_setcompare2(tim3,led0pwmval);  

timx為哪個定時器,ledxpwmval為占空比的值;

tim2_pwm_init(899,0);  //不分頻 72000/900=8khz

arr 自動重裝載計數器初值 900 =899+1;

占空比例如 佔 50% = 450 / 900

ledxpwmval = 450;

定時器初始化,裡面有個重對映

void tim2_pwm_init(u16 arr,u16 psc) {   

gpio_inittypedef gpio_initstructure;  tim_timebaseinittypedef  tim_timebasestructure;  tim_ocinittypedef  tim_ocinitstructure;  

rcc_apb1periphclockcmd(rcc_apb1periph_tim2, enable); //使能定時器3時鐘   rcc_apb2periphclockcmd(rcc_apb2periph_gpioa,enable);  //使能gpio外設和afio復用功能模組時鐘      //設定該引腳為復用輸出功能,輸出tim2 ch1的pwm脈衝波形  pa0  gpio_initstructure.gpio_pin = gpio_pin_0; //tim_ch1  gpio_initstructure.gpio_mode = gpio_mode_af_pp;  //復用推挽輸出  gpio_initstructure.gpio_speed = gpio_speed_50mhz;  gpio_init(gpioa, &gpio_initstructure);//初始化gpio      //初始化tim2  tim_timebasestructure.tim_period = arr;    //設定在下乙個更新事件裝入活動的自動重裝載暫存器週期的值  tim_timebasestructure.tim_prescaler =psc;  //設定用來作為timx時鐘頻率除數的預分頻值  tim_timebasestructure.tim_clockdivision = 0; //設定時鐘分割:tdts = tck_tim  tim_timebasestructure.tim_countermode = tim_countermode_up;  //tim向上計數模式  tim_timebaseinit(tim2, &tim_timebasestructure); //根據tim_timebaseinitstruct中指定的引數初始化timx的時間基數單位    //初始化tim2 channel 1 pwm模式   tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm2;             //選擇定時器模式:tim脈衝寬度調製模式2   tim_ocinitstructure.tim_outputstate = tim_outputstate_enable; //比較輸出使能  tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high;     //輸出極性:tim輸出比較極性高  tim_oc1init(tim2, &tim_ocinitstructure);                     //根據t指定的引數初始化外設tim2 oc1

tim_oc1preloadconfig(tim2, tim_ocpreload_enable);  //使能tim2在ccr1上的預裝載暫存器    tim_cmd(tim2, enable);  //使能tim2  

//tim3 pwm部分初始化 //pwm輸出初始化 //arr:自動重灌值 //psc:時鐘預分頻數 void tim3_pwm_init(u16 arr,u16 psc) {   gpio_inittypedef gpio_initstructure;  tim_timebaseinittypedef  tim_timebasestructure;  tim_ocinittypedef  tim_ocinitstructure;  rcc_apb1periphclockcmd(rcc_apb1periph_tim3, enable); //使能定時器3時鐘   rcc_apb2periphclockcmd(rcc_apb2periph_gpiob  | rcc_apb2periph_afio, enable);  //使能gpio外設和afio復用功能模組時鐘    gpio_pinremapconfig(gpio_partialremap_tim3, enable); //timer3部分重對映  tim3_ch2->pb5         //設定該引腳為復用輸出功能,輸出tim3 ch2的pwm脈衝波形 gpiob.5  gpio_initstructure.gpio_pin = gpio_pin_5; //tim_ch2  gpio_initstructure.gpio_mode = gpio_mode_af_pp;  //復用推挽輸出  gpio_initstructure.gpio_speed = gpio_speed_50mhz;  gpio_init(gpiob, &gpio_initstructure);//初始化gpio      //初始化tim3  tim_timebasestructure.tim_period = arr; //設定在下乙個更新事件裝入活動的自動重裝載暫存器週期的值  tim_timebasestructure.tim_prescaler =psc; //設定用來作為timx時鐘頻率除數的預分頻值  tim_timebasestructure.tim_clockdivision = 0; //設定時鐘分割:tdts = tck_tim  tim_timebasestructure.tim_countermode = tim_countermode_up;  //tim向上計數模式  tim_timebaseinit(tim3, &tim_timebasestructure); //根據tim_timebaseinitstruct中指定的引數初始化timx的時間基數單位    //初始化tim3 channel2 pwm模式   tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm2; //選擇定時器模式:tim脈衝寬度調製模式2   tim_ocinitstructure.tim_outputstate = tim_outputstate_enable; //比較輸出使能  tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high; //輸出極性:tim輸出比較極性高  tim_oc2init(tim3, &tim_ocinitstructure);  //根據t指定的引數初始化外設tim3 oc2

tim_oc2preloadconfig(tim3, tim_ocpreload_enable);  //使能tim3在ccr2上的預裝載暫存器    tim_cmd(tim3, enable);  //使能tim3  

關於stm32定時器的理解

tim ocinitstructure.tim ocpolarity tim ocpolarity high 表面意思是輸出控制極性為高,但是意思是定時器輸入0,不反相,輸出0 輸出控制極性為低,定時器輸入0,反相輸出1 cc1e控制開關。define tim ocpolarity high uin...

stm32關定時器 STM32定時器TIM3程式

include led.h include delay.h include key.h include sys.h include usart.h include timer.h stm32開發板實驗8 定時器中斷實驗 int main void delay init 延時函式初始化 nvic co...

STM32關於Systick定時器

delay ms和delay us支援 os 需要用到的三個巨集定義 值為 1,當 os 還未執行時,該巨集定義值為 0。delay ostickspersec,用於表示 os 的時鐘節拍,即 os 每秒鐘任務排程次數。delay osintnesting,用於表示 os 中斷巢狀級別,即中斷巢狀次...