cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement microsecond delay on STM8S103F3?

e_zhui2
Associate II
Posted on May 04, 2010 at 03:34

How to implement microsecond delay on STM8S103F3?

4 REPLIES 4
jeffrey23
Associate II
Posted on May 17, 2011 at 15:08

How about the nop instruction approach ?

mozra272
Associate II
Posted on May 17, 2011 at 15:08

.ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 p.MsoNormal, .ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 li.MsoNormal, .ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 div.MsoNormal {margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:'Calibri','sans-serif';} .ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 span.EmailStyle15 {font-family:'Calibri','sans-serif';color:windowtext;} .ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 .MsoChpDefault {;} @page Section1 {size:8.5in 11.0in;margin:1.0in 1.0in 1.0in 1.0in;} .ExternalClass0DCDBD5026184B978A5ED1ABAF097CF4 div.Section1 {page:Section1;}

Hi

Boltnut,

try this solution:

/* General Purpose 1µs Time base */

void Init_TIM2(void)

{

    /* TimerTick = 1µs

       Warning: fcpu must be equal to 16MHz

       fck_cnt = fck_psc/presc = fcpu/16 = 1MHz --> 1 tick every 1µs

       ==> 1 µs / 1µs = 1 ticks

     */

    TIM2_TimeBaseInit( TIM2_PRESCALER_16, 1);

    TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

    TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

    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();

}

/**

  * @brief Timer2 Update/Overflow/Break Interruption routine.

  * @par Parameters:

  * None

  * @retval

  * None

  */

#ifdef _COSMIC_

@far @interrupt void TIM2_UPD_OVF_BRK_IRQHandler(void)

#else /* _RAISONANCE_ */

void TIM2_UPD_OVF_BRK_IRQHandler(void) interrupt 13

#endif /* _COSMIC_ */

{

    GTimeStamp++;

   

TIM2_ClearFlag(TIM2_FLAG_UPDATE);

}

Regards

mozra

jeffrey23
Associate II
Posted on May 17, 2011 at 15:08

Can you explain what the sim()/rim() are in WaitDelay()

function ?

My idea is shown as below:

void I2C_Delay (u8 bTime)

{

    /*

                            ld  a,(OFST+1,sp)

  72  0007 0a01             dec (OFST+1,sp)

  73  0009 4d               tnz a

  74  000a 26f9             jrne    L13

    */

    bTime /= 3;         // 3.5 cycles each loop !

    while (bTime--)

        ;

} /* I2C_Delay */

bTime is in STM8S machine cycles

(SYSCLK = 1 / 16MHz = 62.5 ns = 0.0625 us) !

ne562
Associate II
Posted on May 17, 2011 at 15:08

#define US(us) ( F_CPU / 3000000.0 * us )

#define MS(ms) US(ms * 1000) // maximum 10ms

#define _delay( loops ) \

     _asm(''$N: \n decw X \n jrne $L \n nop'', (U16)loops);

#define _delay_us(us) _delay(US(us))

void _delay_ms( U16 ms ) {

     while (ms--) {

         _delay_us( 1000 );

     }

}