2014-09-29 06:25 AM
Hei,
I'm sending a 3 us pulse to trigger the sensor. Than I have a time frame to reconfigure the pin in input capture, in order to measure the pulse. The pulse is working, but when I configurate the pin in input capture the pin is in a high output state.Why is the pin HIGH
? Shouldn't it be low. Here is the code:/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/* strucutures ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Private function prototypes -----------------------------------------------*/
void Pin_Config(void);
void TIM_Config(void);
char a=0;
int main(void)
{
Pin_Config();
TIM_Config();
while (1);
}
void Pin_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // GPIOB clock enable
/*PA.01 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6= TIM4_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2); //Connect TIM pins to AF2, PB6= TIM4_CH1
}
void TIM_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4 clock enable
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 203;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //PWM1MODE: CNT>CCR1 -> OCREF==0 else OCREF==1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // begining output is low
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_CC1); // Clear the CC1 flags
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // Enable the TIM4 receive interrupt
TIM_Cmd(TIM4, ENABLE); //Enable TIM counter
}
void TIM4_IRQHandler(void)
{
// Configure PWM Input Capture //
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // PA6 TIM3_CH2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Falling, Rising
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; // every 8 rising edge is a capture
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
}
// Unused functions that have to be included to ensure correct compiling
// ###### DO NOT CHANGE ######
// =======================================================================
uint32_t L3GD20_TIMEOUT_UserCallback(void)
{
return 0;
}
uint32_t LSM303DLHC_TIMEOUT_UserCallback(void)
{
return 0;
}
// ============================================================================================================================
#stm32 #tim #tim #stm32
2014-09-29 07:24 AM
I don't know, but if you don't actually service the interrupt, you'll never be able to leave.
2014-10-01 02:40 AM
clive1 you are right, but I can't find any explanation why is the pin high.
2014-10-01 03:00 AM
here is the code with the int. flags cleared:
int main(void)
{
Pin_Config();
TIM_Config();
while (1);
}
void Pin_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // GPIOB clock enable
/*PA.01 configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6= TIM4_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2); //Connect TIM pins to AF2, PB6= TIM4_CH1
}
void TIM_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //TIM4 clock enable
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 71; // 1M Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 203;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //PWM1MODE: CNT>CCR1 -> OCREF==0 else OCREF==1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // begining output is low
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_CC1); // Clear the CC1 flags
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // Enable the TIM4 receive interrupt
TIM_Cmd(TIM4, ENABLE); //Enable TIM counter
}
void TIM4_IRQHandler(void)
{
if (TIM4 ->SR & TIM_IT_CC1 ) TIM4 ->SR &= (~TIM_IT_CC1);
// Configure PWM Input Capture //
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; // PA6 TIM3_CH2
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Falling, Rising
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; // every 8 rising edge is a capture
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//Checks whether the TIM interrupt has occurred or not.
if (TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET) TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
}
2014-10-02 06:55 AM
Hi,
Would you mind giving a little more information about your issue so we can better explain it to our development team? That would help more easily figure out where a problem could be.Regards,Heisenberg.2014-10-02 07:21 AM
Start perhaps with posting the content of timer's registers read out after being switched to the input capture mode.
JW2014-10-02 09:35 AM
Does the sensor hold the pin high? What does the pin do if the sensor is NOT connected? And without the sensor with the pin having a weak pull-down resistor?
2014-10-03 05:28 AM
Hei,
I'm very thankful for the help and also I'm bit occupied this week. So, I will post it on Wednesday, 8.10, with more details and what have I done till now… I haven't yet connected the sensor with this code.2014-10-07 09:23 AM
2014-10-08 12:19 AM
I've just realized from the title of your first post, that you are using a STM32f3 discovery board - on that, there is a 10k pullup on PB6. Have you removed that?
JW