如何采用SysTick来做定时器

控制/MCU

1878人已加入

描述

1 硬件电路配置

这里还是借用前面LED电路我就不贴图片。

2 时钟说明

SysTick和HCK的时钟频率是一样的库函数代码如下

/**

*@briefInitializeandstarttheSysTickcounteranditsinterrupt.

*

*@paramticksnumberofticksbetweentwointerrupts

*@return1=failed,0=successful

*

*Initialisethesystemticktimeranditsinterruptandstartthe

*systemticktimer/counterinfreerunningmodetogenerate

*periodicalinterrupts.

*/

static__INLINEuint32_tSysTick_Config(uint32_tticks)

{

 

if(ticks》SysTick_LOAD_RELOAD_Msk)return(1);/*Reloadvalueimpossible*/

SysTick-》LOAD=(ticks&SysTick_LOAD_RELOAD_Msk)-1;/*setreloadregister*/

NVIC_SetPriority(SysTick_IRQn,(1《《__NVIC_PRIO_BITS)-1);/*setPriorityforCortex-M0SystemInterrupts*/

SysTick-》VAL=0;/*LoadtheSysTickCounterValue*/

SysTick-》CTRL=SysTick_CTRL_CLKSOURCE_Msk|

SysTick_CTRL_TICKINT_Msk|

SysTick_CTRL_ENABLE_Msk;/*EnableSysTickIRQandSysTickTimer*/

return(0);/*Functionsuccessful*/

}

由库函数可知道没有进行分频动作所以时钟和主时钟一样

配置时钟中断的时间间隔

voidSysTick_Init(void)

{

/*SystemFrequency/10001ms中断一次

*SystemFrequency/10000010us中断一次

*SystemFrequency/10000001us中断一次

*/

//if(SysTick_Config(SystemFrequency/100000))//ST3.0.0库版本

if(SysTick_Config(SystemCoreClock/1000))//ST3.5.0库版本

{

/*Captureerror*/

while(1);

}

//我需要不断的开启中断所以这句话屏蔽起来

//SysTick-》CTRL&=~SysTick_CTRL_ENABLE_Msk;

}

用中断模式所以需要在中断文件中做定时标志。一下这个函数在中断文件中

/**

*@briefThisfunctionhandlesSysTickHandler.

*@paramNone

*@retval:None

*/

voidSysTick_Handler(void)

{

TimingDelay_Decrement();

}

中断函数在调用一个计数函数,计数函数应该放在外面一个公共文件中方便各个文件中数据传递。

/*

*函数名:TimingDelay_Decrement

*描述获取节拍程序

*输入无

*输出无

*调用在SysTick中断函数SysTick_Handler()调用

*/

voidTimingDelay_Decrement(void)

{

if(TimingDelay!=0x00)

{

TimingDelay--;

}

}

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
评论(0)
发评论
systechillinois 2022-08-22
0 回复 举报
http://house.china.com.cn/sousou/%25C6%25BD%25CC%25A8%252C%2B%25C5%25B7%25D1%25C7%25B9%25FA%25BC%25CA%25D5%25E6%25C8%25CB%25D3%25E9%25C0%25D6B2024.cN%252C%25D5%25C5%25D2%25B4%25C9%25FA%25BB%25EE_22s.htm http://house.china.com.cn/sousou/%25D6%25B1%25D3%25AA%252C%2B%25D3%25F1%25CF%25E9%25D3%25E9%25C0%25D6%25B3%25C7B2024.cN%252C%25D3%25F1%25C1%25D6%25CC%25E5%25D3%25FD_2s.htm http://house.china.com.cn/sousou/%25BE%25AD%25C0%25ED%252C%2B%25C3%25E5%25B5%25E9%25D3%25F1%25CF%25E9%25B9%25AB%25CB%25BEB2024.cN%252C%25CE%25E2%25D6%25D2%25C8%25C8%25CB%25D1_2s.htm http://house.china.com.cn/sousou/%25C0%25CF%25BD%25D6%252C%2B%25C3%25E5%25B5%25E9%25D3%25C0%25B2%25FD%25B9%25FA%25BC%25CA%25B6%25B7%25C5%25A3B2024.cN_1s.htm http://house.china.com.cn/sousou/%25B9%25D9%25B7%25BD%25CD%25F8%25D5%25BE%252C%2B%25C3%25E5%25B5%25E9%25B0%25EB%25C9%25BD%25B9%25FA%25BC%25CAB2024.cN_78s.htm 收起回复

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分