STM8s Timer 2 usage

Publisher:AngelicGraceLatest update time:2021-02-22 Source: eefocusKeywords:STM8s Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Introduction: I recently started using the stm8s103k microcontroller, and today I will record the use of its timer 2. First of all, it needs to be explained that the stm8s manual says that TIM2 16-bit counts up, which is wrong.


The stm8s timing is easy to use. First, set the clock frequency of the timer. Once the clock period T of the timer is known, the timing time Tn is determined, that is, Tn = T * ARR (automatically load data).


The procedure is as follows:


1. MCU clock setting:


//fmaster=fcpu=2MHz


CLK_ECKR=0x00;


CLK_ICKR=0x01;


CLK_CMSR=0xe1;


CLK_SWR=0xe1;


CLK_CKDIVR=0x18;


2. Timer TIM2 initialization


//Timer 2 initialization fmaster/frequency division = 2M/2 = 1M, count once in 1us, interrupt once in 50us


void TIM2_Init(void)


{


_asm("sim"); //sim is to disable interrupts


TIM2_IER = 0x00; //Disable interrupt


TIM2_EGR = 0x01; //Allow update flag to be generated


TIM2_PSCR =0x01; //Set clock division 2M/2=1MHz---1us


TIM2_ARRH = 0x00; //0x32=50; Cycle = 50 times, reset timer 2 every 50us


TIM2_ARRL = 0x32; //ARR automatically loads value, decrements by 1 every 1us


TIM2_CNTRH=0x00; //initial value


TIM2_CNTRL=0x00;


TIM2_CR1 |= 0x81; //Start the timer


TIM2_IER |= 0x01; //Enable interrupt


_asm("rim"); //rim enables interrupt


}


3. Interrupt execution of program


@far @interrupt void TIM2_UPD_IRQHandler(void)


{


TIM2_SR1 &=~(0x01); //=0x0e; // Clear interrupt flag


PC_ODR=~PC_ODR; //You need to configure pc and pb as output ports first


PB_ODR=~PB_ODR;


}


4. Modify the interrupt vector table


Open the stm8_interrupt_vector.c file and add the following content:


extern @far @interrupt void TIM2_UPD_IRQHandler(void);


Modify the following line:


{0x82, NonHandledInterrupt},


for:


{0x82,(interrupt_handler_t)TIM2_UPD_IRQHandler},


That's it, you can try it.


During my operation, I encountered a very annoying problem, that is, the downloader often does not work, error number 30006, 30003.


In both cases, first confirm whether your stlink wiring is correct, and then measure the voltage of the 4 download pins. The correct voltage is: 5V, Gnd, NRST-5V, SWIM-0v (roughly like this). If there is no problem above, it may be that the downloader and the computer are not connected properly. You need to reconnect and confirm the software (Target-setting).


Keywords:STM8s Reference address:STM8s Timer 2 usage

Previous article:STM8 learning nRF24L01
Next article:The TIMER2 timer cannot enter the interruption problem

Recommended ReadingLatest update time:2024-11-16 12:46

About the halt state and wake-up of STM8S
Introduction: There is a project using STM8S103F3. Because it is battery-powered, it needs to enter the halt state and use GPIO external wake-up. Because it is the first time to use STM8S, I took a lot of detours. I recorded the process so that people who are new to STM8S like me can be inspired. Phenomenon: Unable
[Microcontroller]
STM8S MCU realizes multi-channel conversion
Today I am using the AD function of the stm8s microcontroller. There are ten channels from AN0 to AN9 on the microcontroller. I want to use only three of them, AN5 to AN7. //ADC initialization void ADC_Init(void) {  ADC_CR1 = 0x50;  ADC_CR2 = 0x38;  ADC_TDRL = 0xE0; //Disable Schmitt trigger } unsigned int Read_ADC_Te
[Microcontroller]
stm8s input capture
Input capture, in short, records the value of CNTR when the signal arrives, and then passes the value to CCR. Taking PC1 as an example, the code is as follows:     TIM1_CR1_DIR = 0; //Counter counts up     TIM1_IER_UIE = 1; // Enable TIM1 overflow interrupt       TIM1_PSCRH = 0x00;     TIM1_PSCRL = 0x05; //Configure
[Microcontroller]
STM8S Timer 1 Input Capture
To set the input capture pin, use stvp + stlink to set it, otherwise you cannot enter the interrupt. Then here is the code: C language:  Code#11818 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Experimental platform: STM8S103F2P6 FM receiving experiment core board  +
[Microcontroller]
STM8S Timer 1 Input Capture
STM8S (105K4) user notes - basic configuration of independent watchdog IWDG
0. Use of independent watchdog The independent watchdog operates from a low speed internal RC oscillator (LSI). If the LSI runs normally, when an error occurs in the main program, the watchdog counter cannot be reset, and the independent watchdog will generate a reset flag and restart. Since the independent watchd
[Microcontroller]
Description of assert_param() of stm8s
STM8S programs and subroutines all have assert_param(....), as follows: What is the use of this sentence? ? ? Should it be OK to delete it? ? void TIM1_TimeBaseInit(u16 TIM1_Prescaler,                        TIM1_CounterMode_TypeDef TIM1_CounterMode,                        u16 TIM1_Period,                        u8 T
[Microcontroller]
MSP430F5529 (VI) Timer_A-2
6.4 Capture Compare Module This is the official introduction to the important functions of TA based on the above introduction. First, let's look at a register TACCTL0-TACCTL6: (The most complex register in TA, look up the table when used) CMx: Capture mode setting 00 No capture  01 Rising edge capture  10 Fall
[Microcontroller]
MSP430F5529 (VI) Timer_A-2
About STM8S low power management
I have received some questions about STM8S low power consumption in the background. Today I will write about low power consumption. 1STM8S power consumption source STM8S power consumption is divided into static power consumption and dynamic power consumption. Static power consumption: mainly generated by the bias cu
[Microcontroller]
About STM8S low power management
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

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号