## Clock System (CLK)

The internal network of STM8 clock system allows us to tune up operating speeds of peripherals and CPU according to our needs. Software delays and power consumption depend on how the clock system is set.

In STM8 micros, there are three main clock sources – High Speed Internal Clock (HSI), High Speed External Clock (HSE) and Low Speed Internal Clock (LSI). The HSI has an oscillating frequency of 16MHz and is an internal RC oscillator with good precision – about 1% tolerant over a wide temperature range. HSE can be an external clock circuitry, temperature-compensated crystal oscillator (TCXO) or ordinary crystal resonator. It accepts all frequencies from 1MHz to 24MHz. Lastly, LSI clock is also an independent internal RC oscillator-based clock source that is mainly intended for idle or low power operating modes and the independent watchdog timer (IWDG). It has a fixed factory calibrated operating frequency of 128kHz and is not as accurate as HSI or HSE. There are also clock dividers/prescalers at various points to scale clocks as per requirement. Mainly two prescalers are what we need – the HSI prescaler and the CPU divider. Peripherals are directly feed by the main clock source. Additionally, there is a clock output pin (CCO) that outputs a given clock frequency. It can be used to clock another micro, generate clock for other devices like logic ICs. It can also be used as a free oscillator or perform clock performance tests. There’s fail-safe clock security that makes HSI backup of HSE. Should HSE fail, HSI takes over automatically. Check the internal block diagram below:

#### Code Example

The code example below demonstrates how to run the CPU at 2 MHz clock using HSI and extract 500 kHz clock output from CCO pin using CCO output selection. HSE is divided by 8, i.e. 16 MHz divided by 8 equals 2 MHz. This 2 MHz is the master clock source and further divided four times to get 500 kHz.

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                      GPIO_PIN_0
#define LED_port                     GPIOD


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);

  CLK_CCOConfig(CLK_OUTPUT_CPU);
  CLK_CCOCmd(ENABLE);
  while(CLK_GetFlagStatus(CLK_FLAG_CCORDY) == FALSE);
}

void GPIO_setup(void)
{
  GPIO_DeInit(LED_port);
  GPIO_Init(LED_port, LED_pin, GPIO_MODE_OUT_OD_HIZ_FAST);
}

#### Explanation

The full explanation of this code is given in the last segment of this article. The only thing I’ll describe here is this part:

CLK_CCOConfig(CLK_OUTPUT_CPU);
CLK_CCOCmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_CCORDY) == FALSE);

These lines select the clock source that the CCO pin will output, enable the CCO module and wait for it to stabilize. Here I selected the CCO to output CPU clock.

### Related Posts

• ABHINAY MANANDHAR

can anyone tell me how to use alternate function of the pin using pheriphral library?

• for which MCU?

• ABHINAY MANANDHAR

stm8s003f3

• There is no alternative GPIO function in this device. Could you be specific why you are looking for alternative GPIOs?

• ABHINAY MANANDHAR

HI,

Can you tell me the procedure to interface Seven segment displace because in th program, I cant find any thing related to seven segment, Like how you are providing serial clock and latch input to seven segment display?

thank you

• Timer Interrupt (TIM4) example discusses about seven segment display. Have you not seen it?

Hi,
I am new to this stm8 .

I have a small doubt .
my doubt is “while configuring the interrupts, why we are commented or un-commented the particular part of the code
in the BASIC INTERRUPT VECTOR TABLE FOR STM8 devices file ”

//@far @interrupt void NonHandledInterrupt (void)
//{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
// return;
//}

this part you used in the above code .

Any specific reason for this commenting this section.

sorry for my poor English.

• ABHISHEK SABNIS

Great work on this document. Highly appreciated.
I am using STM8S105K4 with PB0, PB1 PB2 and PB4 as ADC input pins. I have In my application i see when i use ADC measurement using following functions:

I am seeing noise on PB0 pin and channel PB1, PB2 are reading 0 (even though there is significant voltage on pin) value when application is debugged. When i turn off ADC function completely noise goes away. so there is definatly some interaction on board with ADC noise. i am using HSI clk with 1Mhz frq.

• Anantha narayanan

Good article. I’m new to stm8. i was using stm32 till date. now I chose stm8s001j3 controller.
I’m able to compile code & open the light programmer. but it ends up saying “error: The specified module could not be found”
Am i missing something with respect to swim connection? I have left swim pin connected to ground & then is connected to ST-LinkV2.
kindly help out. Thanks.
BR,
Anantha R

• seems like a programmer software issue….

• Raymond

Good tutorial, thanks, but I have a question about the GPIO configuration of SPI MOSI. In the SPI example code of STM8S/A Standard Peripherals Firmware Library, the GPIO of SPI MOSI, MISO are not configured.

• If you setup SPI hardware, the MOSI-MISO GPIOs are automatically sets….

• Digvijay Kapdi

i use the code for my STM8S003F3P6 controller & i have changed the PIN PC2 and PC3 to Port B (due to PC2&PC3 is not available in stm8s003f3) also make changes in lcd.h &lcd.c. but while Build i found below errors. and i don’t know the meaning of error, kindly help me.

