How to write LED Blinking program on STM8S with timer 2 peripheral.

As we know STM8S family microcontroller generally consists of  TIM2|TIM3|TIM5 general purpose timers. Here we have set a problem how to create a 1 Second LED ON/ LED OFF. For this we need to understand some feature which are as follows.

Material Required:

1. STM8S Development Board (Here we are using STM8S003F3P6)





2. STM8 & STM32 ST-LINK V2 programmer






Software Required:

STVD (ST Visual Developer)

STVP (ST Visual Programmer)

Cosmic CXSTM8 Special Edition 4.5.2 Compiler

Source files and Header files can be download from this link: https://github.com/vivek889/STM8S



/*STM8S003F3P6 LED BLINK PROGRAM WITH TIMER 2 (1 SECOND ON 1 SECOND OFF)*/

#include "stm8s.h"

int main() {

  // Default clock is HSI/8 = 2MHz

GPIO_DeInit(GPIOB); // prepare Port B for working 

GPIO_Init (GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_SLOW); //Declare PB5 as push pull Output pin

TIM2_DeInit();

TIM2_TimeBaseInit(TIM2_PRESCALER_2048, 1952);

 TIM2_Cmd(ENABLE);

 while(TRUE)

    {

          if(TIM2_GetCounter() > 976)

          {

               GPIO_WriteHigh(GPIOB, GPIO_PIN_5);

          }

          else

          {

               GPIO_WriteLow(GPIOB, GPIO_PIN_5);

          }

    };

}




Explanation of the code:

1. #include "stm8s.h" 

This file gives the information about which STM8S family microcontroller we are going to use and what are its associated peripherals. STM8S_CONF.H is used for putting all the configuration stuff for the peripheral library.

2.int main() {

This is the main function which is going to repeated itself again and again.

3.Initializing port B and 5 no pin.

4.Initializing Timer 2.

5.TIM2_PRESCALER_2048, 1952



 


Prescaler = 2048, Reptiation Counter = 1, Counts = 1992, Fmaster = 2MHz

Time Event = 2 Second Approx(1.998848 Seconds)

6. Toggling of onboard led with the counter logic of 1 Second. 





Comments

Popular posts from this blog

How to interface RGB LED with STM8S microcontroller.

How to interface Buzzer with STM8S microcontroller.