STM8S 定時(shí)器1 輸入捕獲
然后下面是代碼:
C語(yǔ)言:Codee#11818
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 實(shí)驗(yàn)平臺(tái): STM8S103F2P6 FM 接收實(shí)驗(yàn)核心板
+ 硬件 : STM8S103F2P6
+ 開(kāi)發(fā)平臺(tái): IAR For STM8 1.10
+ 仿真器 : ST-Link
+ 日期 : 2010-6-28
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "stm8s.h"
u16Get_time;
/*******************************************************************************
* ==啟用外部晶振函數(shù)==
*******************************************************************************/
voidHSE_CLK_INIT(void)
{
CLK_DeInit();
CLK_HSECmd(ENABLE);
while(SET!=CLK_GetFlagStatus(CLK_FLAG_HSERDY));
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchCmd(ENABLE);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSE,DISABLE,CLK_CURRENTCLOCKSTATE_ENABLE);
}
//==============================================================================
// ¤¤¤ 定時(shí)器1初始化 ¤¤¤
// --> 定時(shí)器1 輸入捕獲通道2 PortC 7
// TIM1_DeInit(); --> 定時(shí)器1 寄存器復(fù)位
// TIM1_ICInit( TIM1_CHANNEL_2, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI , TIM1_ICPSC_DIV8, 0x0);
// --> 輸入捕獲CH2 , 下降沿捕獲 , 輸入捕捉源為直接中斷方式 , 輸入捕捉預(yù)分頻 , 輸入捕捉濾波器值
// TIM1_ITConfig( TIM1_IT_CC2 , ENABLE); --> 開(kāi)啟通道二輸入捕獲中斷使能
// TIM1_ClearFlag(TIM1_FLAG_CC2); --> 清除輸入捕獲中斷標(biāo)志位
// TIM1_Cmd(ENABLE); --> 使能定時(shí)器1
//==============================================================================
voidTIME1_INIT(void)
{
TIM1_DeInit();
TIM1_ICInit(TIM1_CHANNEL_2,TIM1_ICPOLARITY_FALLING,TIM1_ICSELECTION_DIRECTTI,TIM1_ICPSC_DIV8,0x0);
TIM1_ITConfig(TIM1_IT_CC2,ENABLE);
TIM1_ClearFlag(TIM1_FLAG_CC2);
TIM1_Cmd(ENABLE);
}
/*******************************************************************************
* ==Main函數(shù)==
*******************************************************************************/
voidmain(void)
{
HSE_CLK_INIT();// 外部時(shí)鐘初始化
TIME1_INIT();// 定時(shí)器1 輸入捕獲初始化
enableInterrupts();// 開(kāi)總中斷
while(1);
}
//==============================================================================
// ¤¤¤ 中斷服務(wù)函數(shù) ¤¤¤
// --> 定時(shí)器1 捕獲/比較 中斷向量
//==============================================================================
#pragma vector=0xE
__interruptvoidTIM1_CAP_COM_IRQHandler(void)
{
Get_time=TIM1_GetCapture2();// 獲得捕獲時(shí)間
TIM1_ClearFlag(TIM1_FLAG_CC2);// 清標(biāo)志位
}
+ 實(shí)驗(yàn)平臺(tái): STM8S103F2P6 FM 接收實(shí)驗(yàn)核心板
+ 硬件 : STM8S103F2P6
+ 開(kāi)發(fā)平臺(tái): IAR For STM8 1.10
+ 仿真器 : ST-Link
+ 日期 : 2010-6-28
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "stm8s.h"
u16Get_time;
/*******************************************************************************
* ==啟用外部晶振函數(shù)==
*******************************************************************************/
voidHSE_CLK_INIT(void)
{
CLK_DeInit();
CLK_HSECmd(ENABLE);
while(SET!=CLK_GetFlagStatus(CLK_FLAG_HSERDY));
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchCmd(ENABLE);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSE,DISABLE,CLK_CURRENTCLOCKSTATE_ENABLE);
}
//==============================================================================
// ¤¤¤ 定時(shí)器1初始化 ¤¤¤
// --> 定時(shí)器1 輸入捕獲通道2 PortC 7
// TIM1_DeInit(); --> 定時(shí)器1 寄存器復(fù)位
// TIM1_ICInit( TIM1_CHANNEL_2, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI , TIM1_ICPSC_DIV8, 0x0);
// --> 輸入捕獲CH2 , 下降沿捕獲 , 輸入捕捉源為直接中斷方式 , 輸入捕捉預(yù)分頻 , 輸入捕捉濾波器值
// TIM1_ITConfig( TIM1_IT_CC2 , ENABLE); --> 開(kāi)啟通道二輸入捕獲中斷使能
// TIM1_ClearFlag(TIM1_FLAG_CC2); --> 清除輸入捕獲中斷標(biāo)志位
// TIM1_Cmd(ENABLE); --> 使能定時(shí)器1
//==============================================================================
voidTIME1_INIT(void)
{
TIM1_DeInit();
TIM1_ICInit(TIM1_CHANNEL_2,TIM1_ICPOLARITY_FALLING,TIM1_ICSELECTION_DIRECTTI,TIM1_ICPSC_DIV8,0x0);
TIM1_ITConfig(TIM1_IT_CC2,ENABLE);
TIM1_ClearFlag(TIM1_FLAG_CC2);
TIM1_Cmd(ENABLE);
}
/*******************************************************************************
* ==Main函數(shù)==
*******************************************************************************/
voidmain(void)
{
HSE_CLK_INIT();// 外部時(shí)鐘初始化
TIME1_INIT();// 定時(shí)器1 輸入捕獲初始化
enableInterrupts();// 開(kāi)總中斷
while(1);
}
//==============================================================================
// ¤¤¤ 中斷服務(wù)函數(shù) ¤¤¤
// --> 定時(shí)器1 捕獲/比較 中斷向量
//==============================================================================
#pragma vector=0xE
__interruptvoidTIM1_CAP_COM_IRQHandler(void)
{
Get_time=TIM1_GetCapture2();// 獲得捕獲時(shí)間
TIM1_ClearFlag(TIM1_FLAG_CC2);// 清標(biāo)志位
}
評(píng)論