這是用stm32的timer2的通道3和通道4同事捕獲pwm訊號,顯示高電平的時間,在keil4.7中編譯。
tim_icinittypedef tim2_icinitstructure;
/* ** ¶¨ê±æ÷2í¨µà1êäè벶»ñåäöã
*/
void tim2_cap_init(u16 arr, u16 psc)
/* ** ¶¨ê±æ÷2öð¶ï·þîñ³ìðò
*/
void tim2_irqhandler(void)
else
tim2ch3_capture_sta++;
}
} if(tim_getitstatus(tim2, tim_it_cc3) != reset) // ²¶»ñ1·¢éú²¶»ñêâ¼þ
else /* »¹î´¿ªê¼µúò»´î²¶»ñéïéýñø */
} }
if((tim2ch4_capture_sta & 0x80) == 0) // »¹î´³é¹¦²¶»ñ
else
tim2ch4_capture_sta++;
}
} if(tim_getitstatus(tim2, tim_it_cc4) != reset) // ²¶»ñ1·¢éú²¶»ñêâ¼þ
else /* »¹î´¿ªê¼µúò»´î²¶»ñéïéýñø */
} }
tim_clearitpendingbit(tim2, tim_it_cc3|tim_it_cc4|tim_it_update); // çå³ýöð¶ï±êö¾î»
} //主函式u8 tim2ch3_capture_sta = 0; // êäè벶»ñ×´ì¬
u16 tim2ch3_capture_val; // êäè벶»ñöµ
u8 tim2ch4_capture_sta = 0; // êäè벶»ñ×´ì¬
u16 tim2ch4_capture_val; // êäè벶»ñöµ
void tim2_cap_init(u16 arr,u16 psc);
int main(void)
if(tim2ch4_capture_sta&0x80)//³é¹¦²¶»ñµ½áëò»´îéïéýñø
stdelay_ms(100);
}}
在測試程式的時候遇到了乙個問題,在tim2_cap_init() 函式中,如果用
tim2_icinitstructure.tim_channel = tim_channel_3 | tim_channel_4;
tim2_icinitstructure.tim_icpolarity = tim_icpolarity_rising;
tim2_icinitstructure.tim_icselection = tim_icselection_directti;
tim2_icinitstructure.tim_icprescaler = tim_icpsc_div1;
tim2_icinitstructure.tim_icfilter = 0x00;
tim_icinit(tim2, &tim2_icinitstructure);
來代替下面的初始化程式程式會卡在乙個地方出不來或者執行的有問題,不知道怎麼回事,當時也沒有深究。。。。
tim2_icinitstructure.tim_channel = tim_channel_3
tim2_icinitstructure.tim_icpolarity = tim_icpolarity_rising;
tim2_icinitstructure.tim_icselection = tim_icselection_directti;
tim2_icinitstructure.tim_icprescaler = tim_icpsc_div1;
tim2_icinitstructure.tim_icfilter = 0x00;
tim_icinit(tim2, &tim2_icinitstructure);
tim2_icinitstructure.tim_channel = tim_channel_4;
tim2_icinitstructure.tim_icpolarity = tim_icpolarity_rising;
tim2_icinitstructure.tim_icselection = tim_icselection_directti;
tim2_icinitstructure.tim_icprescaler = tim_icpsc_div1;
tim2_icinitstructure.tim_icfilter = 0x00;
tim_icinit(tim2, &tim2_icinitstructure);
STM32單個定時器四通道輸入捕獲
定時器2通道1輸入捕獲配置 arr 自動重灌值 psc 時鐘預分頻數 void tim2 cap init u16 arr,u16 psc 捕獲狀態 7 0,沒有成功的捕獲 1,成功捕獲到一次.6 0,還沒捕獲到高電平 1,已經捕獲到高電平了.5 0 捕獲高電平後溢位的次數 u8 tim2ch1 c...
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的定時器
stm32一共有8個都為16位的定時器。其中tim6 tim7是基本定時器 tim2 tim3 tim4 tim5是通用定時器 tim1和tim8是高階定時器。1.基本定時器 基本定時器tim6和tim7只具備最基本的定時功能,就是累加的時鐘脈衝數超過預定值時,能觸發中斷或觸發dma請求。由於在晶元...