1 questions
?? In the following test program, if the CLK_CKDIVR in the INIT_CLK () function is |= 0x08 and the '|' is removed, then the function implementation of TIM1 is the same as the preset (10ms interrupts once), However, the PWM frequency of the TIM2 is higher, and if added, the TIM2 function is the same as the preset (which produces 1Hz PWM), but the TIM1 cycle becomes longer;
?? Tried a lot of tests, both are invalid (buy the development Board and another GS from the artboard test is also the same).
2 Try
?? Since the stm8s uses the internal 16M high-speed RC oscillator By default and is 8-divided, the system starts the main clock at 2M. That is, CLK_CKDIVR = 0X18; If you go to assign value CLK_CKDIVR |= 0X08; The master clock is still the same as 0x18, but if the assignment is CLK_CKDIVR = 0X08, the main clock changes and becomes 8M. But TIM1 and TIM2 's fmaster clock should be the same, so that the assignment should have an impact on TIM1 and TIM2, but two different assignments, the implementation of the function is correct, and only one can be set to work by default, what is the matter?
?? When the discovery is set to CLK_CKDIVR = 0x08 (8M), TIM1 can work according to the preset, and TIM2 's working frequency is obviously faster, like a faster, with this discovery, I will now TIM2 the Fmaster clock frequency as a TIM1, that is, 16M, Again to reconfigure the TIM2 register, and then compile, download into the microcontroller, incredibly and TIM1 realize the same function. What the hell is going on here? Is TIM2 's fmaster clock always a TIM1 of the Fmaster clock? try several different clk_ckdivr (of course the TIM1 fmaster frequency cannot exceed 8M).
?? The next test is CLK_CKDIVR = 0X10; (4M), CLK_CKDIVR = 0X18; (2M). Whether the speculation is correct, or do not understand what is going on. Looking at stm8s's clock tree, I can't figure it out.
3 Test procedure
/ * MAIN. C FILEFUNCTION:TIM1 Timer cycle interrupt to achieve LED1 period of 2S, TIM2 with PWM wave to achieve the same function, TIM1 10ms timer cycle interrupt, TIM2_CH3 PD2 output period of 1Hz PWM;PR Oblem: (1) in the clock initialization function INIT_CLK (), the problem of |= and =, plus ' | ' With no, TIM1 and TIM2 always have one of them that is not the same as the expected implementation. */#include <stm8s003f3p.h>_boolPD2 @PD_ODR:2;_boolPC7 @PC_ODR:7;#define LED1 PD2#define LED2 PC7voidINIT_CLK (void){//When it is ' |= ', the actual CLK = 2M //when it is ' = ', the actual CLK = 8MCLK_CKDIVR =0X08;}voidInit_gpio (void){/*LED/PWM configured for push-pull output * /Pd_ddr |=0x04;//PD2--PWM Output---connection LED1PD_CR1 |=0x04; Pd_cr2 &=0XFD; Pc_ddr |=0X80;//pc7---LED2;PC_CR1 |=0X80; Pc_cr2 &=0X7F;}voidINIT_TIM1 (void) {_asm ("Sim"); Tim1_ier =0x00; TIM1_CR1 =0x00; Tim1_egr |=0x01; TIM1_PSCRH =199/ the;//2m system Clock f=fck/(pscr+1) TIM1 16-bit dividerTIM1_PSCRL =199% the;//f=8m/(199+1) =40000hz, each counting cycle 1msTIM1_CNTRH =0x00; Tim1_cntrl =0x00; Tim1_arrh = -/ the;//Auto Reload Register arr=400TIM1_ARRL = -% the;//400 times per number of interrupts, i.e. 10msTIM1_CR1 |=0x81; Tim1_ier |=0x01;}/*tim1---8M TIM2---4m*/voidINIT_TIM2 (void){//tim2_ier = 0x00; Prohibit various interrupts //tim2_egr |= 0x01; If you want a new Prescaler value to take effect, you must generate an update eventTIM2_CCMR3 |=0X60;//Set timer 23-channel (PD2) output compare three modesTIM2_CCMR3 |=0X08;//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/128=0.0625mhzTIM2_PSCR =0X07;//Initialize the automatic load register to determine the frequency of the PWM square wave, fpwm=0.0625m/62500=1hzTim2_arrh =62500/ the; TIM2_ARRL =62500% the;//Initialize comparison register to determine the duty ratio of the PWM square wave: 50%TIM2_CCR3H =31250/ the; TIM2_CCR3L =31250% the;//Start count; Update Interrupt deactivationTIM2_CR1 =0x01;} Main () {INIT_CLK (); Init_gpio (); INIT_TIM1 (); INIT_TIM2 (); LED1 =1; LED2 =1; _asm ("Rim"); while(1);} @far @interruptvoidTim1_upd_ovf_trg_brk_irqhandler (void){Static unsigned inti =0; TIM1_SR1 &=~ (0x01); ++i;if( -= = i) {LED2 = ~led2; i =0; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
stm8s---TIM2 clock problem that generates PWM and TIM1 timer cycle interrupts