Use of internal timer 2 of stm8s microcontroller

Publisher:HuanleLatest update time:2021-10-19 Source: eefocusKeywords:stm8s Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The Chinese manual of stm8s states that TIM2 16-bit upward counting 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:Use of internal timer 2 of stm8s microcontroller

Previous article:PCF8563 electronic clock experiment based on STM8
Next article:Use of STVD in STM8

Recommended ReadingLatest update time:2024-11-15 15:11

STM8S_ 006_AWU automatic wake-up
Preface Ⅰ In some low-power devices, the device needs to enter low power consumption and wake up the MCU at a certain interval, so a timed "AWU automatic wake-up" function is needed. To implement the above functions in STM32, the common operation is to use RTC. However, RTC needs to be configured before entering l
[Microcontroller]
STM8S Self-study Notes-006 GPIO input: key input and key filtering
GPIO Input In "STM8S Self-study Notes-003 GPIO Output: Lighting up LEDs and Marquee Effects", we have set the GPIO of the LED to push-pull output mode, which is only one of the GPIO output functions. Similarly, GPIO has more than one input function. Floating input, no interrupt Pull-up input, no interrupt Floating i
[Microcontroller]
STM8S FLASH and EEPROM read and write operations
The following are FLASH operations: #include "flash.h" #include "stm8s_flash.h"     void Flash_Write_bytes(uint32_t Address , uint8_t * DataBuff,uint16_t length) {   uint16_t Count=0;   for( Count=0 ; Count length ; Count++ )   {     FLASH_ProgramByte_User(Address+Count,DataBuff );   } }       void FLASH_ProgramBy
[Microcontroller]
STM8S ADC initialization settings and applications
//ADC channel number definition #define ADC_Chanel0 (unsigned char)0x00 #define ADC_Chanel1 (unsigned char)0x01 #define ADC_Chanel2 (unsigned char)0x02 #define ADC_Chanel3 (unsigned char)0x03 #define ADC_Chanel4 (unsigned char)0x04 #define ADC_Chanel5 (unsigned char)0x05 #define ADC_Chanel6 (unsi
[Microcontroller]
ADC Application of STM8S
//Software environment: IAR FOR STM8 V1.0 //Author: Nicole //Function: AD single conversion program, suitable for temperature collection and voltage collection //Date: 2010.11.05 #include "iostm8s105c6.h" unsigned int DATA; unsigned int DATAH; unsigned int DATAL; // Function: delay function // Input paramete
[Microcontroller]
STM8S Window Watchdog
The .h file is as follows:   #ifndef __WWDG_H #define __WWDG_H #include "stm8s.h" void Delay();  void WWDG_Configuration(void) ;   void Refresh_WWDG_Window(void); #endif The .c file is as follows: #include "wwdg.h" #include "stm8s_wwdg.h"   #define CounterInit 0x7f #define window      0x77   void Delay() //delay
[Microcontroller]
Internal eeprom programming of STM8S MCU
Introduction: STM8S microcontroller chip also integrates EEPROM, with a capacity ranging from 640 bytes to 2K bytes. The most unique feature is that in STM8 microcontroller, the access to EEPROM is just like regular RAM, which is very convenient. The address space of EEPROM is uniformly addressed with the memory, star
[Microcontroller]
STM8S Learning 01——SPI&IIC
1. Review the IIC bus protocol 1) Some characteristics of the I2C bus 1 Only two bus lines are required: one serial data line SDA and one serial clock line SCL 2 Each device connected to the bus can be addressed by software with a unique address and a simple master-slave relationship that always exists. The master can
[Microcontroller]
Latest Microcontroller Articles
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号