cancel
Showing results for 
Search instead for 
Did you mean: 

Hi all, My question is about the stm8 series. I am currently working with stm8s001jm3 and am trying to generate 2 MHz from a pin. The problem is that I can only generate a maximum of 380 kHz, although I have set everything correctly.

ATürk.11
Associate II

Im using the COSMIC Toolchain with the standard library.

Here is the setting:

##########################################################

void clock_init(void)

{

 CLK_DeInit();

             

 CLK_HSECmd(DISABLE);

 CLK_LSICmd(DISABLE);

 CLK_HSICmd(ENABLE);

 while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

                 

 CLK_ClockSwitchCmd(ENABLE);

 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

 CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

                 

 CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, 

 DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

                 

 CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);

}

##############################################################

who has experience with this processor and can help me ?

7 REPLIES 7

Try using the TIM itself to toggle the pin rather than using interrupts

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ATürk.11
Associate II

thx for your answer,

I dont use interrupts, i use the tim1 and the 2 mhz can still not be measured.

Cristian Gyorgy
Senior III

Hi!

You showed us a clock_init function. What about the signal generating function/routine you use ?

And, from all the functions you call inside your clock_init function, you only need the last one - in case you want to use timer 1 - otherwise all the rest are default settings after reset.

ATürk.11
Associate II

Hi Cristian,

As I said, I'm trying to generate 2 mhz on a pin as an output. And first I only measure with the oscilloscope.

I don't really have a function / routine (:

in the while loop i set the pin high and low so that i can only see the frequency.

Can you give me an example of what you mean by the functon / routine?

Thank you

Here the Loop.:

##########################################################################

main()

{

unsigned int i = 0;

CONFIG_UNUSED_PINS_STM8S001

clock_init();

GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW); // CCO is on PC4 (pin 7)

     TIM1_setup();

  

while (1)

{

for(i = 0; i < 1000; i += 1)

     { GPIO_WriteHigh(scope_port, scope_pin );

      TIM1_SetCompare1(i);

     }

for(i = 1000; i > 0; i -= 1)

     {

  GPIO_WriteLow(scope_port, scope_pin );

  TIM1_SetCompare1(i);

     }

  };

}

##########################################################################

Cristian Gyorgy
Senior III

Hi!

I don't really understand your code. How do you configure Tim1? Please post the TIM1_setup function. Maybe also the TIM1_SetCompare. What is the scope_pin?

ATürk.11
Associate II

Hi Cristian,

I have named it like this, it is only a port pin definition, in which I read out my frequency with the scope.

here is the TIM1_setup function.:

void TIM1_setup(void)

{

   TIM1_DeInit();

         

   TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 1000, 1);

         

   TIM1_OC4Init(TIM1_OCMODE_TIMING , 

         TIM1_OUTPUTNSTATE_ENABLE, 

         1000, 

         TIM1_OCNPOLARITY_LOW, 

         TIM1_OCNIDLESTATE_RESET);

         

  TIM1_Cmd(ENABLE);

}

I use OC4 because I have several timers on one pin C !!!

################################################################

main()

{

unsigned int i = 0;

CONFIG_UNUSED_PINS_STM8S001

clock_init();

GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW); // CCO is on PC4 (pin 7)

    TIM1_setup();

  

while (1)

{

for(i = 0; i < 1000; i += 1)

     { GPIO_WriteHigh(scope_port, scope_pin );

      TIM1_SetCompare4(i); // OC4

     }

for(i = 1000; i > 0; i -= 1)

     {

  GPIO_WriteLow(scope_port, scope_pin );

  TIM1_SetCompare4(i); // OC4

     }

 };

}

########################################################

thank you !

Cristian Gyorgy
Senior III

So, I don't use the C compiler and libraries, but to generate 2 MHz clk signal on PC4 is very simple:

  1. activate the master clock for TIM1 (probably done in the predefined setup functions)
  2. set channel 4 to toggle mode
  3. write 0x0003 in TIM1_ARR register to have a toggle frequency of 16/4=4 MHz
  4. enable the channel 4 in TIM1_CCER2
  5. enable the output in TIM1_BKR
  6. start timer by setting CEN bit in TIM1_CR1

Most probably all these steps are done in the library functions you call, so yuo'll just have to check what arguments you need to provide for those setup functions.

In the example above I chose the toggle method, but yuo can also do it with 50% duty cycle PWM, it's your choice.

After you execute the above the clock signal is running until you stop the timer, disable the channel or the MOE bit in brake register.

I still don't understand your for loops and the TIM1_SetCompare() functions...