stm32延时函数可行和不可行

1. 可行的延时

#MeLED.c

include "MELED.h"
 void LED_TIM_Config(void)
{
    TIM_TimeBaseInitTypeDef LED_TIM_Struct;
    TIM_OCInitTypeDef TIM_OCInitConfig;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    TIM_DeInit(TIM4);
    //GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);



    TIM_OCInitConfig.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitConfig.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitConfig.TIM_OCPolarity = TIM_OCPolarity_Low;

    LED_TIM_Struct.TIM_Prescaler = (36000 - 1);
    LED_TIM_Struct.TIM_Period = 2 - 1;
    LED_TIM_Struct.TIM_ClockDivision = TIM_CKD_DIV1;
    LED_TIM_Struct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &LED_TIM_Struct);    

    TIM_ClearFlag(TIM4, TIM_FLAG_Update);
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_OC1PreloadConfig(TIM4, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}
void LED_NVIC_Config(void)
{
    NVIC_InitTypeDef LED_NVIC_Struct;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    LED_NVIC_Struct.NVIC_IRQChannel = TIM4_IRQn;
    LED_NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级
    LED_NVIC_Struct.NVIC_IRQChannelSubPriority = 0;//响应优先级
    LED_NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&LED_NVIC_Struct);
}

int i = 0;//注意该变量是定义在MeLED.c文件中的

void TIM4_IRQHandler(void)
{


    if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
        i++;
    }

}

void My_Systick_Delay(uint32_t nTime)
{
    i = 0;
    while(nTime != i);
}

#MeLED.h

#ifndef __MELED_H
#define __MELED_H

#include "stm32f10x.h"
#include "led.h"
#include "Oper_System_Delay.h"

void MELED_Init(void);
void MELED_PWM_Init(void);
void LED_TIM_Config(void);
void LED_NVIC_Config(void);
void TIM4_IRQHandler(void);
void My_Systick_Delay(uint32_t nTime);
#endif

#main.c

#include "stm32f10x.h"
#include "usart.h"
#include "led.h"
#include "RTC_Time.h" 
#include <stdio.h>
#include "MeLED.h"
#include "Delay.h"
int main(void)
{
                    while(1)
                    {
                        My_Systick_Delay(1000);
                        printf("Delay over\n");
                    }


}

#2.不可行的延时(我想知道为什么不可行?)
#Delay.c

#include "Delay.h"
void My_Systick_Delay(uint32_t nTime)
{
    Attain_delay = 0;
    while(nTime != Attain_delay);//程序会不停的在这里循环
}

#Delay.h

#ifndef __Delay_H
#define __Delay_H

#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_it.h"
#include "core_cm3.h"
#include "Oper_System_Delay.h"

#endif

#Oper_System_Delay.h

#ifndef __Oper_System_Delay_H
#define __Oper_System_Delay_H


#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_it.h"


static uint32_t Attain_delay;

//void Oper_System_Delay();

#endif

#MeLED.c

void TIM4_IRQHandler(void)
{


    if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
        Attain_delay++;
    }

}

所以第2点有人知道为什么不可行吗?最好可以提供第1和第2的差别在哪?

2个回答

各文件之间有包含关系就可见呀!

方案1:
变量i是一个全局变量,在定时器中断时候是可以改变的,也就是说i可以通过i++达到1000退出while循环。
方案2:
变量static uint32_t Attain_delay,只有在当前文件可见,在MeLED.c文件是不可见的,这样你的 Attain_delay++不能改变Attain_delay的值,所以循环是无限的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问