/** ****************************************************************************** * @file TIM4/TIM4_TimeBase/main.c * @author MCD Application Team * @version V1.5.0 * @date 13-May-2011 * @brief Main program body ****************************************************************************** * @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. * * © COPYRIGHT 2011 STMicroelectronics ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm8l15x.h" #include "stm8_eval.h" #define TIM4_PERIOD 124 __IO uint32_t TimingDelay; /* Private function prototypes -----------------------------------------------*/ static void CLK_Config(void); static void TIM4_Config(void); void Delay(__IO uint32_t nTime); void TimingDelay_Decrement(void); void main(void) { /* CLK configuration -------------------------------------------*/ CLK_Config(); /* Initialize Leds mounted on STM8L1526-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Turn on LED1 and LED3 */ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED3); /* TIM4 configuration -------------------------------------------*/ TIM4_Config(); while (1) { /* Toggle LED2 and LED4 */ STM_EVAL_LEDToggle(LED2); STM_EVAL_LEDToggle(LED4); /* Insert 50 ms delay */ Delay(50); /* Toggle LED1 and LED3 */ STM_EVAL_LEDToggle(LED1); STM_EVAL_LEDToggle(LED3); /* Insert 100 ms delay */ Delay(100); } } static void CLK_Config(void) { CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); CLK_SYSCLKSourceSwitchCmd(ENABLE); CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSE); while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSE) {} CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE); } static void TIM4_Config(void) { / /* Time base configuration */ TIM4_TimeBaseInit(TIM4_Prescaler_128, TIM4_PERIOD); /* 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); } void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while (TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } 这个函数干什么用的 ? |
RE:TIM4定时器库实例
就可以达到delay的目的,
回复:TIM4定时器库实例
回复:TIM4定时器库实例
RE:TIM4定时器库实例
你贴出来的代码既有声明,也有定义啊。?
TimingDelay_Decrement(void)的作用很简单,如果全局变量TimingDelay的值不为0,就减一,
而在Delay()里面是去判断TimingDelay 是不是等于0 ,等于0就结束等待,
所以你用delay的时候,比如delay(50),TimingDelay 的值被初始化为50,肯定是没一毫秒调用一次TimingDelay_Decrement(void)起做TimingDelay的自减,直到0为止结束的delay,不知道你清楚了没?
你可以去看看你的中断例程。。。
RE:TIM4定时器库实例
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
RE:TIM4定时器库实例