Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help with Timer 2 (TIM2) on STM8S103F3


  • Please log in to reply
4 replies to this topic

#1 ByteMaster

ByteMaster

    Advanced Member

  • Members
  • PipPipPip
  • 76 posts

Posted 21 May 2012 - 12:41 AM

I've successfully been able to get TIM4 to work, but ran into a dead end with TIM2. Can some one with some experience on this point me in the right direction?

Here’s my TIM4 code, I assume I need something to specify the channels for TIM2, but can’t seem to figure out the right combination.

	TIM4_DeInit();
	TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0x7D); /* Each tick should be a MS */
	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);		
	TIM4_Cmd(ENABLE);			

And then capture the overflow in the interrupt handler.
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
        TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}

Thanks in advance!

Kevin...
Kevin D. Wolf
Windows Phone Development MVP
President Software Logistics, LLC
Tampa, FL

#2 Matt Isenhower

Matt Isenhower

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts
  • LocationSan Diego, CA

Posted 21 May 2012 - 01:14 AM

Hi Kevin,

Are you enabling interrupts? If you are using the ST standard peripheral libraries, you can enable interrupts with:
enableInterrupts();

What are you looking to do with Timer 2? If you want to use it for PWM output you don't actually need to use (or enable) interrupts. Here's a PWM example using the ST libraries:
    /* Time base configuration */
    TIM2_TimeBaseInit(TIM2_PRESCALER_1, 0x03FF);
    
    /* PWM1 Mode configuration: Channel1 */ 
    TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE, 125, TIM2_OCPOLARITY_HIGH);
    TIM2_OC1PreloadConfig(ENABLE);
    TIM2_ARRPreloadConfig(ENABLE);
    
    /* TIM2 enable counter */
    TIM2_Cmd(ENABLE);

Matt
Komodex Labs
Follow me on Twitter: @mattisenhower

#3 ByteMaster

ByteMaster

    Advanced Member

  • Members
  • PipPipPip
  • 76 posts

Posted 21 May 2012 - 01:22 PM

Are you enabling interrupts? If you are using the ST standard peripheral libraries, you can enable interrupts with:

enableInterrupts();


Thanks Matt - I am enabling interrupts and can successfully get them to fire with TIM4, just not with TIM2?!?!?!? Assuming it has something to do with TIM2 having three channels and TIM4 only having one. I'll have to play around a bit with it a little later.

I'm actually using it to do some internal timing on buffering/sending data back to the main board, but PWM is a future task, so thanks for the code.

Kevin...
Kevin D. Wolf
Windows Phone Development MVP
President Software Logistics, LLC
Tampa, FL

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 21 May 2012 - 01:45 PM

I am enabling interrupts and can successfully get them to fire with TIM4, just not with TIM2?!?!?!? Assuming it has something to do with TIM2 having three channels and TIM4 only having one.

Could you please show your code for TIM2? There are more differences between TIM2 and TIM4 (16 bit vs. 8 bit, prescaler range, interrupt vector etc.).

#5 ByteMaster

ByteMaster

    Advanced Member

  • Members
  • PipPipPip
  • 76 posts

Posted 21 May 2012 - 11:21 PM

Hi CW2 - not really sure what's different but I went in with a clear head tonight and was able to get TIM2 to work just fine?!?!?!?

Could you please show your code for TIM2? There are more differences between TIM2 and TIM4 (16 bit vs. 8 bit, prescaler range, interrupt vector etc.).


Anyway, here is my code:
	TIM2_DeInit();
	TIM2_TimeBaseInit(TIM2_PRESCALER_128, 0x7D); /* Each tick should be a MS */
		
	TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);		
		
	TIM2_Cmd(ENABLE);			
With a 16MHz clock this should yield a 1ms period (which I've verified on my scope).

Then since I've configured the interrupt to fire via the timer Update TIM2_IT_UPDATE, the following interrupt handler gets called every 1MS
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
  TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
  /*Do something interesting*/
}

Of course as Matt said, somewhere in your code you need to enable interrupts:

enableInterrupts();		

And since I'm using the standard libraries the files must be added to your project:
stm8s_tim2.h
stm8s_tim2.c

I still need to grok using different channels, especially on TIM1, but getting this timer working moves the ball down the field a little further.

Kevin...
Kevin D. Wolf
Windows Phone Development MVP
President Software Logistics, LLC
Tampa, FL




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.