STM8 timer TIM1 timing

Publisher:石头上种庄稼Latest update time:2020-02-04 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The TIM1 timer of ST's STM8 microcontroller is a 16-bit advanced control timer that can be used to achieve basic timing and PWM wave generation. Here we mainly analyze the basic timing function.


Let's first look at the initialization function of the TIM1 timer in the library function provided by ST


void TIM1_TimeBaseInit(uint16_t TIM1_Prescaler,

                       TIM1_CounterMode_TypeDef TIM1_CounterMode,

                       uint16_t TIM1_Period,

                       uint8_t TIM1_RepetitionCounter)


Among them, TIM1_Prescaler is a 16-bit frequency division configuration, and the input value can be 0-65535. This value is finally written into the TIM1_PSCR register, and the formula for calculating the timer frequency is: fcnk = fsys/(PSCR[15:0]+1), where fsys is the system clock frequency and PSCR[15:0] is the 16-bit value of the TIM1_PSCR register;


TIM1_CounterMode is the counting mode, there are 5 modes in total, namely:

TIM1_COUNTERMODE_UP Up counting mode

TIM1_COUNTERMODE_DOWN Down counting mode

TIM1_COUNTERMODE_CENTERALIGNED1    

TIM1_COUNTERMODE_CENTERALIGNED2    

TIM1_COUNTERMODE_CENTERALIGNED3    

TIM1_Period, count value, this item can take any value from 1 to 65535

TIM1_RepetitionCounter, the number of repetition counts. This item can take any value from 0 to 255.


Assuming our system clock is 8Mhz and we set the timer to 1 second, then our initialization settings should be:  


TIM1_TimeBaseInit(7,TIM1_COUNTERMODE_UP,1000,100);


Time calculation:

The frequency of the timer fcnk = 8Mhz/(7+1) = 1Mhz, which means that one count is 1us, 1000 counts are 1ms, and if the count is repeated 100 times, the interrupt timing time is 100ms. In order to achieve the effect of 1s, we also need to make a judgment 10 times in the interrupt function.


Interrupt function code implementation

INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)



  count_number++;

  if(count_number < 10)

  {

  }

  else

  {

       count_number = 0; //1 second timer has been reached

  }  

  TIM1_ClearITPendingBit(TIM1_IT_UPDATE);


}


The following is the relevant code to implement 1S timing


/*********************

*Function name: TIM_Config

*Function description: timer configuration

*Parameter Description:

* Input parameters None

* Output parameters None

*********************/

void TIM_Config(void)

{

  TIM1_DeInit();

  TIM1_TimeBaseInit(7,TIM1_COUNTERMODE_UP,1000,100);

  TIM1_ARRPreloadConfig(ENABLE);

  TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);

  TIM1_Cmd(ENABLE);

}

/**

  * @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine.

  * @param  None

  * @retval None

  */

INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)

{

  /* In order to detect unexpected events during development,

     it is recommended to set a breakpoint on the following instruction.

  */

  count_number++;

  if(count_number < 10)

  {

  }

  else

  {

    count_number = 0;  

  }  

  TIM1_ClearITPendingBit(TIM1_IT_UPDATE);

}

Keywords:STM8 Reference address:STM8 timer TIM1 timing

Previous article:STM8S0 TIM1_PWM complementary output
Next article:stm8s003MCU_PWM_pin function configuration

Recommended ReadingLatest update time:2024-11-16 09:17

S5PV210 hours
TCFG0, R/W, Address = 0xE250_0000 Timer Input Clock Frequency = PCLK / ( {prescaler value + 1} ) / {divider value} TCFG1, R/W, Address = 0xE250_0004 CON, R/W, Address = 0xE250_0008 TCNTB4, R/W, Address = 0xE250_003C Example #define TCFG0 (*(unsigned int*)0xE2500000) #define TCFG1 (*(unsigned int*)0xE2500004
[Microcontroller]
S5PV210 hours
STM8 Read-while-Write (RWW)
The RWW feature allows the user to write to the DATA EEPROM area while executing a program and reading program memory, so the execution time is optimized. The opposite operation is not allowed: you cannot read the DATA EEPROM while writing program memory. The RWW feature is always available and can be used at any ti
[Microcontroller]
STM8 Read-while-Write (RWW)
IAR configuration implementation based on STM8
  Take STM8 as an example:   Copy the icf file of the MCU model used in the project to the project. ICF is usually placed in   C:Program FilesIAR SystemsEmbedded Workbench 6.5stm8config   In this directory.   1. Copy lnkstm8s207rb.icf   For example, if I use STM8S207RB, I will copy the file lnkstm8s207rb.icf into my
[Microcontroller]
IAR configuration implementation based on STM8
IAR for STM8 Issue 1
IAR for STM8 pop-up window: Fatal error while generating source browse infomation. See the Source Browse Log window for more infomation This happens because there are Chinese characters in the include path of the .c or .h file. After changing Chinese to English and resetting the path, it will run normally as follows.
[Microcontroller]
IAR for STM8 Issue 1
Detailed explanation and application of STM8's ADC multiple acquisition modes
First, the ADC of STM8S supports 5 conversion modes: single mode, continuous mode, buffered continuous mode, single scan mode, and continuous scan mode. 1Single mode In single-shot mode, the STM8S ADC performs only one ADC conversion on the selected channel, and the conversion result is stored in the ADC_DR register
[Microcontroller]
Detailed explanation and application of STM8's ADC multiple acquisition modes
STM8 MCU ADC, Timer, USART practical routines
Introduction: This is a routine for STM8L-051 that I spent a long time to figure out. It controls the LED light. Timer2 enters the interrupt after 100us. The software starts the ADC, samples 10 times and takes the average. The result is sent to the PC through UASART. The utility program displayed on the hyperterminal
[Microcontroller]
Solution to the problem of STM8 MCU external crystal oscillator not oscillating
Some models of STM8 microcontrollers can support external crystal oscillators up to 24MHz. When configuring an external crystal oscillator, follow the steps below: 1 Define external crystal oscillator macro definition The macro definition is written as follows: #define HSE_VALUE ((uint32_t)24000000) Write the valu
[Microcontroller]
STM8 SPI slave
Hardware platform: stm8s103  Compilation environment: IAR for stm8 //SPI slave initialization mode 0: CPOL=0 CPHA=0  void spiSlaveInit(void) {     SPI_CR1_SPE = 0;                    //  禁用SPI Disable SPI     SPI_CR1_CPOL = 0;                   //  CPOL = 0     SPI_CR1_CPHA = 0;                   //  CPHA = 0      S
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号