Introduction
This article introduces how to use timer2 in the STM8L series to generate 38K frequency PWM.
Among them, this article uses the first channel (PB0) of timer2.
experiment platform
Compiler software: IAR for STM8 1.42.2
Hardware platform: stm8l101f3p6 development board
Emulator: ST-LINK
Library version: STM8L_STMTouch_Lib_V1.1.0
Experimental procedures
1. Add the driver of timer 2 written by yourself to the project
1) Write a driver GUA_Timer2_PWM.c (stored in the USER folder of the project)
//**********************************************************************************
//name: GUA_Timer2_PWM.c
//introduce: PWM driver for timer 2
//author: Sweet big melon
//email: 897503845@qq.com
//QQ group Melon MCU STM8/STM32 (164311667)
//changetime: 2016.12.01
//**********************************************************************************
#include "stm8l10x.h"
#include "GUA_Timer2_PWM.h"
/*************************Macro definition************************/
#ifndef U8
typedef unsigned char U8;
#endif
#ifndef U16
typedef unsigned short U16;
#endif
#ifndef U32
typedef unsigned long U32;
#endif
//Infrared pin macro
#define GUA_TIMER2_PWM_PORT GPIOB
#define GUA_TIMER2_PWM_PIN GPIO_Pin_0
#define GUA_TIMER2_PWM_MODE GPIO_Mode_Out_PP_High_Fast
//Infrared switch macro
#define GUA_TIMER2_PWM_ON SET //High level on
#define GUA_TIMER2_PWM_OFF RESET //Low level off
//**********************************************************************************
//name: GUA_Timer2_PWM_Status
//introduce: PWM switch of timer 2
//parameter: nGUA_Timer2_PWM_Status: GUA_TIMER2_PWM_STATUS_ON or GUA_TIMER2_PWM_STATUS_OFF
//return: none
//author: Sweet big melon
//email: 897503845@qq.com
//QQ group Melon MCU STM8/STM32 (164311667)
//changetime: 2016.12.01
//**********************************************************************************
void GUA_Timer2_PWM_Status(U8 nGUA_Timer2_PWM_Status)
{
// Turn on PWM
if(nGUA_Timer2_PWM_Status == GUA_TIMER2_PWM_STATUS_ON)
{
TIM2_CtrlPWMOutputs(ENABLE);
//GPIO_WriteBit(GUA_TIMER2_PWM_OFF, GUA_TIMER2_PWM_PIN, GUA_TIMER2_PWM_ON);
}
// Turn off PWM
else
{
TIM2_CtrlPWMOutputs(DISABLE);
GPIO_WriteBit(GUA_TIMER2_PWM_PORT, GUA_TIMER2_PWM_PIN, GUA_TIMER2_PWM_OFF);
}
}
//**********************************************************************************
//name: GUA_Timer2_PWM_SetDutyCycle
//introduce: PWM setting duty cycle of timer 2
//parameter: nGUA_Timer2_PWM_DutyCycle: 0~100 means 0%~100%
//return: none
//author: Sweet big melon
//email: 897503845@qq.com
//QQ group Melon MCU STM8/STM32 (164311667)
//changetime: 2016.12.01
//**********************************************************************************
void GUA_Timer2_PWM_SetDutyCycle(U8 nGUA_Timer2_PWM_DutyCycle)
{
TIM2_SetCompare1(GUA_TIMER2_PWM_FREQUENCY_VALUE*nGUA_Timer2_PWM_DutyCycle/100);
}
//**********************************************************************************
//name: GUA_Timer2_PWM_Init
//introduce: PWM initialization of timer 2
//parameter: none
//return: none
//author: Sweet big melon
//email: 897503845@qq.com
//QQ group Melon MCU STM8/STM32 (164311667)
//changetime: 2016.12.01
//**********************************************************************************
void GUA_Timer2_PWM_Init(void)
{
//PB0 channel IO is configured as output
GPIO_Init(GUA_TIMER2_PWM_PORT, GUA_TIMER2_PWM_PIN, GUA_TIMER2_PWM_MODE);
//Clock configuration
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2,ENABLE);
//Configuration
TIM2_DeInit(); //Reset
TIM2_TimeBaseInit(TIM2_Prescaler_1, TIM2_CounterMode_Up, GUA_TIMER2_PWM_FREQUENCY_VALUE); //Recount every 26us, 38K
TIM2_OC1Init(TIM2_OCMode_PWM2, TIM2_OutputState_Enable, 0, TIM2_OCPolarity_High, TIM2_OCIdleState_Reset); //Idle-first-run mode, output enable, duty cycle initialized to 0, high when running, low in dead zone
TIM2_Cmd(ENABLE); //Enable timer 2
// Turn off PWM
GUA_Timer2_PWM_Status(GUA_TIMER2_PWM_STATUS_OFF);
}
The total value here is 26us, and the duty cycle is temporarily initialized to 0%.
Note that when turning off PWM, you need to set the IO port to low, otherwise infrared rays will continue to be emitted, resulting in high power consumption.
2) Write a driver header file GUA_Timer2_PWM.h (stored in the USER folder of the project)
//**********************************************************************************
//name: GUA_Timer2_PWM.h
//introduce: header file of PWM driver for timer 2
//author: Sweet big melon
//email: 897503845@qq.com
//QQ group Melon MCU STM8/STM32 (164311667)
//changetime: 2016.12.01
//**********************************************************************************
#ifndef _GUA_TIMER2_PWM_H_
#define _GUA_TIMER2_PWM_H_
/*************************Macro definition************************/
//PWM switch macro
#define GUA_TIMER2_PWM_STATUS_ON 0 //PWM is on
#define GUA_TIMER2_PWM_STATUS_OFF 1 //PWM off
//PWM frequency value
#define GUA_TIMER2_PWM_FREQUENCY_VALUE 16*26 //26us is one cycle
/*************************External function declaration****************************/
void GUA_Timer2_PWM_Status(unsigned char nGUA_Timer2_PWM_Status);
void GUA_Timer2_PWM_SetDutyCycle(unsigned char nGUA_Timer2_PWM_DutyCycle);
void GUA_Timer2_PWM_Init(void);
#endif
3) Add GUA_Timer2_PWM.c to the project
4) Add the driver path
$PROJ_DIR$..USER
Note that the USER folder is a custom file at the same level as the "Project" folder, and is used to store the code written by Cantaloupe.
2. Add library driver
1) Add the driver file of the library
3. Calling in the application layer
1) Add header file (in main.c)
#include "GUA_Timer2_PWM.h"
2) Initialization (in the main function of main.c)
//Timer 2 PWM initialization
GUA_Timer2_PWM_Init();
3) Test code (can be placed in different key processing codes)
① Turn on PWM with 50% duty cycle
GUA_Timer2_PWM_SetDutyCycle(50);
GUA_Timer2_PWM_Status(GUA_TIMER2_PWM_STATUS_ON);
② Turn off PWM
GUA_Timer2_PWM_Status(GUA_TIMER2_PWM_STATUS_OFF);
③ Turn on 60% duty cycle PWM
GUA_Timer2_PWM_SetDutyCycle(60);
GUA_Timer2_PWM_Status(GUA_TIMER2_PWM_STATUS_ON);
4. Multiplex IO port as timer2
The multiplexing function of stm8 is quite special and needs to be modified manually. It took me two days to figure it out.
1) Open the Option Bytes under ST-LINK of the project (some information on the Internet shows that it must be in simulation mode, but the actual test of Cantaloupe does not necessarily need to be in simulation mode)
2) Configure and save
3) Create an .obc configuration file
4) Enable the project to automatically load the saved configuration file
5) Rebuild all.
Experimental Results
Connect PB0 with a logic analyzer and capture the waveforms of the three test codes respectively. The experimental results are as follows:
1. Turn on PWM with 50% duty cycle
It can be seen that PB0 generates a 38.647K square wave with a duty cycle of 13/25.875 (about 50.2%).
2. Turn off PWM
No waveform generated
3. Turn on PWM with 60% duty cycle
It can be seen that PB0 generates a 38.647K square wave with a duty cycle of 15.5/25.875 (about 59.9%).
Therefore, the experiment was successful.
Note: Cantaloupe has been tested and simulated many times, so there is no need to repeat the steps of configuring the reuse function. Most of the online methods use STVD to generate the reuse configuration, and then use other methods to download the program. Whether the steps in this article are still effective in mass production still needs to be tested later.
Previous article:STM8L infrared emission (timer interrupt generates 38K signal)
Next article:STM8L internal 16M crystal
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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
- EEWORLD University ---- QA7: The most important component of the analog world - signal chain and power supply: gate driver
- title
- 【TI Recommended Course】#TI Sports Camera and Handheld Stabilizer Solutions#
- Factory Automation Linear Position Sensors Feature Requirements in Various Industries
- Zhuhai is looking for embedded engineers
- Baud rate adaptation of TI DSP chip SCI module
- Shenzhen Industry Exhibition Recruitment: Product Engineer, Technical Support Engineer
- Thank you for being there + looking forward to EEWORLD in 2020
- 1. Unboxing & First Experience with Integrated Development Environment CDK
- Playing with Zynq Serial 38——[ex57] Zynq AXI HP bus bandwidth test