2013-07-15 09:52 PM
I'm working with a quadrature decoder that I set up on timer 8. I'm having trouble because the counter on timer 8 only seems to count up to 16 bits. I really need it to count up to 32 bits. Is it possible to configure the counter to do this?
I tried doing this:TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
But it hasn't worked for me. Any ideas? Thanks in advance for the help! #know-your-tools2013-07-24 07:07 AM
For some reason, I thought that only TIM1 and TIM8 were configurable in encoder mode, but after looking again at the data sheet, it looks like TIM2-4 may also be. I hadn't considered them because I thought they couldn't be used.
I'm having issues however with getting the CNT register to change at all. Just trying to set it, I'm doing the following: TIM2->CNT = 5; printf(''Actual: %lu\n\r'', (TIM2->CNT));I'm getting out Actual: 0 always, which really confuses me. For some reason, is TIM2 -> CNT unsettable or am I doing something wrong?2013-07-24 07:13 AM
Oops, caught my own error. I was still initializing the peripheral clock for TIM8 instead of for TIM2
2013-07-24 07:38 AM
So I figured out one issue and ran into another. For some reason, the counter still seems to overflow at 16 bits. Is there some setting that I need to change for it to be a 32 bit timer instead of 16? I'm getting overflow when I set it to a value higher than what fits in 16 bits. Also, I switched to TIM3 because I was running into this issue with TIM2 and I wanted to make sure it wasn't just the timer. Here is my current intialization:
/* TIM8 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(MCBMINI_QUAD_TIM, ENABLE); //Configure the encoder interface TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); /* TIM8 counter enable */ TIM_Cmd(TIM3, ENABLE); TIM_CtrlPWMOutputs(TIM3, ENABLE);I've tried taking out the last line because I've heard that's only necessary for timers 1 and 8, but it had no effect.2013-07-24 08:37 AM
Unless I'm mistaken the only 32-bit timer on the F3 is TIM2
2013-07-24 09:45 AM
Oh, you're right. I didn't see the parenthesis next to the more significant digits of the counter that says it's just for TIM2.
I'm still working on it, but the encoder isn't working as it should anymore. (I had it counting the ticks with TIM8 but only up to 16 bits) I can scope the signals and see a tick train going into channels 1 and 2 (I'm using PD3 and PD4). It kind of confuses me that there are multiple pins that can be used for TIM2 channels. Do I have to specify which port I'm using for the timer? I'm not sure if it's defaulting to some other port.Also, I don't think this is an issue but I noticed that channel 1 for TIM2 is labelled TIM2_CH1_ETR. I don't think this should affect the encoder count, but these are the only differences that I can see from TIM8 and it isn't working, so I'm looking at all of the changes.Thanks so much for the help so far. It's really great that you are so active on this forum to help out people like me who aren't nearly as knowledgeable or experienced with this type of thing.2013-07-24 09:50 AM
This is a blind implementation I built earlier.
// STM32 TIM2 Encoder Decoder (PD.3, PD.4) STM32F3-Discovery - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
/* Enable TIM2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Free pins located in STM32F3-Discovery manual, UM1570
// AF located in F302/303 Data Manual (Alternate functions for port D - Table 15, Rev 5)
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_2); // PD.3 TIM2_CH1_ETR (CH1 or ETR)
GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_2); // PD.4 TIM2_CH2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
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; // Or UP for open-collector sources
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/**************************************************************************************/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM2, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM2_Configuration();
// TIM2->CNT contains current position
while(1); // Don't want to exit
}
2013-07-24 10:20 AM
Yes! It works!
I didn't realize that the alternate functions were numbered differently between TIM8 and TIM2, though that seems pretty obvious now. Thanks again for the help!2015-03-27 10:26 AM
I tried to run this code, but it comes out with a bunch of errors, sayin that several identifiers result undeclared. How come that? :\
2015-03-27 10:39 AM
I tried to run this code, but it comes out with a bunch of errors, sayin that several identifiers result undeclared. How come that? :\
You don't have your libraries and tool chain set up properly, your project settings don't include the right defines, and include paths?Review the project templates for your tool chain in thehttp://www.st.com/web/en/catalog/tools/PF258154
firmware release.2015-03-27 11:05 AM
Probably it's a stupid issue, since I haven't been using keil for a while and surely I'm not an expert, but I tried setting the IDE as shown here:
https://www.youtube.com/watch?v=_76lPzXf8To but still it doesn't work (while the example works properly).