2015-09-02 06:38 AM
Hi,
I'm trying to enable the timer 2 on a rising edge of a signal that i have connect on the tim2_ch1. I understand that the slave trigger mode of the timer can do that. I want to generate a signal on the tim2_ch2 on the rising edge of the tim2_ch1. Additionally, I would like to generate another signal on tim3_ch2 on the rising edge of the tim2_ch2. I did that by connecting the tim2 and tim3 as master and slave and it's worked. But the tim2_ch2 is generated before the rising edge of the tim2_ch1. Is it possible to start a counter on an external trigger or i have missed something? I've copied my program below :TimHandle.Instance = TIM2;
TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.Period = 10; //Compteur_SCK TimHandle.Init.Prescaler = 0; TimHandle.Init.RepetitionCounter = 0; ocConfig.OCMode = TIM_OCMODE_TOGGLE; ocConfig.OCPolarity = TIM_OCPOLARITY_HIGH; icConfig.ICFilter = 1; icConfig.ICPolarity = TIM_ICPOLARITY_RISING; icConfig.ICPrescaler = TIM_ICPSC_DIV1; icConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { Error_Handler(); } if(HAL_TIM_OC_ConfigChannel(&TimHandle,&ocConfig,TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } if(HAL_TIM_IC_ConfigChannel(&TimHandle,&icConfig,TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } slaveConfig.InputTrigger = TIM_TS_TI1FP1; slaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER; slaveConfig.TriggerFilter = 1; slaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; slaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1; if( HAL_TIM_SlaveConfigSynchronization(&TimHandle,&slaveConfig) != HAL_OK) { Error_Handler(); } masterConfig.MasterOutputTrigger = TIM_TRGO_OC2REF; masterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle,&masterConfig) != HAL_OK) { Error_Handler(); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// Tim2Handle.Instance = TIM3; Tim2Handle.Init.ClockDivision = 0; Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Tim2Handle.Init.Period = 2000; Tim2Handle.Init.Prescaler = 0; oc2Config.OCMode = TIM_OCMODE_PWM1; oc2Config.OCPolarity = TIM_OCPOLARITY_HIGH; oc2Config.Pulse = 20; if(HAL_TIM_OC_Init(&Tim2Handle) != HAL_OK) { Error_Handler(); } if(HAL_TIM_PWM_ConfigChannel(&Tim2Handle,&oc2Config,TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } if(HAL_TIM_IC_ConfigChannel(&Tim2Handle,&icConfig,TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } slaveConfig.InputTrigger = TIM_TS_ITR1; slaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER; slaveConfig.TriggerFilter = 1; slaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; slaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1; if( HAL_TIM_SlaveConfigSynchronization(&Tim2Handle,&slaveConfig) != HAL_OK) { Error_Handler(); } if(HAL_TIM_IC_Start(&TimHandle,TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } if(HAL_TIM_IC_Start(&Tim2Handle,TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } if(HAL_TIM_OC_Start(&TimHandle,TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } if(HAL_TIM_OC_Start(&Tim2Handle,TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } Thanks for your help, sorry for my english.. #trigger #timer #stm32f42015-09-02 09:26 AM
Your english is OK, what I don't understand is the Cube gibberish.
Post the content of relevant registers, including registers for the GPIO pin you use as external trigger. JW2015-09-03 02:02 AM
Thanks for your answer,
I've copied some registers values, I hope it helps to find the solution, I can provide more values if you need. The pins which are used for TIM1 are : tim1_ch1 : PA8 tim1_ch2 : PA9 tim2_ch1 : PA0 tim2_ch2 : PA1 Timer 1 Register : BDTR : MOE = 1 (Main Output Enable) CCER : CC1E = 1 (Capture enable) CC2E = 1 (Compare enable) CCMR1_Input : CC1S = 1 (CC1 Channel configure as input, IC1 mapped on TI1) CCMR1_Output : CC1S = 1 (CC1 Channel configure as input, IC1 mapped on TI1 ) OC1M = 1 (Set channel 1 to active level on match) OC2M = 3 (OC2REF toggle when CNT = CCR) CR1 : CEN = 1 (Counter enable) CR2 : MMS = 5 ( OC2REF is used a signal trigger output) SMCR : MSM = 1 ( Master Slave mode enabled) SMS = 6 ( slave Trigger mode) TS = 5 ( Filtered timer input 1)GPIOA Register
: MODER : MODER0 = 2 (Alternate function mode) MODER1 = 2 MODER8 = 2 MODER9 = 2 PUPDR : PUPDR0 = 0 (AF0 is selected) PUPDR1 = 0 PUPDR8 = 0 PUPDR9 = 02015-09-03 02:44 AM
> I can provide more values if you need.
I don't need them - it's YOU who needs them! :) 1. GPIOA->AFRL/AFRH? 2. Once TIM1->CR1.EN is set, there's no point in triggering - the only thing the trigger does is it *sets* CR1.EN, never resets it. This kind of problems can be effectively debugged directly by writing/observing registers in the debugger. First, get the capture working - set the timer to free running, set GPIO appropriately, enable capture, then externally toggle the pin's level and observe changes in the capture register. JW2015-09-03 06:23 AM
I have found the problem by doing step by step and observing the registers values.
CR1.EN was enabled before the capture on channel 1 and others values like MOE and CC1E was not properly set. Thanks a lot for your support :)