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).
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
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- MakeCode now supports ESP32 and ESP32-S2
- When planning for Industry 4.0, what do you see from these two buying cases?
- Sensor board provides solutions for Bluetooth applications in IoT
- [Topmicro Intelligent Display Module] Part 2: Power-on Test
- uf2 bootloader for nRF52840DK development board
- MSP430 MCU Register Summary - CPU Register
- A New Method of Expanding SRAM Externally in MCU
- CircuitPython 6.0.0 Beta 0 released!
- Qorvo Wireless Technology Wi-Fi Father Cees Links Inducted into Wi-Fi NOW Hall of Fame
- October 29 live broadcast review: ST Data Center and Communication Network Power Management Solutions (including video, presentation documents, Q&A)