⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM8L的tim4定时器使用
💻 C
字号:
/**
  ******************************************************************************
  * @file    Project/Template/main.c
  * @author  MCD Application Team
  * @version V1.3.0
  * @date    07/14/2010
  * @brief   Main program body
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDIN 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 2010 STMicroelectronics</center></h2>
  */ 
	
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"

/** @addtogroup Template
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
#define LED1_PORT GPIOE
#define LED1_PIN  GPIO_Pin_7
__IO uint32_t TimingDelay;

/* Private functions ---------------------------------------------------------*/
void delay_ms(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  GPIO_Init(LED1_PORT,LED1_PIN,GPIO_Mode_Out_PP_Low_Fast);
  //输出低电平-高速10M
  /* Enable TIM4 CLK */
   CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE);
  /* TIM4 configuration:
    - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
    clock used is 16 MHz / 128 = 125 000 Hz
   - With 125 000 Hz we can generate time base:
       max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
       min time base is 0.016 ms if TIM4_PERIOD = 1   --> (  1 + 1) / 125000 = 0.016 ms
   - In this examle we need to generate a time base equal to 1 ms
    so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
 
   /* Time base configuration */
   TIM4_TimeBaseInit(TIM4_Prescaler_128, 124);
   /* Clear TIM4 update flag */
   TIM4_ClearFlag(TIM4_FLAG_Update);
   /* Enable update interrupt */
   TIM4_ITConfig(TIM4_IT_Update, ENABLE);
   /* enable interrupts */
   enableInterrupts();
 
   /* Enable TIM4 */
   TIM4_Cmd(ENABLE);
  /* Infinite loop */
  while (1)
  {
    delay_ms(1000);
    GPIO_ToggleBits(LED1_PORT,LED1_PIN);
  }
}
 /**
   * @brief  Inserts a delay time.
   * @param  nTime: specifies the delay time length, in milliseconds.
   * @retval None
   */
void delay_ms(__IO uint32_t nTime)
{
   TimingDelay = nTime;
 
   while (TimingDelay != 0);
}
 /**
  * @brief  Decrements the TimingDelay variable.
   * @param  None
   * @retval None
   */
void TimingDelay_Decrement(void)
{
   if (TimingDelay != 0x00)
   {
     TimingDelay--;
   }
}

/**
  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */

#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

/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -