_
How to configure TIM1 & TIM4

and test it on STM8S Discovery



Home Page
STM8 home page
STM8L home page



Suggested readings
Preconditions
What does the example
The connections on STM8S-Discovery
The FirmWare





-
Suggested readings:

  • UM0817: STM8S Discovery user manual is here.
  • STM8S105xx DATASHEET is here.
  • RM0016: REFERENCE MANUALS is here.
  • PM0051: How to program STM8S Flash program memory and data EEPROM is here.
  • Flow diagram for developing software in C on STM8 - click here

UP



-
Preconditions:

UP




-
What does the example:


The example explains how to configure TIM1 and TIM4 to generate two time bases.
TIM1 Interrupt 100uS that drive PC4 and PC5 (see below)
TIM4 Interrupt 1mS that blink LED every 500mS.
All the SW works under Interrupt.
This SW was tested on
STM8S Discovery.


Remember that there is also the example:  How to configure TIM4 and test it on STM8S Discovery that analize in deep the TIM4.


UP




-
The connections on STM8S-Discovery








-
The FirmWare:


Click here to get this SW Example ready to use (STM8-Discovery-TIM1&TIM4)

The tree of the project is:


For open the project start STM IDE (ST VISUAL DEVELOP) and select:
File -> Open Workspace
go in the directory Cosmic and select:
Project.stw

First, is important look the clock tree that is show below.
It is important select the clock source that you want to use in your application (default is HSI, RC osc. at 16Mhz).
In my application I'm using HSE clock source (OSCIN - OSCOUT).



Second, is a good idea see the TIMERs specification, see below.



The implementation of the software was very easy because I used the STM8 Library.
configure the HSE clock source
configure GPIO (see the file named: define.h)
configure TIM4
configure TIM1
enable interrupts
see below.

main.c

/*
 
    www.emcu.it
    By: Enrico M.
    Date: 05-Febr-2012
    ST Visual Develop: 4.3.1
    Cosmic C Compiler: 4.3.6
    STM Library: 2.1.0
    PC: Vindows XP SP3
    Tested on STM8S-Discovery
     
    TEST POINT:
    CN2 -> pin_6 and pin_5
    On the pins you should see a square wave of about 0.1 mS.
 
    The LED must blink arround of 500 mS
     
    TIM4 - interrupt 1 mS
    TIM1 - interrupt 100 uS
 
*/ 
 
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "define.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/ 
u16 tempo = 0;
/* Private function prototypes -----------------------------------------------*/
void TIM1_Config(void);
void TIM4_Config(void);
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
void Delay (uint16_t nCount);
 
 
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
     
    // Configure Quartz Clock
    CLK_DeInit();
    CLK_HSECmd(ENABLE);
    CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);
     
 
    // Configure I/O x Buzzer
    GPIO_Init(Port_BUZ1, BUZ1, GPIO_MODE_OUT_PP_LOW_FAST);
    GPIO_Init(Port_BUZ2, BUZ2, GPIO_MODE_OUT_PP_LOW_FAST);     
    /* Initialize I/Os LED in Output Mode */
    GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
     
    /* TIM1 configuration -----------------------------------------*/
    TIM1_Config();
    /* TIM4 configuration -----------------------------------------*/
    TIM4_Config();
  
    // SetUp BUZ1 & BUZ2
    GPIO_WriteHigh(Port_BUZ1, BUZ1);
    GPIO_WriteLow(Port_BUZ2, BUZ2);
     
    // Enable Interrupts
    enableInterrupts();
     
  while (1)
    {
    } 
}
 
/**
  * @brief  Configure TIM4 to generate time base 1mS
  * @param  None
  * @retval None
  */
void TIM4_Config(void)
{
  CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER4 , ENABLE);
  TIM4_DeInit();
  /* Time base configuration */ 
  TIM4_TimeBaseInit(TIM4_PRESCALER_128, 0x7D); // 127 and 0x7D == Interrupt 1mS
  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  TIM4_Cmd(ENABLE);    // Enable TIM4 
}
 
/**
  * @brief  Configure TIM1 to generate time base 
  * @param  None
  * @retval None
  */
void TIM1_Config(void)
{
    CLK_PeripheralClockConfig (CLK_PERIPHERAL_TIMER1 , ENABLE);     
    TIM1_DeInit();
    TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_DOWN, 92, 0); //timer freq = (clock CPU/16) -> 1bit = 1uS -> 92*1uS=92uS
    TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
    TIM1_Cmd(ENABLE);
}
 
void BuzOsc2(void)
{
 static u8 porta=0;
 
    GPIO_WriteReverse(Port_BUZ1,BUZ1);
    porta=GPIO_ReadOutputData(Port_BUZ1);
    if (ValBit(porta,5)) GPIO_WriteLow(Port_BUZ2,BUZ2); // Attenzione 5 per poterlo usare con STM8S-Discovery
    else GPIO_WriteHigh(Port_BUZ2,BUZ2);
}
 
/**
  * @brief Delay
  * @param nCount
  * @retval None
  */
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}
 
 
#ifdef USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)

  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
     
  while (1)
  {
  }
}
#endif
 


stm8s_it.c

/**
  ******************************************************************************
  * @file     stm8s_it.c
  * @author   MCD Application Team
  * @version  V2.0.1
  * @date     18-November-2011
  * @brief    Main Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 
 
/* Includes ------------------------------------------------------------------*/
#include "stm8s_it.h"
#include "define.h"
 
extern u16 tempo;
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
 
void BuzOsc(void)
{
static u8 porta=0;
 
    GPIO_WriteReverse(Port_BUZ1,BUZ1);
    porta=GPIO_ReadOutputData(Port_BUZ1);
    if (ValBit(porta,5)) GPIO_WriteLow(Port_BUZ2,BUZ2);
    else GPIO_WriteHigh(Port_BUZ2,BUZ2);
}

 
/** @addtogroup TIM1_7PWM_Output
  * @{
  */
#ifdef _COSMIC_
/**
  * @brief  Dummy interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(NonHandledInterrupt, 25)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*_COSMIC_*/
 
/**
  * @brief  TRAP interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

CONTINUE.....
 
/**
  * @brief Timer1 Update/Overflow/Trigger/Break Interruption routine.
  * @par Parameters:
  * None
  * @retval
  * None
*/
#ifdef _COSMIC_
@far @interrupt void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)
#else /* _RAISONANCE_ */
void TIM1_UPD_OVF_TRG_BRK_IRQHandler(void) interrupt 11
#endif /* _COSMIC_ */
{
    BuzOsc();
    TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
    return;
}
 
/**
  * @brief  Timer1 Capture/Compare Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */   
}
 
CONTINUE.....
 
#ifdef STM8S903
/**
  * @brief  Timer6 Update/Overflow/Trigger Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
/**
  * @brief  Timer4 Update/Overflow Interrupt routine
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
  tempo++;
 
        if (tempo>500)
        {
        /* Toggles LED */
            GPIO_WriteReverse(Port_LED, LED);
            tempo=0;
        }
 
    TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
#endif /*STM8S903*/
 


define.h

/*
 
    Configurazione  I/O 
 
    By E.M.
     
*/
 
#define Port_BUZ1    (GPIOC)
#define BUZ1    (GPIO_PIN_5)             
#define Port_BUZ2    (GPIOC)
#define BUZ2    (GPIO_PIN_4)
#define Port_LED    (GPIOD)
#define LED        (GPIO_PIN_0)





Home Page
STM8 home page
STM8L home page