I am having a Ultrasonic Rangers SRF04, and using STM32F103RBT6 to calculate the distance.
I have studied the reference manual for a while, and I think that the solution for my problem is Gated Mode of Timer. The counter of TIM2 starts counting on the internal clock as long as TI2 is high, and stops as soon as TI2 becomes low. (I connect PA1 to the Echo Pulse Output of SRF04)
When doing this, I do not know when I can get the value of counter because this mode does not generate an interrupt, so I config EXTI Line1 to detect Falling edge of pin PA1.
If the SRF04 does not receive any sonic wave, the Echo Pulse Output drops down automatically after 36ms, so I config TIM2 counting with f = 1.5Mhz to avoid the counter overflow.
TIM2_CH1 in OCMode starts counting when TI2 is high, and stops when TI2 is low.
TIM3 is working in PWM Mode to trigger the SRF04.
Here is my code:
void GPIO_Config(void) { GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); // PC6 for TIM3 CH1, Trigger Pulse Input GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); // PA1: TIM2 CH2, measuring the pulse length of Echo Pulse Output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); } void TIM_Config(void) { // TIM2 frequency: 36Mhz/24 = 1.5Mhz, counting 60000 in 40ms TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 23; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 65535; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated);// TIM_Cmd(TIM2, ENABLE); // TIM3 channel 1 for PWM Output, f = 20Hz, duty cycle equal to 20us // 36Mhz/72 = 500k // 500k/25000 = 20Hz // 1/(500k)*10 = 20us TIM_TimeBaseStructure.TIM_Period = 24999; TIM_TimeBaseStructure.TIM_Prescaler = 71; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 10; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_Cmd(TIM3, ENABLE); } void EXTI_Config(void) { GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1); EXTI_InitStructure.EXTI_Line = EXTI_Line1; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line1); } void EXTI1_IRQHandler(void) { if(EXTI_GetITStatus(EXTI_Line1) != RESET) { EXTI_ClearITPendingBit(EXTI_Line1); GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8))); } }
I just check if PA1 detects the falling edge of Echo Pulse Output, it will change the LED that I have connected to PB8, and the result is nothing happen.
My questions are: 1/ Do I understand well about Gated Mode? Is my configuration right? 2/ Is there any another TIM mode that I can use for this problem?
P/S: my English is not good, but I hope that everybody can understand what I say ^_^