cancel
Showing results for 
Search instead for 
Did you mean: 

using TIM1 to measure a pulse witdth on STM8S207S

colin239955_st
Associate II
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?
8 REPLIES 8
brazov22
Associate II
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

colin239955_st
Associate II
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?
brazov22
Associate II
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

colin239955_st
Associate II
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?

brazov22
Associate II
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) << 😎 | (u16)(TMPICL));

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

  }

colin239955_st
Associate II
Posted on September 08, 2011 at 11:53

Thanks save me alot of time 🙂

colin239955_st
Associate II
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.

brazov22
Associate II
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;