2009-05-18 07:57 PM
problem with timer
2011-05-17 04:12 AM
Hi:
It would be useful to indicate what it is the actual problem: cannot program the MCU? Code doesn't run? Timer doesn't run? Have you tried the debugger and step through the code? A quick look at you code reveals a possible source of problems:Code:
while(1) continue; The semicolon at the end of the while statement and the keywordQuote:
continue
create an infinite loop, stalling the processor. Regards, Alex.2011-05-17 04:12 AM
Hi everyone.I'm a beginner.I choose stm32 to do my term project.I have a problem with timerx. I want to use the timer2 interface with encoder from the motor. What should I do to program the MCU ?
I need your help. Suggest me please ps. I have some example of code which I try to program it, but it doesn't work! #include ''stm32f10x_lib.h'' /* Private typedef --------*/ /* Private define ---------*/ /* Private macro ----------*/ /* Private variables ------*/ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; ErrorStatus HSEStartUpStatus; /* Private function prototypes -------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); int main() { /* System Clocks Configuration */ RCC_Configuration(); /* Configure all unused GPIO port pins in Analog Input mode (floating input trigger OFF), this will reduce the power consumption and increase the device immunity against EMI/EMC *************************************************/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_Init(GPIOE, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, DISABLE); /* Enable GPIOA and TIM2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* GPIO Configuration */ GPIO_Configuration(); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Encoder interface confrigulation */ TIM_EncoderInterfaceConfig(TIM2,TIM_EncoderMode_TI12, TIM_ICPolarity_Rising,TIM_ICPolarity_Rising); TIM_Cmd(TIM2,ENABLE); while(1) continue; { int count; count = TIM2 -> CNT; return count; } } /******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } } /******************************************************************************* * Function Name : GPIO_Configuration * Description : Configure the TIM1 Pins. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA Configuration: Channel 1, 2 as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } [ This message was edited by: acuransx_pj1 on 11-05-2009 17:29 ]2011-05-17 04:12 AM
Quote:
On 11-05-2009 at 21:28, Anonymous wrote: Hi: It would be useful to indicate what it is the actual problem: cannot program the MCU? Code doesn't run? Timer doesn't run? Have you tried the debugger and step through the code? A quick look at you code reveals a possible source of problems:Code:
while(1) continue; The semicolon at the end of the while statement and the keywordQuote:
continue
create an infinite loop, stalling the processor. Regards, Alex. thank a lot Alex. I try to step through the code. It can not run after int main(). I've no idea about this problem. Who used to try the encoder interface please give me a suggestion or sample of code. thank you2011-05-17 04:12 AM
Now,I know about how to configuration encoder interface.After run the program the timer will count the signal from the encoder and record the value at some parameter right? I need to know how to show the value that counter can count? and I want to know how many cycle of motor what should I do? ( If I miss understand about the timer please explain for me.)
PS. I've never program MCU before. This is my first time. Help me please :-[2011-05-17 04:12 AM
acuransx_pj1,
Your question is like: How does a plant work ? If that was your question, then we wouldn't know if you were interested in the kreb's cycle, photosynthesis, plant reproduction, fluid transport etc. Consider how you want to see the result, and try to match your skill level to get a method that is easy for you to implement. If you are using a demo board with a display, then use the code they used to drive the display, or for serial output.. etc. to answer your question as you have posed it, is for us to do your work for you, and we are working on our own projects already.2011-05-17 04:12 AM
Quote:
On 18-05-2009 at 11:45, Anonymous wrote: acuransx_pj1, Your question is like: How does a plant work ? If that was your question, then we wouldn't know if you were interested in the kreb's cycle, photosynthesis, plant reproduction, fluid transport etc. Consider how you want to see the result, and try to match your skill level to get a method that is easy for you to implement. If you are using a demo board with a display, then use the code they used to drive the display, or for serial output.. etc. to answer your question as you have posed it, is for us to do your work for you, and we are working on our own projects already. Thank you rael. I 'll try it.