stm8s---timer generates PWM (TIM2)

Source: Internet
Author: User

      • Description
      • Code

1 description
    • Using stm8s internal clock (HSI);
    • PWM mode 2;
    • Duty ratio is 50%, frequency is 2Hz (convenient test LED lamp);
    • PD2 The LED lights, PD2 port output PWM wave;
    • System clock Initialization is important: CLK_CKDIVR |= 0x08;
2 Code
/*tim2_ch3 PWMPD2 output*/#include <STM8S003F3P.h>voidClk_init (void){Clk_ickr|=0x01;//Enable internal high-speed clock HSI   CLK_CKDIVR|=0x08;//16m Internal RC via 2, System clock is 8M     while(! (clk_ickr&0x02));//hsi ready .Clk_swr=0xe1;//HSI Primary Clock source}voidINIT_TIM2 (void) {TIM2_CCMR3 |=0X70;//Set timer 23-channel (PD2) output compare three modesTIM2_CCMR3 |=0x04;//Output comparison 3 pre-load enableTim2_ccer2 |=0x03;//Channel 3 Enable, low active, configured as output    //Initialize the clock divider to 1, i.e. the counter clock frequency is Fmaster=8m/64=0.125mhzTIM2_PSCR =0x06;//Initialize the automatic load register to determine the frequency of the PWM square wave, fpwm=0.125m/62500=2hzTim2_arrh =62500/ the; TIM2_ARRL =62500% the;//Initialize comparison register to determine the duty ratio of the PWM square wave: 5000/10000 = 50%TIM2_CCR3H =31250/ the; TIM2_CCR3L =31250% the;//Start count; Update Interrupt deactivationTIM2_CR1 |=0x81;//tim2_ier |= 0x00; }voidInit_gpio (void){/ * Set to push-pull output, PD2 LED light * /   pd_ddr|=0x04;//Set PD2 port to output modePD_CR1 |=0x04;//Set PD2 port to push-pull output modePd_cr2 &=0XFD;pa_ddr|=0X08;//Set PA3 port to output modePA_CR1 |=0X08;//Set PA3 port to push-pull output modePa_cr2 |=0xf7;}voidMainvoid) {clk_init ();    Init_gpio (); INIT_TIM2 (); while(1);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

stm8s---timer generates PWM (TIM2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.