cancel
Showing results for 
Search instead for 
Did you mean: 

No output from TIM2 in compare mode on STM8L101F3

AMacd.1
Associate III

I am trying to get a pulse out of TIM2 in compare mode but I see no output.

Here is my snippet of code:

#include "iostm8l101f3.h"
void main( void )
{
  CLK_CKDIVR = 0x01;  // 8MHz
  PA_CR1 = 0xFF;      // all pullups
  PB_DDR = 0x01;      // 0000 0001 = PB0 (TIM2 CCR1) output, all other input
  PB_CR1 = 0xFF;      // 1111 1111 = Pullups: PB1-PB7, push-pull: PB0
  PC_ODR = 0x01;      // PC0 high, PC1 low
  PC_DDR = 0x03;      // PC0 and PC1 output, rest are input
  PC_CR1 = 0xFC;      // Pullups on inputs PC2 - PC7
  PD_CR1 = 0xFF;      // Pullup on PD0-PD7
 
  CLK_PCKENR |= 0x1;  // enable TIM2 clock
  TIM2_ARRH   = 0x07; // period high byte
  TIM2_ARRL   = 0xD0; // period low byte
  TIM2_CCR1H  = 0x03; // 50% duty
  TIM2_CCR1L  = 0xE8;
  TIM2_CCMR1_COMPARE_OC1M  = 6; // pwm 1
  TIM2_BKR_MOE = 1;     // Main output enable
  TIM2_CCER1_CC1E = 1;  // enable output
 
  TIM2_CR1_CEN    = 1;  // enable counter
  TIM2_EGR       |= 1;  // Reset timer
 
  while(1)
  {
    // loop forever
  }
}

When I run this in the IAR debugger, I put a breakpoint on the while loop and watch the timer registers and hit go. Every time the program breaks, I can see the counter has moved, or has reset to zero and the interrupt flags get set, etc. However, when I watch PB_IDR_IDR0, it never changes and when I put a scope on PB0, I see no pulse.

I can bit-bang the same IO and it works so the hardware is OK.

What am I missing?

Edit: I did discover a couple of missing lines but they didn't seem to help:

 TIM2_CCMR1_COMPARE_OC1PE = 1; // preload enable

 TIM2_CR1_ARPE = 1;           // auto-reload

I updated my snippet.

1 ACCEPTED SOLUTION

Accepted Solutions
AMacd.1
Associate III

i just answered my own question:

The missing line was TIM2_BKR_MOE = 1;

The other two lines I thought I needed were unnecessary.

I updated the snippet so it shows the correct operations to get the timer output going.

Hopefully, someone else will find this useful.

View solution in original post

1 REPLY 1
AMacd.1
Associate III

i just answered my own question:

The missing line was TIM2_BKR_MOE = 1;

The other two lines I thought I needed were unnecessary.

I updated the snippet so it shows the correct operations to get the timer output going.

Hopefully, someone else will find this useful.