Skip to main content
Visitor II
February 28, 2016
Question

stm8s003f channel 1 PWM

  • February 28, 2016
  • 1 reply
  • 698 views
Posted on February 28, 2016 at 07:02

Hi,

please let me know how to use port b as output port in stm8s003f mode.

Thanks
    This topic has been closed for replies.

    1 reply

    Visitor II
    March 5, 2016
    Posted on March 05, 2016 at 23:16

    I tested my instructions with STM8S103F3P6, but they will probably also work with STM8S003F3P6. (I am using SDCC, but something like this should work in the other compilers.)

    First, using something like STVP, change AFR0 to ''Port C5 Alternate Function = TIM2_CH1, Port C6 Alternate Function = TIM1_CH1, Port C7 Alternate Function = TIM1_CH2''. (That is, AFR0=1, so OPT2 = 0x) Next, configure the PWM normally:

    #include <stm8/stm8sh>
    void
    main() {
    CLK_CKDIVR = 0x00; 
    // Set the frequency to 16 MHz
    CLK_PCKENR1 = 0xFF; 
    // Enable peripherals
    TIM1_ARRH = 1023>>8; 
    //ARR = 1023
    TIM1_ARRL = 1023&0xff;
    TIM1_CCMR1 = 0x68; 
    //PWM mode 1, preload on compare 1
    TIM1_CCER1 = 0x01; 
    //active high, enable TIM1_CH1
    TIM1_BKR = 0x80; 
    //enable all TIM1 outputs
    TIM1_CR1 = 0x81; 
    //enable auto reload preload, enable timer
    TIM1_CCR1H = 500>>8; 
    //duty cycle: TIM1_CCR1/(ARR+1) so it is 500/1024
    TIM1_CCR1L = 500&0xff;
    while
    (1);
    }

    This puts a 500/1024 duty cycle on PC6. Hope this helps you.