cancel
Showing results for 
Search instead for 
Did you mean: 

Time controlled by timer

mchnani
Associate II
Posted on June 29, 2010 at 17:08

Time controlled by timer

13 REPLIES 13
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi moussa,

You can find in the link below a set of examples that are useful for you.

In TIM4 example, you will fin what you are looking for :)

http://www.st.com/products/support/micro/files/stm8sfwlib.zip

Hope this helps and tell me if It is OK.

With regards,

MCU Lüfter,

be happy be low power

sylvain_laurent
Associate II
Posted on May 17, 2011 at 15:09

Hi,

If you want to generate an interrupt each 1ms, you can use the output compare.

And it's run very well.

Example in library TIM2.

Regards.

lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi Moussa,

Why Init_TIM2

() is not called in main( ) ?

some question regarding WaitDelay

( ) ???

Tell me if it ok or ????

Regards,.

MCU Lüfter

mchnani
Associate II
Posted on May 17, 2011 at 15:09

Hi,

I tested just now the exemple given in frame library of STM8 and the result is bad (

the time variable is remained the initial value)

. see my code and tell me it's ok or not. thank you so much  

Regards

#include ''stm8s.h''

#include ''stm8s_tim2.h''

#include ''stm8s_it.h''

u16 GTimeStamp=0;

void Init_TIM2(void)

{

TIM2_TimeBaseInit(TIM2_PRESCALER_128,124);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

TIM2_ClearFlag(TIM2_FLAG_UPDATE);

TIM2_ITConfig(TIM2_IT_UPDATE,ENABLE);

enableInterrupts();

TIM2_Cmd(ENABLE);

}

/** Wait a delay

* @param[in] Delay based on timer Tick

* @return None

**/

void WaitDelay(u16 Delay)

{

GTimeStamp = 0x00;

sim();

while (GTimeStamp <= Delay);

rim();

}

void TIM2_IRQHandler (void)

{

if (TIM2_GetITStatus(TIM2_IT_UPDATE)!= RESET)

{

GTimeStamp++;

TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

}

}

void main (void)

{

//GPIO-------------------------------

/* Entrées et sorties du micro*/

// Configure PF.4 en entrée flotante PRESENCE SECTEUR

GPIOF->CR1&=~0x10;

GPIOF->DDR&=~0x10;

// Configure PB.5 en entrée flotante COURANT BAES

// Configure PB.4 en entrée flotante COURANT BAEH

// Configure PB.3 en entrée flotante TELECOMMANDE

// Configure PB.2 en entrée flotante TENSION Leds Vertes

// Configure PB.1 en entrée flotante Detection presence batterie BAES

// Configure PB.0 en entrée flotante Detection presence batterie BAEH

GPIOB->CR1&=~0x3F;

GPIOB->DDR&=~0x3F;

// Configure PE.5 en sortie PushPull Pilotage Alim driver BAEH

GPIOE->DDR|= 0x20;

GPIOE->CR1|= 0x20;

// Configure PC.4 en sortie PushPull Pilotage Leds Vertes

GPIOC->DDR|= 0x10;

GPIOC->CR1|= 0x10;

// Configure PD.0 en sortie PushPull Pilotage charge batterie BAEH

// Configure PD.2 en sortie PushPull Pilotage charge batterie BAES

// Configure PD.3 en sortie PushPull Pilotage SATI Vert

// Configure PD.4 en sortie PushPull Pilotage SATI Jaune

// Configure PD.7 en sortie PushPull Pilotage Alim driver BAES

GPIOD->DDR|= 0x9D;

GPIOD->CR1|= 0x9D;

while (1) {

if(GTimeStamp <= 200)

{

GPIOD->ODR &= 0x10;

GPIOD->ODR |= 0x10;

}

else

{

GPIOD->ODR &= 0x00;

GPIOD->ODR |= 0x00;

}

while (1);

}

Hi moussa,

You can find in the link below a set of examples that are useful for you.

In TIM4 example, you will fin what you are looking for :)

http://www.st.com/products/support/micro/files/stm8sfwlib.zip

Hope this helps and tell me if It is OK.

With regards,

MCU Lüfter,

be happy be low power

Hello,

I'm using a STM8S105k4 and i need some program to generate an interrupt each 1ms for control of time (for example, create a counter value wich is incremented each interrupt)

Thank you for your help,
mchnani
Associate II
Posted on May 17, 2011 at 15:09

Hi lowpowermcu,

i added Init_TIM2() to the main() and the interrupt is generated but the flag interrupt is remained in permanant at high level. i dont know

how to pass the flag interrupt to the low state at the end of the increment of the time value.

when run :

@far @interrupt void NonHandledInterrupt (void)

{

 /* in order to detect unexpected events during development,

    it is recommended to set a breakpoint on the following instruction

 */

 return;

}

  

Best regards,

Hi Moussa,

Why Init_TIM2

() is not called in main( ) ?

some question regarding WaitDelay

( ) ???

Tell me if it ok or ????

