cancel
Showing results for 
Search instead for 
Did you mean: 

External Clock Input for timers

mubinicyer
Associate II
Posted on March 24, 2014 at 21:28

Hi,

I want to make a timer to count on each rising edge of a square signal on PC.6. I am using STM32F103RBT6. I searched a sample code for that, but I couldn't find anything. What I found are all about timer interrupts or so. I looked at the Standard Peripheral Library source codes as well, but I really don't understand :)

Can anybody help me?

Thanks.

#external-clock-source.
4 REPLIES 4
Posted on March 24, 2014 at 22:07

For the F1 you'd probably need to define the pin as an input and remap TIM3

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=134]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FSTM32F4%20Timer%20External%20Pulse%20counting&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=134
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mubinicyer
Associate II
Posted on March 24, 2014 at 23:23

Thank you very much for your answer :), I already read that thread and noticed that there are a function (GPIO_PinAFConfig) which is not in my development environment (CooCox IDE 1.7.6),  but then I have used GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);

now it seems to work with external clock, however I have now another problem:

What exactly I wanto to make is to connect RFID Frond-End chip EM4095 with STM32. I am using two outputs of EM4095, RDY/CLK and DEMOD_OUT. I have a small osciloscope (DSO QUAD) and I upload the scope picture.

EM4095 outputs a square wave on RDY/CLK pin at 125 KHz as expected. If there is no RFID tag in front of the coil, it gives a square wave on DEMOD_OUT pin at ~3.600 KHz. I am using EXTI interrupts for that input and I want to count the RDY/CLK cycles until next rising edge of DEMOD_OUT, therefore I configured the TIM3 so that it will count up on each rising edge of RDY/CLK signal.

As you can see on the picture below, the TIM3 shall give 31 or 32 until next interrupt, but it gives always 0 or 1. Blue line is RDY/CLK, yellow is DEMOD_OUT.

 0690X00000602PvQAI.bmp

And here is my code:

void

 

EXTI_Exp(

void

)

 

{

 

RCC_Configuration();

NVIC_Configuration();

GPIO_Configuration();

Timer_Configuration();

//other configurations like USART and LCD

GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6);

EXTI_InitStructure.

EXTI_Line

= EXTI_Line6;

EXTI_InitStructure.

EXTI_Mode

=

EXTI_Mode_Interrupt

;

EXTI_InitStructure.

EXTI_Trigger

=

EXTI_Trigger_Rising

;

EXTI_InitStructure.

EXTI_LineCmd

=

ENABLE

;

EXTI_Init(&EXTI_InitStructure);

 while

(1)

   {

   }

}

void

 

GPIO_Configuration(

void

)

 

{

 

// Input for DEMOD_OUT and RDY/CLK signal from EM4095

 

GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_IN_FLOATING

;

 

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_6;

 

GPIO_Init(GPIOC, &GPIO_InitStructure);

 

 

GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_IN_FLOATING

;

 

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_7;

 

GPIO_Init(GPIOC, &GPIO_InitStructure);

 

GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,

ENABLE

);

 

}

 

 

 

void

Timer_Configuration(

void

)

 

{

 

TIM_TimeBase_InitStructure.

TIM_ClockDivision

= TIM_CKD_DIV1;

 

TIM_TimeBase_InitStructure.

TIM_CounterMode

= TIM_CounterMode_Up;

 

TIM_TimeBase_InitStructure.

TIM_Period

= 65535;

 

TIM_TimeBase_InitStructure.

TIM_Prescaler

= 0;

 

TIM_TimeBaseInit(TIM3, &TIM_TimeBase_InitStructure);

 

TIM_Cmd(TIM3,

ENABLE

);

 

TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);

 

 

}

 

void

NVIC_Configuration(

void

)

 

{

 

// Enable EXTI 9-5 Lines interrupt

 

NVIC_InitStructure.

NVIC_IRQChannel

= EXTI9_5_IRQn;

 

NVIC_InitStructure.

NVIC_IRQChannelPreemptionPriority

= 0;

 

NVIC_InitStructure.

NVIC_IRQChannelSubPriority

= 0;

 

NVIC_InitStructure.

NVIC_IRQChannelCmd

=

ENABLE

;

 

NVIC_Init(&NVIC_InitStructure);

 

}

 

 

void

RCC_Configuration(

void

)

 

{

 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | \

 

RCC_APB2Periph_GPIOC | \

 

RCC_APB2Periph_GPIOB | \

 

RCC_APB2Periph_AFIO ,

ENABLE

);

 

 

RCC_APB1PeriphClockCmd( RFID_TIM_RCC | \

 

RCC_APB1Periph_TIM3 | \

 

RCC_APB1Periph_USART2 | \

 

RCC_APB1Periph_USART3,

ENABLE

);

 

}

 

 

  </p>

 

void

 

</b>

 

 

EXTI9_5_IRQHandler(

void

)

 

{

 

uint8_t i;

 

 

if

(EXTI_GetFlagStatus(EXTI_Line6) ==

SET

) {

 

        EXTI_ClearITPendingBit(EXTI_Line6);

 

        iCnt = TIM_GetCounter(TIM3);

 

        LCD_SendText(

'':''

);

 

        LCD_SendNumber(iCnt);

 

        TIM_SetCounter(TIM3, 0);

 

}

 

 

 

I am calling EXTI_Exp() function in the main.c

 

 

 

 

 

 

 

Posted on March 24, 2014 at 23:54

If you must reset the timer's counter, then do it BEFORE outputting to the LCD, which is likely very slow.

Alternatively don't reset the counter, just remember the previous value and compute a delta (b-a) of how it changed.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mubinicyer
Associate II
Posted on March 25, 2014 at 11:26

Thank you, I will activate USART and send the numbers to PC.