Compiling lcd.c…
cxstm8 +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ lcd.c
lcd.c:
The command: “cxstm8 +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ lcd.c ” has failed, the returned value is: 1
exit code=1.

uv_lcd_stvd_1.elf – 3 error(s), 0 warning(s)

so kindly give me the solution because i don’t understand why this error is appear.

• Ghanshyam

Is there anyway to send a float to the display? Or cast a float to a string to use the LCD_putstr function that is written?

• Yes there’s a way…. I’ll send you in your gmail address….

• Yes,same configuration for both adc1 0/1 channel.

{

DISABLE,

“same for channell 1″….then:

• First of all a big thank you for the work done!

I have a issue with ADC1 in stm8s105k4 device:for my project i have used ADC1 in scan mode (i have to measure two analog voltage),I used the code that you described in your course (Multi Channel ADC with Scan Mode).
I have enable AIN0(on PB0) and AIN1(on PB1) but the result is that only the reading on PB0 is continuos updated,the value on PB1 is updated only one time (after reset).
Do you have any suggestions?
Thanks!

• Both channels have same settings?

• Navneet Tripathi

Hi,
I am trying to compile the Source code for SM8S, and getting error
#error cpstm8 ..\..\stm8s_eval.h:127 “Please select first the STM8S EVAL board to be used (in stm8s_eval.h)”

I ALREADY DEFINED THE BOARD UNDER TOOL CHAIN, COULD YOU TELL ME WHERE I CAN DEFINE THE EVAL BOARD, ”

REGARDS,

• I never tried to use this…. Can’t help in this regard….

• Atendra Pathak

You can try uncommenting the line respective to eval board you are using in “stm8s_eval.h”.

• Asif Karimi

thank you so much sir for sharing good tutorials for us. i complete all your tutorials also done my minor project in stm8s. love from india.

• Many thanks….

• clnk -l”C:\Program Files (x86)\Common Files\Lib” -o Debug\alpha_lcd.sm8 -mDebug\alpha_lcd.map Debug\alpha_lcd.lkf
#error clnk Debug\alpha_lcd.lkf:79 Debug\stm8_interrupt_vector.o: symbol f_NonHandledInterrupt multiply defined (Debug\stm8s_it.o)
The command: “clnk -l”C:\Program Files (x86)\Common Files\Lib” -o Debug\alpha_lcd.sm8 -mDebug\alpha_lcd.map Debug\alpha_lcd.lkf ” has failed, the returned value is: 1
exit code=1.

• lcd file add file stm8s_it.c compile error

• lcd and external interrupt compile error

• Asif Karimi

Compiling ..\stm8s_3f3_library\src\stm8s_it.c…
cxstm8 -i..\stm8s_3f3_library\inc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ ..\stm8s_3f3_library\src\stm8s_it.c
this error– ” #error cgstm8:0 x() unknown space: intrrupt ”
..\stm8s_3f3_library\src\stm8s_it.c:
The command: “cxstm8 -i..\stm8s_3f3_library\inc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ ..\stm8s_3f3_library\src\stm8s_it.c ” has failed, the returned value is: 1
exit code=1.

intrupt.elf – 3 error(s), 0 warning(s)
i dont know what i can do

• Asif Karimi

Thank you so much for shering this kind of information. Sir something wrong with me after extract zip (spl) file i cannot find config.h and flash.h , please sir tell me what i can do ?

• Ted Jackson

Thank you, Shawon and Embedded Lab for these wonderful tutorials. I’d just like to ask if you’ve added a (preferably linked) index of their titles somewhere? As far as I can tell, one has to select a blind number (above) before one can tell what subject it’s a tutorial about!

• Recommend that you use the PDF document of the tutorial.

• Ted Jackson

Thank you for the example code. What series of steps should one take in order to migrate your code to a different MCU? I have an STM8TL52F4.

• In the section “Preparing the Software Tools” I discussed what you need.

• Abhishek Sabnis

I used scan mode example from your example files, but its not updating ADC result when i change voltage on ADC pin. It keeps showing initial value.

• Naveed Ahmed

Hello Sir
Sir i am little bit confuse about code size its ” s19 ” files is too big to controler memory
how can we know about our code how big it

• Abhishek Sabnis

I am trying to use one ADC channel PB0 for ADC interrupt
I am trying to use another channel PB3 to measure voltage.
How should i use ADC here?
If i use ADC interrupt PB0 with single channel and want to use PB3 for measurement of voltage using single channel ADC,
how processor would know which channel i am scanning?

• Use scan mode conversion when you are using multiple ADC channels….

• Abhishek Sabnis

Thanks for your comment. I used scan mode but its not updating ADC result when i change voltage on ADC pin. It keeps showing initial value.

• Naveed Ahmed Siddiqui

I love You Very Well this side i am very happy to work on this controller Thank again

• Hello!
And HUGE thanks for a one of the best documenting on “Embedded-lab Starting STM8 Microcontrollers” projects.
I am trying making SPI demo with MAX7219- based scrolling dot-matrix display and a STM8S003k3, but there is something wrong with my debugging giving an error: “#error clnk Debug\matriisi2.lkf:1 symbol _delay_ms not defined (Debug\main.o Debug\max72xx.o )”

Im Not sure if I copied the MAIN.c, MAX72XX.h MAX72xx.h- files accordingly because of copy/paste function from “Code Example section”

P.S. PC4 SPI__SS does not work for my STM8CUBEMX and PE5 shows SPI_NSS?!?!?!

• Seems like software delay library is missing from your project….

HMM…Obvioysly software delay library is missing, but…. I´m I totally lost trying to solve the problem by using the section “Creation & Addition of libraries” under “Some Useful Tips”? Please tell me where to find this “stm8s_delay.h”-file. Only I find is a STM8L15x “delay.h” which hardly suitable for my STM8S ?!?!?

My workaround was to use function “void delay(unsigned int n)” I dont know if the trick is an acceptable programming practice but VOILA!!!! I can see and read the text from my Matrix-LCD.

ER

• You can find stm8s_delay.h and stm8s_delay.c files in inc and src folders of project folders like LCD example project folder….

• Abhishek Sabnis

Hello Shawon,

Thanks for taking effort to put such a nice document on STM8. I would like to have multiple interrupt routines for multiple pins on PORT B. how can i declare multiple routines under PORT B. Here you are showing Port B Pin 7 routine. I would like to have Port B Pin6 also as external interrupt. How can i declare its routine seperatly like EXTI1_IRQHandle.

• Since there is one interrupt vector for each port, you have to read needed GPIO pins of the respective port once interrupt triggers. In this way you can have multiple external interrupts on same port.

• Abhishek Sabnis

I have similar question regarding ADC usability. In my application i will be using multiple ADC pins. How can read ADC value of specific pin. I am refering to your ADC continuous conversion example.
In that exaple you have PB0 as ADC pin and when you call ADC1_startcoversion(); it reads PB0 and you collect result at A0= ADC1_GetConversionValue();
Now, if i have multiples ADC pin how can i get conversion value from intended pin? for e.g. PB1(AN1) and PB2(AN2)

• Abhishek Sabnis

How to find sampling period of ADC in this example. I will probably measure 50 samples and take average.

Thanks once again to put together very good document.

• Hi! I’m trying to run the Timer 1 PWM program on my Discovery Board (105C6T6 MCU). I’m testing it on all the channels of Timer 1. I get complementary PWM outputs from channel 2 on my oscilloscope. But for channel 1 and 3, only the complementary pins i.e. CH1N and CH3N pins give outputs while their counterparts only show some noise on the scope. Also channel 4 gives proper output. I’ve also tried with the code example given in the Standard Peripheral Library, but the same thing happens. What could be the reason? Please help!

• Hi! I’m using your codes on my DISCOVERY BOARD (STM8S105C6T6 MCU) . Every time I try to debug a program on STVD it says “configuration error: detected mcu different from the selected one”. I’ve tried to change the MCU from Project settings => MCU selection, but to no avail. Please help!

• Also I’ve uncommented my MCU in the stm8s.h file and commented out the previous one. Is there anything else that needs to be done?

• Solved it. Thanks!

• muharrem çetinkaya

Hi Shawon Shahryiar,

I am sorry for my English,
I’m using translate.

Did you use software uart with stm8?
can you share a example study?

• Didn’t do such but I do have software UART example for other microcontrollers….

• dharanipathi

Hi Shawon Shahryiar

“stm8s conf.h” not there in stm library pls upload in drive and share the link for it

• Everything has been included…. Please check: http://embedded-lab.com/blog/stm8-microcontrollers-final-chapters/10/

• dharanipathi

thank you , you have used in many programs like this delay_ms(20) but if i use this it is showing missing prototype so where you have defined it if you have that c or header file pls drop a link of that

• dharanipathi

my mail id is dharanipathi002@gmail.com can you send those delay header and c file (delay.h and delay.c)

• Showing “Missing Prototype” means that “stm8s_delay.h” is missing from your project…. Everything has been included…. Please check: http://embedded-lab.com/blog/stm8-microcontrollers-final-chapters/10/

• dharanipathi

stm8s_delay.h and .c is not there i have searched fully

• Of course it is there…. thousands of people have read and used the codes of this blog….

• dharanipathi

thank you got it sorry for disturbing thank you Shawon Shahryiar

• SANDIP NAKRANI

Hi Shawon Shahryiar,
I have debug time base example, but not getting any timer output. Actually i am not able to start any of timer. I have also tried with your example but same result. Am i doing anything wrong or something missing?
Hardware : STM8S003F3

• As per my demo videos, things are working in my case…. I guess you are making a mistake somewhere or overlooking something….

• SANDIP NAKRANI

I am using simulator for debug, Not hardware. In simulator HSICLK_RDY bit always remains zero, So in simulator it is possible to check or not?

• I don’t believe in simulations and so I go for real hardware tests….

• hi Shawon , i tried this for kit stm8s discovery with 105C6T6 and have nothing on the display of LCD . can u help me !!! tks a lot , brate !!!

• Which LCD? Text LCD/OLED/TFT LCD?

• LCD 16X6 bro !!

• 16×2 ,sr

• i hope u can send me this project.

• text LCD 16×2

• R/W pin grounded and the pins connected to the MCU as in the code? Is it a 3.3V compatible LCD or a 5V one? At what voltage the MCU is running?

• Hi, just wanted to ask where did you get the documentation for the SPL. The pic in your GPIO tutorial, where did you get that from ?

• Got the documentation , thankz for your tutorial , it really helped a lot.

• 🙂

• Hi sir, I’m using stvp + cosmic for almost a year. Recently as I compile there happens errors as follows: flexLM features has expired. I think my cosmic licence has expired. Thus I want to know what can I do? Can I get licence like the first time?

• Yeah just like first time you have to reacquire a new license from Cosmic and reapply….

• Excuse me shawon, I’m a bjt confused. I should resend the “CM8_license.txt” file to cosmic and how can I reapply the new license?should I replace it with previous license? I couldn’t find the previous license file. I don’t know where did I copy it…

• First go to this location of your PC: “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8″…. Run LmregFSE.exe file and fill in the infos…. You send them the generated info via email and they will send you a new license file…. Copy-paste and overwrite the license file they will send here: “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\License”…. Your license would then be renewed for another year…. Simple…. 🙂

• I sent it for cosmic but the problem is that I don’t remember where I copied license.lic. It’s not in “C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\License” I must overwrite the new license. how can I find previous license location?

• Shawon I’ve really progressed with your masterly article. I have a question; in “interupt.vector.c” you said we should comment the “@far @interrupt void nonHandledInterrupt(void)” ( in above the interrupt.vextor.c file ) but as I comment this function I got 33 errors for 33 nonHandeledInterupts… so I didn’t do that and I’ve never had an issue yet. I want to know does it make a problem? Is it necessary to comment that function??

• No not mandatory…. In my case, it was needed to be so….

• Hi shawon☺ , I used an External interupt for connecting a switch to micro. The problem is that when I push the button the ISR repeated many times but I want my ISR run only a time. Can you help me please??

• Firstly did you use pull-up and glitch removal capacitor across the switching point? Secondly, you can remove this repetitive interrupt by disabling external/global interrupt once an interrupt has been detected and reenabling external/global after processing the ISR….

• Thanks a lot shawon. I’ve tried to achieve real time. For do that I used 11.0592 Mhz crystal and timer2 on prescaler 1024, timer period: 108. With this regulation the interupt must occurs every 1ms. But it doesn’t work probably . In other words it has 1 second difference in a minute. What’s the problem?

• With these values you should be getting 10ms and certainly not 1ms…. (1024 x 108) / 11059200 = 0.01

• Yes you right. I want to make a stop watch so it’s the right value for me. (.01 × 100 = 1) . It’s 1 sec behind the real time every 2 minutes…

• That’s because it is not based on a real time clock (RTC)…. It is rather a rudimentary RTC and won’t be accurate enough as a stopwatch….

• So there isn’t any way to do that?? What about timer 4? Another type of stm8s micro?

• You can use a crystal resonator instead of quartz crystal or dedicated timer/counter chip for timing….

• Hi shawon, I had a look at register map and I realised that the reset value of ARR register is 0xFF provided other control register reset value are 0x00, thus I’ve putted timer period on 107 and it’s been extremely precise. What do u think about it?

• Hi Gents,

I’m using the IAR environment and tried to use the delay feature from these tutorials. Unfortunately I
meet an issue with the dealay function.
I have imported both .c & .h files to the project and tried to use the delay_ms(); but during compilation I see error like below:
“Error[Li005]: no definition for “_asm” [referenced from D:\…\Debug\Obj\stm8s_delay.o].

I have no idea what can be wrong, maybe I missed add some definitions, perhaps someone could explain it to me?

• I have no idea about IAR compiler…. Can’t help in this regard….

• I see
Thanks for response

• Hey guys;
Could you help me please, I’ve copied the Tmer Interupt(TIM4) project, but when I built it, there was an error:
“@svlreg missing for function f_TIM4_UPD_IRQHandler”
I just recomment the top of “interupt_vector.c” because I had 33 errors.

• It should work as I have shown…. Somewhere you are doing something wrong….

• Maybe, why STVD doesn’t permit me to write in libraries, I have to run them with Subline text and make changes there. Afterthat I can do it in STVD.

• Hey shawon; when I use alphanumeric LCD’s function in my ISR that error apears.what should I do?

• You should keep ISR as short as possible…. You must try to avoid external function calls in ISR….

• What a pity! I want to use external functions especialy LCD when an event occurs. Is’t there any way to do it??

• Use software-based flags that get triggered on interrupt…. In the main, check the flags and update the display…. Alternative use a timer to periodically check the flags and then update the display….

• ——- Project stm8s103 – STM8 Cosmic – Configuration Release ————-

“on release option i encounter this error what is the solution for this

Compiling ..\..\main.c…
cxstm8 +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clRelease\ -coRelease\ ..\..\main.c
#error cpstm8 ..\..\main.c:29 can’t open stm8s.h
#error cpstm8 ..\..\main.c:46(12+8) missing )
#error cpstm8 ..\..\main.c:60(11) missing prototype
#error cpstm8 ..\..\main.c:60(45) missing prototype
#error cpstm8 ..\..\main.c:65(21) missing prototype
#error cpstm8 ..\..\main.c:65(55) missing prototype
#error cpstm8 ..\..\main.c:66(9) missing prototype
#error cpstm8 ..\..\main.c:60(45+10) GPIO_PIN_0 undefined
#error cpstm8 ..\..\main.c:60(12+5) GPIOA undefined
#error cpstm8 ..\..\main.c:60(60+25) GPIO_MODE_OUT_PP_LOW_FAST undefined
#error cpstm8 ..\..\main.c:76(11+8) missing )
#error cpstm8 ..\..\main.c:76(26) old style argument declaration
#error cpstm8 ..\..\main.c:79(9+6) nCount undefined
..\..\main.c:
The command: “cxstm8 +mods0 -pp -i”C:\Program Files\COSMIC\FSE_Compilers\Hstm8″ -clRelease\ -coRelease\ ..\..\main.c ” has failed, the returned value is: 1
exit code=1.

stm8s103.elf – 15 error(s), 0 warning(s)

• Seems like the compiler doesn’t know the location of the source and header files…. Go to Project >> Settings…. Select Debug/Release and check the debug tab there…. Locations for the header and source files should be shown there or else you’ll get this sort of error….

• yes, sir as per your video i did the process , but previously i use st’s standard peripheral library “template” to check GPIO ready project. in st’s standard library ” Example project” compiled in debug mode but in release mode it gives above error. then i follow your video process and error gone hex.file generated in debug and release mode too. my request is to make separate page for stm8 and explain the st’s standard peripheral library and your project list for stm8 only with separate section.Thanks.

• Good to know that your issue is fixed…. But I didn’t understand your request on standard peripheral library….

• Hi SHAWON SHAHRYIAR,

I have been working through your articles and trying to understand as much as possible. I am currently looking at the AWU,

I have brought in the project, compiled and built with no errors. 🙂

However when running the program the CPU goes into the halt mode, and never Auto wakes up! 🙁

Is there some more work that has to be done in the interrupt side of things? Does the project need the ITC.c and ITC.h files and some adjustments in there?

Could I also have the delay.c and delay .h files please.

Andy.

• By the way, I am using the STM8SVL Discovery with the 003K processor on it also so all hardware is identical.

• No, nothing needs to be done in the interrupt “itc” files but you need to add some lines in the “it” interrupt files:

@far @interrupt void AWU_trigger_IRQHandler(void);

and in the “it” source file add the following lines:

void AWU_trigger_IRQHandler(void) { AWU_GetFlagStatus(); }

Change the interrupt vector file as shown below:

 #include "stm8s_it.h"

 typedef void @far (*interrupt_handler_t)(void); struct interrupt_vector { unsigned char interrupt_instruction; interrupt_handler_t interrupt_handler; }; //@far @interrupt void NonHandledInterrupt (void) //{ /* in order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction */ // return; //} extern void _stext(); /* startup routine */ 

struct interrupt_vector const _vectab[] = { {0x82, (interrupt_handler_t)_stext}, /* reset */ {0x82, NonHandledInterrupt}, /* trap */ {0x82, NonHandledInterrupt}, /* irq0 */ {0x82, AWU_trigger_IRQHandler}, /* irq1 */ {0x82, NonHandledInterrupt}, /* irq2 */ {0x82, NonHandledInterrupt}, /* irq3 */ {0x82, NonHandledInterrupt}, /* irq4 */ {0x82, NonHandledInterrupt}, /* irq5 */ {0x82, NonHandledInterrupt}, /* irq6 */ {0x82, NonHandledInterrupt}, /* irq7 */ {0x82, NonHandledInterrupt}, /* irq8 */ {0x82, NonHandledInterrupt}, /* irq9 */ {0x82, NonHandledInterrupt}, /* irq10 */ {0x82, NonHandledInterrupt}, /* irq11 */ {0x82, NonHandledInterrupt}, /* irq12 */ {0x82, NonHandledInterrupt}, /* irq13 */ {0x82, NonHandledInterrupt}, /* irq14 */ {0x82, NonHandledInterrupt}, /* irq15 */ {0x82, NonHandledInterrupt}, /* irq16 */ {0x82, NonHandledInterrupt}, /* irq17 */ {0x82, NonHandledInterrupt}, /* irq18 */ {0x82, NonHandledInterrupt}, /* irq19 */ {0x82, NonHandledInterrupt}, /* irq20 */ {0x82, NonHandledInterrupt}, /* irq21 */ {0x82, NonHandledInterrupt}, /* irq22 */ {0x82, NonHandledInterrupt}, /* irq23 */ {0x82, NonHandledInterrupt}, /* irq24 */ {0x82, NonHandledInterrupt}, /* irq25 */ {0x82, NonHandledInterrupt}, /* irq26 */ {0x82, NonHandledInterrupt}, /* irq27 */ {0x82, NonHandledInterrupt}, /* irq28 */ {0x82, NonHandledInterrupt}, /* irq29 */ };`

Sorry for the trouble…. I probably forgot to state these in the code and explanation segments…. Will be updating the post…. Thanks for this catch…. 🙂

• tinkerer

Hey,
Grateful for the tutorial! Nice work.
I tried this section of your tutorial and got 33 errors after building:
it was due to commenting of :

@far @interrupt_handler_t void NonHandledInterrupt (void)
{
return;
}

because these were used in the interrupt vector struct again. I uncommented those lines and got one error:
“unknown space: interrupt_handler_t”

what should i do now?
Thanks alot.

• You are getting this error for which interrupt example?

• Hi,
Firstly, thanks alot for your tutorials. It helps alot for beginners in STM8S because there are not much documentation for it.
Secondly, I have been trying to implement the Timer interrupt tutorial(pg. 19) and wrote the code as instructed, however I am getting this error of “invalid pointer initializer” when I added the interrupt
{0x82, (interrupt_handler_t)TIM2_IRQHandler}, /* irq13 */

in the interrupt_vector.c file as was instructed in your tutorial. Can you please help me around this issue? What am I doing wrong here? Any suggestions would be of great help.

• Hi

Thank you for the post,
I ve used your code for another sensor and noticed that in the i2c read routine you’ve forgot to send a stop to release the clock line. Modification is as follows

..

Regards

{
unsigned long value = 0x0000;
unsigned char num_of_bytes = 0x02;
unsigned char bytes[2] = {0x00, 0x00};

while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

while(num_of_bytes)
{
{
if(num_of_bytes == 0)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}

num_of_bytes–;
}
};

/// the following line should be added
I2C_GenerateSTOP(ENABLE);

….

}

• Noted with thanks…. 🙂

• I reviews the code and found that I added the I2C stop command here:

if(num_of_bytes == 0)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}

So there is no need to add stop command after the loop….

• Hi

You are right, it was due to my modification.
Anyway I just wanted to thank you for the postings.

Regards

• 🙂

• Archana

Hello Sir,
I am trying to compile the code of led blink. It gives me error like this

Compiling main.c…
cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c
#error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:106(13) missing enumeration member
#error cpstm8 ..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc\stm8s_uart1.h:113(13) missing enumeration member
main.c:
The command: “cxstm8 -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” -i”..\..\..\..\users\rajes\documents\sisai technologies\software\stm8s lib\stm8s_stdperiph_lib\stm8s_stdperiph_lib\project\stm8s_stdperiph_examples\adc2\adc2_continuousconversion” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
exit code=1.

• Archana

Can you help me?

• Archana

• Add only the hardware files that your target chips supports…. For example, you cannot add ADC2 for STM8S003…. This is so as STM8S003 doesn’t have these hardware blocks…. Likewise if you are using STM8S207, you cannot add UART2 files for the same reason…. If you don’t add required files then you will get errors…. You’ll also get errors if you add files that are not applicable for your target MCU….

• Hi
I am using stm8s003f3 pin out board and with this code I am just testing output at micro controller pin 2 i.e. TX
I am testing this using an oscilloscope.
but not able to get any output

Thanks

• Did you recompile the code for your target chip?

• Yes I did.
But Not getting expected output

• Apart from the scope is there anything else connected with this pin? It should work as I have shown…. Can you try with a different board?

• hi
nothing else is connected to that pin. yes I did try another board. but the results are same.
TX pin is just going HIGH.

• hi
In STVD , STM8S003F3 MCU is not available but I am using this same so from the MCU selection list I have selected STM8S003F3P.
So does it make any difference??

• no it doesn’t….

• Hi Shawon
Any advice on how to code the Program to get the STM8s talking to the max31855?
This is causing me some problems.
JP

• SSI_Electronics

Hello
I have followed the code as per the explanation.
but I’m getting garbage values on serial port.
Can you suggest any debugging steps??

PS: Great tutorial. It’s helping me a lot.

• 1. Check wiring for loose connections….
2. Use a stable and noise-free power source like a battery-bank to power up the board…. Noise from PC port may cause unwanted issues….
3. Use CP2102 or FT232-based USB-TTL converter…. Don’t use PL2303-based ones because most are clones or fakes….
4. Use a short USB cable….
5. Use external pull-up resistors for both TX and RX pins….
6. Try with another board if these don’t solve the issue…. It should not behave as you are saying because the code has no issues….

Serial receiving program is not working

• Amit Kushwah

Hello sir
Do you tell me how generate pwm in pin no. B5 & B5
in Stm8 board

• Which STM8 chip? Because different chips have different pin mapping and sometimes there are alternative pins for such purposes…. Check with STM8CubeMX if that pin is available for that purpose….

i have done code for multi channel adc but its not working ????
when i use single channel adc its working properly…………

• Which channels are you using?

• Hello guys
here i’m again for ask your help. I wrote a very easy program but when i try to send to stm8 the light programmer give me this error :

Programming…
PROGRAM MEMORY:
>Erase not feasible
>Programming
Error: Device not recognized !

some idea ?
Thank you
Sergio

• Unlock it first with light programmer…. It seems you locked it probably….

• First thank you so much for this wonderful tutorial, i’m new with STM8 i follow all you steps but i still have a problem and here i’m please can you help me this is the error message.
Thank you
Sergio

———– Project test_prov – STM8 Cosmic – Configuration Debug ————-

Compiling main.c…
cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c
#error cpstm8 gpio\inc\stm8s.h:74 “Please select first the target STM8S/A device used in your application (in stm8s.h file)”
main.c:
The command: “cxstm8 -i..\..\en.stsw-stm8069\stm8s_stdperiph_lib\project\stm8s_stdperiph_template -igpio -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
exit code=1.

test_prov.elf – 2 error(s), 0 warning(s)

• It seems that you didn’t select the chip family or category….

• Yes it’s true really i don’t know why i find another stm8s.h inside External Dependencies folder maybe i make some mistake in the beginning.
Tank you
Sergio

• Venkatesh K

Hi Shawon ,
Excellent article on stms8.
within 3 days i was able to understand STMS8 very clearly.
can you send me delay library files please.
i tried to create my own without any success.
warm regards

• hi sir now also getting problem in compilation time …..’

so plz can u send me the soft copy of software wat properly working at u…??

my emain id…

imran.vectorindia@gmail.com

• I’m using the same software you are using – the latest one out there…. and so someone sending it has no meaning…. I suggest that you uninstall it and give it fresh retry, following all the steps carefully…. No one can help you with licensing issue unless to do it correctly….

• hi geting poroblem while click on programmer ten my softare automatically close tab

• The latest version of STVD has this issue…. I’m also facing this issue and so are many other like us…. Hope STM fixes this issue fast…. For now use STVD for coding and STVP for flashing….

• thanks sir

• hi sir…

i getting problem for license configured……………. at compile time getting error like that…………. plz help out

#error cpstm8 C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\HSTM8\mods0.h:1 FlexLM Cannot find license file.
main.c:
The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
exit code=1.

alpha_lcd.elf – 3 error(s), 0 warning(s)

• Did you follow the procedure as I showed?

• YES SIR

• Hemalatha.R

helo…we hav stm8svl discovery..i need embedded c program for the glowing of led,connecting ir receiver and also powersupply…for connecting these things i need code can u please guide me

I am looking for microsd card interface with STM8s discovery board. can you please provide the tutorial for microsd card?

• It is on my plan but not possible recently….

• In relation to disabling unused peripheral clocks as suggested by you:

I found the following list of peripherals in the STM8S105 Discovery Board: Advanced control timer (TIM1), General-purpose timers (TIM2 and TIM3), Basic timer (TIM4), SPI, I2C, UART2, Window WDG, Independent WDG, ADC1,
AWU timer, Beeper.

Is this all?

I could not find the clock peripheral configuration function for Window WDG, Independent WDG, Beeper in the Standard Peripheral Library.
Is it not required?

P.S. I am new to MCU programming with a little experience on Arduino. So please forgive me if the question is foolish.

• 1. STM8S003K3 Discovery – the one I use for the blog is different from STM8S105…. They have different peripherals and so there will be differences in peripheral clocks…. Please check the device datasheet/STM8CubeMX for available hardware….

2. WWDG and IWDG are set in option bytes and they don’t use peripheral/CPU clock…. Their oscillator is separate…. Due to these they don’t have clock configurations like other peripherals….

P.S: No question is a noob question…. every question is allowed no matter how silly it may look….

• Thank You 🙂

• Hi please someone tel me where can i find stm8s_delay.h and stm8s_delay.c
Thanks

• Check the inc and src folders….

• There is not . Could you send me please if you have it.

• mail sent….

• please, send me too the library delay

• Hi , please someone tel me . Where can i find stm8s_delay.h and stm8s_delay.c
Thanks ..

• keerthi prakash

i am using stms003f3p6 controller..i have done as u told exactly but tim1 input capture is not displaying any value …according to your article it should display 10…i dont have any errors everything is fine…pulse on tim2 is ok ..it is coming as u said…but capture not working..please help me …thank you…

• In STM8S003F3P6, TIM1 input capture CH1 is located in PC6 instead of PC1. Did you check this out?

• keerthi prakash

i changed that too..but no response tim2- 50hz as u said..but tim1 ch1 pc6 not capturing …still 000000 on lcd..

• Send me your full code so that I can check and debug it….

• Hi Sir,
i have just follwed your instruction provied in yout tutorial in main.c

#include “stm8s.h”

main()
{
while (1);
}

This error comes, can you please check why this error come, i am new and learn the controller programming.

main.c:
The command: “cxstm8 -i”d:\other datasheet\new folder\lib\stm8s_stdperiph_lib\libraries\stm8s_stdperiph_driver\inc” +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\CXSTM8\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
exit code=1.

main.o – 2 error(s), 0 warning(s)

Thanks,

• Dion Tan

Hi there, i solve the previous ADC problem. Anyway, u make some good tutorial on stm8 chips. Nice work

• What was causing that issue? How did you solve it?

• Dion Tan

because i put all these

in ‘while(1)’. It keeps looping.

Once i put it before ‘while (1)’, it becomes normal.

• Dion Tan

lol, sorry im noob

• dion tan

Hi there, thanks for sharing. I learn alot thru your example, but I have encounter a problem with adc. Im currently using stm8l151k4t6. I wanna use the pin D7 as to read the voltage from my battery and monitor it (display on LCD). Can u please check where it goes wrong on my code?

• dion tan
• What error or issue are you encountering?

Thanks.

• dion tan

LOL my bad, i did not mentioned what error. Basically the lcd is still displaying 4095 which is (2^12)-1, no matter what my voltage is at. I think the pin d7 is not read by the microcontroller

• dion tan

actually i did. U mean this line ?

It is still the same. I not really sure why. Can ADC1 works with several input ?

• Did you enable the clock for the ADC module?

• dion tan

yes I did enable the clock for the ADC module too. Basically i noticed that the pin is like not read by the microC. It just display the bits that I set. when I put 8 bit, it shows 255, 10 bits it show 1023 and so on.

• djalltra

hi shawon great tutorials it would be nice too if you covered the coide compiler for arm it’s a good free compiler

• I have plans to cover STM32 with STM32 System Workbench and STM32CubeMX….

• djalltra

hi shawon thanks for your response and support it turns out that my problem was a license issue I stupidly thought i use one license on multiple pc’s I used the license for a particular computer for mine that’s why I’ve been having compilation issues you have to use the license file stm8 sends you that is generated by your own pc else it won’t compile it’ll jus kip throwing you errors

• It seems that you didn’t follow my instructions completely…. I found some issues:

1. You have included header and source files of ADC2, CAN, UART2, etc which are unavailable in STM8S103…. Add only files for the peripherals available in your target MCU…. Exclude the rest….

2. You have not included stm8s_delay header and source files…. Either disable it from stm8s_conf.h header file if you are not going to use it or add both the header and the source files for it in your project….

Resolve these and you are good to go….

Thanks….

• djalltra

I haven’t compiled any of the examples yet I just tried to compile the main source file just for test purpose n it just keeps showing me the same errors

• djalltra

yes the comic compiler was installed properly i even received a license file from my mail with the instructions to copy the license file to the license folder in the install directory if you fail to do this it will keep popping up for you to put in the license file. iincluded all the source files and header files as instructed i am working with the cheap stm8s103f i uncommented the stm8s103f in the stm8.h header file as you instructed and just to compile the main file it just throws me errors

• Send me your project files and folder…. Let me see what’s going on….

• djalltra

no I just followed your instructions I just compiled it after setup i did not write any code yet just compiled the default main file

• Need more details….

1. The version of STVD and Cosmic
2. Which example is giving this issue?
3. The chip you are using if it other than STM8S003
4. Have you tried to compile something else other than my examples?
5. Are the paths to libraries and other folders properly added?
6. Is the Cosmic compiler registered properly?

• djalltra

it throws this line The command: “cxstm8 -iinc +debug -pxp -no -l +mods0 -pp -i”C:\Program Files (x86)\COSMIC\FSE_Compilers\Hstm8″ -clDebug\ -coDebug\ main.c ” has failed, the returned value is: 1
exit code=1.

• It is because you are making a simple silly mistake like a missing argument or semicolon somewhere in your code….

• djalltra

hi I’m having compilation issues when I compile it just throws me error about comic compiler located in the program file ialso excluded unwanted header files but my problems still weren’t solved

• djalltra

hi thanks for the tutorials but i am having compiler error issues even when I remove unnecessary header files it say comic compiler error

• Hi. What does the error state?

• I am new to STM8S003k3 Discovery board.I have done UART communication using STM standard peripheral library example. Now I want to define any other GPIO pin for UART communication. Is it possible to use d0 and d1 pin for the UART communication? If yes, then please let me know how?﻿

• It is possible to use any GPIO for UART communication using software UART. You’ll need to take care of character timings on your own using a timer.

• Alexander

• James Don

Thank you for your time and effort.

• James Don

Good job bro

• Randy Young

Thank you for this exceptionally comprehensive article, much appreciated

Rando!

• You are welcome…. 🙂

• pankaj verma

Hi Shawon Shahryiar,

I tried 16×2 LCD with stm8s003k3. here lcd donot display any thing.

I have followed all the things with the connection.

I have connect
#define LCD_PORT GPIOD

#define LCD_RS GPIO_PIN_2
#define LCD_EN GPIO_PIN_3
#define LCD_DB4 GPIO_PIN_4
#define LCD_DB5 GPIO_PIN_5
#define LCD_DB6 GPIO_PIN_6
#define LCD_DB7 GPIO_PIN_7