Skip to main content
Visitor II
November 19, 2014
Question

Clock x ports doubts

  • November 19, 2014
  • 14 replies
  • 3144 views
Posted on November 19, 2014 at 12:13

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-clock
    This topic has been closed for replies.

    14 replies

    Visitor II
    November 19, 2014
    Posted on November 19, 2014 at 17:27

    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.

    Visitor II
    November 19, 2014
    Posted on November 19, 2014 at 18:19

    hello

    i checked the assembly code, but it´s like ok:

    0x810a <main+2>             0x725F500F      CLR   0x500f              CLR   _PD_ODR 

    main.c:43 PD_ODR=0B00001000; 

    0x810e <main+6>             0x3508500F      MOV   0x500f,#0x08        MOV   _PD_ODR,#0x08 

    main.c:59 PD_ODR=0B00000000; 

    0x8112 <main+10>            0x725F500F      CLR   0x500f              CLR   _PD_ODR 

    main.c:60 PD_ODR=0B00001000; 

    0x8116 <main+14>            0x3508500F      MOV   0x500f,#0x08        MOV   _PD_ODR,#0x08 

    main.c:62 PD_ODR=0B00000000; 

    0x811a <main+18>            0x725F500F      CLR   0x500f              CLR   _PD_ODR 

    main.c:63 PD_ODR=0B00001000; 

    0x811e <main+22>            0x3508500F      MOV   0x500f,#0x08        MOV   _PD_ODR,#0x08 

    main.c:65 PD_ODR=0B00000000; 

    0x8122 <main+26>            0x725F500F      CLR   0x500f 

    i see in the assembly paper - PM0044 that MOV and CLR takes one machine cycle.... but in my tests i don´t see this..

    Visitor II
    November 20, 2014
    Posted on November 20, 2014 at 09:56

    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.

    Visitor II
    November 21, 2014
    Posted on November 21, 2014 at 21:12

    ok, how i make this alignment??

    Visitor II
    November 24, 2014
    Posted on November 24, 2014 at 09:36

    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,

    Ian
    Visitor II
    November 26, 2014
    Posted on November 26, 2014 at 13:05

    Hi

    but how i check the correct clock and change the status orf the port´s?

    how i set my clock to 2MHz and see a toggle of port of 500ns?

    it is very confuse to me.. in the pic of microchip, i can see the correct operation of this.

    Visitor II
    November 26, 2014
    Posted on November 26, 2014 at 16:59

    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,

    Ian

    Visitor II
    November 26, 2014
    Posted on November 26, 2014 at 19:10

    humm no....

    how i toggle a pin on/off with 500ns, in my example?

    if the machine cycle for the MOV command is 1, if i on/off a pin, the time will be 500ns, right?

    Visitor II
    November 27, 2014
    Posted on November 27, 2014 at 09:50

    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
    Visitor II
    November 27, 2014
    Posted on November 27, 2014 at 19:06

    I think the most feasible way to do the alignment is to put two NOP asm instructions before the gpio output block. It may work.

    I guess I was just lucky when I did the test and I got aligned instructions by chance.