2021-08-01 11:42 PM
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 ?
2021-08-01 11:49 PM
Try using the TIM itself to toggle the pin rather than using interrupts
2021-08-02 02:03 AM
thx for your answer,
I dont use interrupts, i use the tim1 and the 2 mhz can still not be measured.
2021-08-02 08:24 AM
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.
2021-08-02 11:20 PM
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);
}
};
}
##########################################################################
2021-08-04 12:56 AM
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?
2021-08-05 12:22 AM
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 !
2021-08-05 12:29 PM
So, I don't use the C compiler and libraries, but to generate 2 MHz clk signal on PC4 is very simple:
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...