Code Example
The code example below demonstrates how to run the CPU at 2MHz clock using HSI and extract
500kHz clock output from CCO pin using CCO output selection. HSE is divided by 8, i.e. 16MHz divided
by 8 equals 2MHz. This 2MHz is the master clock source and further divided four times to get 500kHz.
Note CCO pin is only available in some pins. For example, in STM8S003K3 this pin is either PD0 pin or
PC4. We will need to override the default function of PD0 pin to favour for CCO output. To do so, we
will need to change Alternate Function (AFR5) configuration bit during code upload.
#include "STM8S.h"
#define LED_pin
#define LED_port
void setup(void);
void clock_setup(void);
void GPIO_setup(void);
void main(void)
{
setup();
GPIO_WriteLow(LED_port, LED_pin);
while(TRUE)
{
};
}
void setup(void)
{
clock_setup();
GPIO_setup();
}
void clock_setup(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_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV4);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
GPIO_PIN_0
GPIOD
Need help?
Do you have a question about the STM8 and is the answer not in the manual?
Questions and answers