Skip to main content
Visitor II
September 6, 2011
Question

using TIM1 to measure a pulse witdth on STM8S207S

  • September 6, 2011
  • 8 replies
  • 1619 views
Posted on September 06, 2011 at 19:56

I'm looking to measure 50/60Hz using TIM1 and pin PD7 as input has anyone an example of this?

Also I don't seem to be able to select the AFR4 in the STDV anyone have a clue why?
    This topic has been closed for replies.

    8 replies

    Visitor II
    September 7, 2011
    Posted on September 07, 2011 at 10:03

    if you have a look on the datasheet

    http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00197787.pdf

    pag.32 note 5 you can see AFR4 is available only on 44-pin package devices. You can use TIM1_CH1 instead of TIM1_CH4 i.e.

    brazov2

    Visitor II
    September 7, 2011
    Posted on September 07, 2011 at 11:27

    Yes ment to say I'm using a 44pin device.

     Do I need to tell the programmer it's 44pin?

    Also when I'm using this pin as a input to the timer can I read it as a input?
    Visitor II
    September 7, 2011
    Posted on September 07, 2011 at 15:19

    yes, you must specify exact stm8s sales type in the programmer.

    You can read pin status in GPIO_IDR register @5010h.

    brazov2

    Visitor II
    September 7, 2011
    Posted on September 07, 2011 at 19:26

    Thanks,

    Any ideas on how to set up the timer just don't want to truge through the datasheet?

    Visitor II
    September 8, 2011
    Posted on September 08, 2011 at 10:06

    /* Private variables ---------------------------------------------------------*/

     u8 TMPICH;  // current CC1 input capture H & L values

     u8 TMPICL;

     u16 ICAP;  // current ICAP sample in word

    // *** GIO INIT ***

           GPIOD->CR1|= (u8)0x80;  // IC1 (PD.7) settings: Input pull-up

           GPIOD->DDR&=~(u8)0x80;

    // *** TIM1 INIT ***

           TIM1->CCMR1= ((15<<4) & TIM1_CCMR_ICxF) | (1 & TIM1_CCMR_CCxS); // CC1 input capture, filter to max

           TIM1->CCER1= TIM1_CCER1_CC1E | TIM1_CCER1_CC1P; // CC1 IC enable, falling edge

           TIM1->CR1|= TIM1_CR1_URS |TIM1_CR1_CEN; // enable timer

     

    // *** MAIN LOOP *** 

     while (1) {

      if ((TIM1->SR1 & TIM1_SR1_CC1IF) != 0) {    // input capture?

       TMPICH= TIM1->CCR1H;               // yes - copy captured value to ICAP variable

       TMPICL= TIM1->CCR1L;

       ICAP= (u16)(((u16)(TMPICH) << 8) | (u16)(TMPICL));

                            TIM1->SR1&=~TIM1_SR1_CC1IF;// clear CC flag

      }

    Visitor II
    September 8, 2011
    Posted on September 08, 2011 at 11:53

    Thanks save me alot of time :)

    Visitor II
    September 8, 2011
    Posted on September 08, 2011 at 13:16

    It looked so simple but I tried it and it didn't work set the clock prescale to 8 max time out > 10ms.

    I looked at the AFR4 but even though the programmer didnt let me see the alternative function I set it anyway but again nothing worked. I assume the SWIM doesn't over ride the option bytes after you use the programmer to program them?

    I assume the default setting for the other timer register dont need changing?

    I know the pin is changing as I set an external pin when it toggles but I don't know if the timer is seeing the change on the I/O.

    Visitor II
    September 8, 2011
    Posted on September 08, 2011 at 13:38

    example code below should be changed anyway to work..... Try to make it work with TIM1 IC1 (PC4 if i remember well) then rewrite it for TIM1 IC4 (PD7).In this case for IC1 on PC4 do the following port configuration. Also to debug it use break points in stvd

    // *** GIO INIT ***

           GPIOD->CR1|= (u8)0x80;  // IC1 (PD.7) settings: Input pull-up

           GPIOD->DDR&=~(u8)0x80;