Regards,.

MCU Lüfter

Hi,

I tested just now the exemple given in frame library of STM8 and the result is bad (

the time variable is remained the initial value)

. see my code and tell me it's ok or not. thank you so much  

Regards

#include ''stm8s.h''

#include ''stm8s_tim2.h''

#include ''stm8s_it.h''

u16 GTimeStamp=0;

void Init_TIM2(void)

{

TIM2_TimeBaseInit(TIM2_PRESCALER_128,124);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

TIM2_ClearFlag(TIM2_FLAG_UPDATE);

TIM2_ITConfig(TIM2_IT_UPDATE,ENABLE);

enableInterrupts();

TIM2_Cmd(ENABLE);

}

/** Wait a delay

* @param[in] Delay based on timer Tick

* @return None

**/

void WaitDelay(u16 Delay)

{

GTimeStamp = 0x00;

sim();

while (GTimeStamp <= Delay);

rim();

}

void TIM2_IRQHandler (void)

{

if (TIM2_GetITStatus(TIM2_IT_UPDATE)!= RESET)

{

GTimeStamp++;

TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

}

}

void main (void)

{

//GPIO-------------------------------

/* Entrées et sorties du micro*/

// Configure PF.4 en entrée flotante PRESENCE SECTEUR

GPIOF->CR1&=~0x10;

GPIOF->DDR&=~0x10;

// Configure PB.5 en entrée flotante COURANT BAES

// Configure PB.4 en entrée flotante COURANT BAEH

// Configure PB.3 en entrée flotante TELECOMMANDE

// Configure PB.2 en entrée flotante TENSION Leds Vertes

// Configure PB.1 en entrée flotante Detection presence batterie BAES

// Configure PB.0 en entrée flotante Detection presence batterie BAEH

GPIOB->CR1&=~0x3F;

GPIOB->DDR&=~0x3F;

// Configure PE.5 en sortie PushPull Pilotage Alim driver BAEH

GPIOE->DDR|= 0x20;

GPIOE->CR1|= 0x20;

// Configure PC.4 en sortie PushPull Pilotage Leds Vertes

GPIOC->DDR|= 0x10;

GPIOC->CR1|= 0x10;

// Configure PD.0 en sortie PushPull Pilotage charge batterie BAEH

// Configure PD.2 en sortie PushPull Pilotage charge batterie BAES

// Configure PD.3 en sortie PushPull Pilotage SATI Vert

// Configure PD.4 en sortie PushPull Pilotage SATI Jaune

// Configure PD.7 en sortie PushPull Pilotage Alim driver BAES

GPIOD->DDR|= 0x9D;

GPIOD->CR1|= 0x9D;

while (1) {

if(GTimeStamp <= 200)

{

GPIOD->ODR &= 0x10;

GPIOD->ODR |= 0x10;

}

else

{

GPIOD->ODR &= 0x00;

GPIOD->ODR |= 0x00;

}

 

while (1);

}

Hi moussa,

You can find in the link below a set of examples that are useful for you.

In TIM4 example, you will fin what you are looking for :)

http://www.st.com/products/support/micro/files/stm8sfwlib.zip

Hope this helps and tell me if It is OK.

With regards,

MCU Lüfter,

be happy be low power

Hello,

I'm using a STM8S105k4 and i need some program to generate an interrupt each 1ms for control of time (for example, create a counter value wich is incremented each interrupt)

Thank you for your help,
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi moussa,

If the interrupt is generated you should be pointed at the following IRQ:

@far @interrupt void TIM2_UPD_OVF_BRK_IRQHandler(void)

The TIM2 update flag is cleared by the following function:

TIM2_ClearITPendingBit(TIM2_IT_UPDATE); which write to 0 the ''UIF'' bi in TIM2_SR1 register.

Hope this helps otherwise I need much details.

Good luck Moussa !

Regards,

MCU Lüfter

mchnani
Associate II
Posted on May 17, 2011 at 15:09

Hi lowpowermcu,

to be pointed at the handler (when the interrupt is generated) i must added following code in the main() or not : 

#ifdef _RAISONANCE_

void TIM2_UPD_OVF_BRK_IRQHandler( void )

#else // COSMIC

FAR @interrupt void TIM2_UPD_OVF_BRK_IRQHandler(void)

#endif

{

 

  GTimeStamp++;

  TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

 

}

thank you,

Hi moussa,

If the interrupt is generated you should be pointed at the following IRQ:

@far @interrupt void TIM2_UPD_OVF_BRK_IRQHandler(void)

The TIM2 update flag is cleared by the following function:

TIM2_ClearITPendingBit(TIM2_IT_UPDATE); which write to 0 the ''UIF'' bi in TIM2_SR1 register.

Hope this helps otherwise I need much details.

Good luck Moussa !

Regards,

MCU Lüfter

Hi lowpowermcu,

i added Init_TIM2() to the main() and the interrupt is generated but the flag interrupt is remained in permanant at high level. i dont know

