2014-11-19 03:13 AM
Hello
i�m new in STM8s105 microcontroller and cosmic c compiler.i wrote a small program:i set the clock with internall clock with prescaler 8:CLK_CKDIVR= 0b00011000;it means that my uc will operate at 2 MHz, right?i set the port D3 as output pin, push pull fast: PD_DDR=0B00001000; PD_CR1=0B00001000; PD_CR2=0B00001000; PD_ODR=0B00000000;in the main, i wrote this code:main(){ PD_ODR=0B00000000; PD_ODR=0B00001000; PD_ODR=0B00000000; PD_ODR=0B00001000; PD_ODR=0B00000000; PD_ODR=0B00001000; PD_ODR=0B00000000; PD_ODR=0B00001000; for (;;) {}}in my mind, if i set the port, it will in high level for 500ns, turn off for 500ms and so on... but in the scope i see times of 1us.what i do wrong?thanks a o lot! #stm8s105-ports-clock2014-11-19 08:27 AM
Hi,
Yes, it looks like the CPU clock is set to 2MHz.Can you see the resulting assembly code from your compiler, maybe it would explain something here.I did some similar tests but with different compiler, if I remember correctly it was compiled to a single cycle MOV instructions and it really took single cycle to set the port.But if your code takes two cycles it's still not that bad.2014-11-19 09:19 AM
2014-11-20 12:56 AM
I don't know, it should be single cycle.
Maybe it's about flash read time.The code in your assembly listing is not aligned to 32-bit/4-byte address so it would need two reads per instruction.It's quite possible it would run twice faster when properly aligned.2014-11-21 12:12 PM
2014-11-24 12:36 AM
Hello,
Given your source is a C file and not assembler then I would have expected the compiler to handle the alignment correctly. It should be possible to place the pin toggling code in a function and then align that function to a 4 byte alignment but somehow that feels a bit of a workaround!? Thanks, Ian2014-11-26 04:05 AM
2014-11-26 07:59 AM
Hi,
You can check the speed of a running clock on the CLK_CCO pin via the CLK_CCOR register. You can change the frequency of the running clock via the SYSCLK register. Does that help? Thanks, Ian2014-11-26 10:10 AM
2014-11-27 12:50 AM
Ah ok... well the code you showed previously looks to use one cycle to write a 0 to the pin and one cycle to write 1 one to the pin, so in theory with a 2MHz clock you'll see 500ns off and 500ns on. However, you appear to be seeing 1us off and 1us on, which implies a 1MHz clock.
That's why I said verify that you are actually using the HSI clock at the rate you think you are via the CLK_CCO pin. Did you correct the alignment of the code as mentioned by another forum user as what he said is correct? Thanks, Ian