1.对应复用功能5 GPIO初始化
2.可选择触发模式:// Rising to Rising edge // Rising to Falling edge // Falling to Falling edge / Falling to Rising edge
3.贴上源码
//*************************************************************************************************************
//****************************TM20ÊäÈ벶»ñ¹¦ÄÜ
//************************************************************************************************************
#include "MG32x02z__Common_DRV.H"
#include "MG32x02z_DRV.H"
void ChipInit(void);
void Sample_Code(void);
uint32_t CaptureData;
#define TM20_IC0_TriggerSrc 3
//************************************************************************************************************
void Sample_TM20_Capture(void)
{
TM_TimeBaseInitTypeDef TM_TimeBase_InitStruct;
//===Set CSC init====
//MG32x02z_CSC_Init.h(Configuration Wizard)
//Select CK_HS source = CK_IHRCO
//Select IHRCO = 12M
//Select CK_MAIN Source = CK_HS
//Configure PLL->Select APB Prescaler = CK_MAIN/1
//Configure Peripheral On Mode Clock->TM20 = Enable
//Configure Peripheral On Mode Clock->Port B = Enable
//==Set GPIO init
//TM20_IC0 pin config:
//MG32x02z_GPIO_Init.h(Configuration Wizard)->Use GPIOB->Pin4
//GPIO port initial is 0xFFFF
//PB4 mode is ODO
//PB4 function TM20_IC0
TM_DeInit(TM20);
TM_TimeBaseStruct_Init(&TM_TimeBase_InitStruct);
TM_TimeBase_InitStruct.TM_MainClockDirection =TM_UpCount;
TM_TimeBase_InitStruct.TM_Period = 65535;
TM_TimeBase_InitStruct.TM_Prescaler = 65535;
TM_TimeBase_InitStruct.TM_CounterMode = Full_Counter;
TM_TimeBase_Init(TM20, &TM_TimeBase_InitStruct);
// 2.config overwrite mode (keep data)
TM_IC0OverWritten_Cmd(TM20, TM_Keep);
// 3.config TM20 channel 0 function
TM_CH0Function_Config(TM20, TM_InputCapture);
TM_IN0Source_Select(TM20, IC0); // TM20_IN0 from IC0 (PB4)
// 4.config TRGI mode
TM_TriggerSource_Select(TM20,TRGI_IN0);
#if (TM20_IC0_TriggerSrc == 1) // Rising to Rising edge
TM_TRGICounter_Select(TM20,TRGI_StartupRising);
TM_TRGIPrescaler_Select(TM20,TRGI_StartupRising);
TM_IN0TriggerEvent_Select(TM20, IC_RisingEdge);
#elif (TM20_IC0_TriggerSrc == 2) // Rising to Falling edge
TM_TRGICounter_Select(TM20,TRGI_StartupRising);
TM_TRGIPrescaler_Select(TM20,TRGI_StartupRising);
TM_IN0TriggerEvent_Select(TM20, IC_FallingEdge);
#elif (TM20_IC0_TriggerSrc == 3) // Falling to Falling edge
TM_TRGICounter_Select(TM20,TRGI_StartupFalling);
TM_TRGIPrescaler_Select(TM20,TRGI_StartupFalling);
TM_IN0TriggerEvent_Select(TM20, IC_FallingEdge);
#elif (TM20_IC0_TriggerSrc == 4) // Falling to Rising edge
TM_TRGICounter_Select(TM20,TRGI_StartupFalling);
TM_TRGIPrescaler_Select(TM20,TRGI_StartupFalling);
TM_IN0TriggerEvent_Select(TM20, IC_RisingEdge);
#endif
TM_ClearFlag(TM20, TMx_CF0A | TMx_CF0B);
TM_Timer_Cmd(TM20,ENABLE);
return;
}
//************************************************************************************************************
void Sample_GPIO_RWInit(void)
{
uint16_t ReadData;
PIN_InitTypeDef PINX_InitStruct;
PINX_InitStruct.PINX_Mode = PINX_Mode_OpenDrain_O; //Ñ¡ÔñIOģʽ // 1.QB: Quasi-Bidirection mode only for PC )
PINX_InitStruct.PINX_PUResistant = PINX_PUResistant_Enable; //ÄÚ²¿ÉÏÀ
PINX_InitStruct.PINX_Speed = PINX_Speed_Low; //ÉèÖÃËÙ¶È
PINX_InitStruct.PINX_OUTDrive = PINX_OUTDrive_Level0; //Êä³öÄÜÁ¦ÉèÖÃ
PINX_InitStruct.PINX_FilterDivider = PINX_FilterDivider_Bypass; //Ñ¡ÔñÊäÈëµÄÂ˲¨·ÖƵÆ÷
PINX_InitStruct.PINX_Inverse = PINX_Inverse_Disable; //ÊäÈëÐźÅÊÇ·ñ·´×ª
PINX_InitStruct.PINX_Alternate_Function = 5; //Ñ¡ÔñGPIOģʽ
GPIO_PinMode_Config(PINB(4),&PINX_InitStruct); //PA0 ÅäÖ
PB4=1;
PINX_InitStruct.PINX_Alternate_Function = 0; //Ñ¡ÔñGPIOģʽ
GPIO_PinMode_Config(PINB(1),&PINX_InitStruct); //PA0 ÅäÖ
}
//************************************************************************************************************
int main()
{
ChipInit();
Sample_GPIO_RWInit();
while(1)
{
Sample_TM20_Capture();
while(TM_GetSingleFlagStatus(TM20,TMx_CF0A) == DRV_UnHappened);
CaptureData = TM_GetIC0Value(TM20);
TM_ClearFlag(TM20, TMx_CF0A | TMx_CF0B);
PB1=0;
}
}
|