how to pass the flag interrupt to the low state at the end of the increment of the time value.

when run :

@far @interrupt void NonHandledInterrupt (void)

{

 /* in order to detect unexpected events during development,

    it is recommended to set a breakpoint on the following instruction

 */

 return;

}

  

Best regards,

Hi Moussa,

Why Init_TIM2

() is not called in main( ) ?

some question regarding WaitDelay

( ) ???

Tell me if it ok or ????

Regards,.

MCU Lüfter

Hi,

I tested just now the exemple given in frame library of STM8 and the result is bad (

the time variable is remained the initial value)

. see my code and tell me it's ok or not. thank you so much  

Regards

#include ''stm8s.h''

#include ''stm8s_tim2.h''

#include ''stm8s_it.h''

u16 GTimeStamp=0;

void Init_TIM2(void)

{

TIM2_TimeBaseInit(TIM2_PRESCALER_128,124);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

TIM2_ClearFlag(TIM2_FLAG_UPDATE);

TIM2_ITConfig(TIM2_IT_UPDATE,ENABLE);

enableInterrupts();

TIM2_Cmd(ENABLE);

}

/** Wait a delay

* @param[in] Delay based on timer Tick

* @return None

**/

void WaitDelay(u16 Delay)

{

GTimeStamp = 0x00;

sim();

while (GTimeStamp <= Delay);

rim();

}

void TIM2_IRQHandler (void)

{

if (TIM2_GetITStatus(TIM2_IT_UPDATE)!= RESET)

{

GTimeStamp++;

TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

}

}

void main (void)

{

//GPIO-------------------------------

/* Entrées et sorties du micro*/

// Configure PF.4 en entrée flotante PRESENCE SECTEUR

GPIOF->CR1&=~0x10;

GPIOF->DDR&=~0x10;

// Configure PB.5 en entrée flotante COURANT BAES

// Configure PB.4 en entrée flotante COURANT BAEH

// Configure PB.3 en entrée flotante TELECOMMANDE

// Configure PB.2 en entrée flotante TENSION Leds Vertes

// Configure PB.1 en entrée flotante Detection presence batterie BAES

// Configure PB.0 en entrée flotante Detection presence batterie BAEH

GPIOB->CR1&=~0x3F;

GPIOB->DDR&=~0x3F;

// Configure PE.5 en sortie PushPull Pilotage Alim driver BAEH

GPIOE->DDR|= 0x20;

GPIOE->CR1|= 0x20;

// Configure PC.4 en sortie PushPull Pilotage Leds Vertes

GPIOC->DDR|= 0x10;

GPIOC->CR1|= 0x10;

// Configure PD.0 en sortie PushPull Pilotage charge batterie BAEH

// Configure PD.2 en sortie PushPull Pilotage charge batterie BAES

// Configure PD.3 en sortie PushPull Pilotage SATI Vert

// Configure PD.4 en sortie PushPull Pilotage SATI Jaune

// Configure PD.7 en sortie PushPull Pilotage Alim driver BAES

GPIOD->DDR|= 0x9D;

GPIOD->CR1|= 0x9D;

while (1) {

if(GTimeStamp <= 200)

{

GPIOD->ODR &= 0x10;

GPIOD->ODR |= 0x10;

}

else

{

GPIOD->ODR &= 0x00;

GPIOD->ODR |= 0x00;

}

 

while (1);

}

Hi moussa,

You can find in the link below a set of examples that are useful for you.

In TIM4 example, you will fin what you are looking for :)

http://www.st.com/products/support/micro/files/stm8sfwlib.zip

Hope this helps and tell me if It is OK.

With regards,

MCU Lüfter,

be happy be low power

Hello,

I'm using a STM8S105k4 and i need some program to generate an interrupt each 1ms for control of time (for example, create a counter value wich is incremented each interrupt)

Thank you for your help,
lowpowermcu
Associate II
Posted on May 17, 2011 at 15:09

Hi moussa,

I don't know which compiler you are using but you have to add your code related to TIM2 Interrupt Service in stm8s_it.c

Regards,

MCU Lüfter

mchnani
Associate II
Posted on May 17, 2011 at 15:09

Hi lowpowermcu,

ok, but when i added the code in the TIM2 interrupt.

Result :  

Running Linker

clnk -m Debug\luminaire1025.map -l''C:\Program Files\COSMIC\CXSTM8_16K\Lib'' -o Debug\luminaire1025.sm8 Debug\luminaire1025.lkf

#error clnk Debug\luminaire1025.lkf:73 Debug\stm8_interrupt_vector.o: symbol f_NonHandledInterrupt multiply defined (Debug\stm8s_it.o)

The command: ''clnk -m Debug\luminaire1025.map -l''C:\Program Files\COSMIC\CXSTM8_16K\Lib'' -o Debug\luminaire1025.sm8 Debug\luminaire1025.lkf '' has failed, the returned value is: 1

exit code=1.

luminaire1025.elf - 3 error(s), 0 warning(s)

Thank you in advance