STM8L timer2 generates PWM

Publisher:WhisperingWishLatest update time:2019-12-18 Source: eefocusKeywords:STM8L Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:STM8L Reference address:STM8L timer2 generates PWM

Previous article:STM8L infrared emission (timer interrupt generates 38K signal)
Next article:STM8L internal 16M crystal

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号