yanjiesh 发表于 2013-12-14 21:31:02

LPC1766 SYSTICK 延迟函数设计 遇到的疑问 请教大家 :)

大家好,想利用 systick 开发一个ms级的定时器 发现 LPC1766只能支持 最小为10ms计时单位的 延迟程序 。
其实 10ms级别也可以接受 但是以下参考代码 看着好像有点问题 。

#include "LPC17xx.h"
#include "type.h"
#include "systick.h"
#include "nvic.h"

#define SYSTICK_ENABLE            0                                          /* Config-Bit to start or stop the SysTick Timer                         */
#define SYSTICK_TICKINT             1                                          /* Config-Bit to enable or disable the SysTick interrupt               */
#define SYSTICK_CLKSOURCE         2                                          /* Clocksource has the offset 2 in SysTick Control and Status Register   */
#define SYSTICK_MAXCOUNT       ((1<<24) -1)                                    /* SysTick MaxCount                                                      */

volatile uint32 TimeTick = 0;


//********************************************************
//功能:           系统Tick中断程序,中断时间为每1MS
//输入:           NONE
//输出:                NONE
//********************************************************
void SysTick_Handler(void)
{
        TimeTick++;
}

//********************************************************
//功能:           系统Tick初始化配置
//输入:           NONE
//输出:                NONE
//********************************************************
uint32 SysTick_Config(uint32 ticks)
{
        if (ticks > SYSTICK_MAXCOUNT)return (1);
        NVIC_ST_RELOAD = (ticks & SYSTICK_MAXCOUNT) - 1;

        NVIC_SYS_H_PRI15 = 0x48;
        NVIC_ST_CURRENT = 0x00;
        NVIC_ST_CTRL = (1 << SYSTICK_CLKSOURCE) | (1<<SYSTICK_ENABLE) | (1<<SYSTICK_TICKINT);
        return 0;
}

//********************************************************
//功能:           延时Ticd ms
//输入:           NONE
//输出:                NONE
//********************************************************
void Delay (uint32 tick)
{
        uint32 systickcnt;
       
        systickcnt = TimeTick;
        while ((TimeTick - systickcnt) < tick);
}

void delaySysTick(uint32 tick)
{
        uint32 timetick;
       
        /* Enable the SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Enable);
       
        timetick = TimeTick;
        while ((TimeTick - timetick) < tick);
       
        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);
       
        /* Clear SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Clear);
}


我分析,在 Delay函数中 ,实参为 TimeTick是全局变量初始化为0,在Systick的中断服务函数中每次加1,。因此进入Delay 函数后,systickcnt赋值为0,而后在While语句中,不断进入systick中断服务,Timetick不断加1,TimeTick和Systickcnt的差值不断增大,直到中断延时到达延迟参考比较值。

参考代码似乎可行,但感觉 Systickcnt 似乎有点多余,各位高手看呢?

谢谢
页: [1]
查看完整版本: LPC1766 SYSTICK 延迟函数设计 遇到的疑问 请教大家 :)