﻿{"id":80,"date":"2010-05-22T09:19:00","date_gmt":"2010-05-22T01:19:00","guid":{"rendered":"http:\/\/nick.workao.org\/?p=80"},"modified":"2010-05-22T09:26:25","modified_gmt":"2010-05-22T01:26:25","slug":"stm32-%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b-%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%92%9f-systick","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/scm\/80","title":{"rendered":"STM32 \u5165\u95e8\u6559\u7a0b \u7cfb\u7edf\u65f6\u949f SysTick"},"content":{"rendered":"<p><strong><span style=\"color: red;\">(\u4e00) \u80cc\u666f\u4ecb\u7ecd <\/span><\/strong><\/p>\n<p>\u5728\u4f20\u7edf\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u8f6f\u4ef6\u6309\u4e2d\u901a\u5e38\u5b9e\u73b0 Delay(N) \u51fd\u6570\u7684\u65b9\u6cd5\u4e3a\uff1a<\/p>\n<p>for(i = 0; i <= x; i ++);<\/p>\n<p>x\u00a0\u00a0&#8212;\u00a0\u00a0\u5bf9\u5e94\u4e8e \u5bf9\u5e94\u4e8e N \u6beb\u79d2\u7684\u5faa\u73af\u503c<\/p>\n<p>\u5bf9\u4e8eSTM32\u7cfb \u5217\u5fae\u5904\u7406\u5668\u6765\u8bf4\uff0c\u6267\u884c\u4e00\u6761\u6307\u4ee4\u53ea\u6709\u51e0\u5341\u4e2a ns\uff0c\u8fdb\u884c for \u5faa\u73af\u65f6\uff0c\u8981\u5b9e\u73b0 N \u6beb\u79d2\u7684 x  \u503c\u975e\u5e38\u5927\uff0c\u800c\u4e14\u7531\u4e8e\u7cfb\u7edf\u9891\u7387\u7684\u5bbd\u5e7f\uff0c\u5f88\u96be\u8ba1\u7b97\u51fa\u5ef6\u65f6 N \u6beb\u79d2\u7684\u7cbe\u786e\u503c\u3002\u9488\u5bf9 STM32  \u5fae\u5904\u7406\u5668\uff0c\u9700\u8981\u91cd\u65b0\u8bbe\u8ba1\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u53bb\u5b9e\u73b0\u8be5\u529f\u80fd\uff0c\u4ee5\u5b9e\u73b0\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Delay(N)\u3002<\/p>\n<p><strong><span style=\"color: red;\">(\u4e8c) STM32 SysTick \u4ecb\u7ecd<\/span><\/strong><\/p>\n<p>Cortex-M3 \u7684\u5185\u6838\u4e2d\u5305\u542b\u4e00\u4e2a SysTick \u65f6\u949f\u3002SysTick \u4e3a\u4e00\u4e2a 24 \u4f4d\u9012\u51cf\u8ba1\u6570\u5668\uff0cSysTick \u8bbe\u5b9a\u521d\u503c\u5e76\u4f7f\u80fd\u540e\uff0c\u6bcf\u7ecf\u8fc7  1 \u4e2a\u7cfb\u7edf\u65f6\u949f\u5468\u671f\uff0c\u8ba1\u6570\u503c\u5c31\u51cf 1\u3002\u8ba1\u6570\u5230 0 \u65f6\uff0cSysTick \u8ba1\u6570\u5668\u81ea\u52a8\u91cd\u88c5\u521d\u503c\u5e76\u7ee7\u7eed\u8ba1\u6570\uff0c\u540c\u65f6\u5185\u90e8\u7684 COUNTFLAG \u6807\u5fd7\u4f1a\u7f6e\u4f4d\uff0c\u89e6\u53d1\u4e2d\u65ad (\u5982\u679c\u4e2d\u65ad\u4f7f\u80fd\u60c5\u51b5\u4e0b)\u3002<\/p>\n<p>\u5728 STM32 \u7684\u5e94\u7528\u4e2d\uff0c\u4f7f\u7528 Cortex-M3 \u5185\u6838\u7684 SysTick \u4f5c\u4e3a\u5b9a\u65f6\u65f6\u949f\uff0c\u8bbe\u5b9a\u6bcf\u4e00\u6beb\u79d2\u4ea7\u751f\u4e00\u6b21\u4e2d\u65ad\uff0c\u5728\u4e2d\u65ad\u5904\u7406\u51fd\u6570\u91cc\u5bf9 N  \u51cf\u4e00\uff0c\u5728Delay(N) \u51fd\u6570\u4e2d\u5faa\u73af\u68c0\u6d4b N \u662f\u5426\u4e3a 0\uff0c\u4e0d\u4e3a 0 \u5219\u8fdb\u884c\u5faa\u73af\u7b49\u5f85\uff1b\u82e5\u4e3a 0 \u5219\u5173\u95ed SysTick \u65f6\u949f\uff0c\u9000\u51fa\u51fd\u6570\u3002<\/p>\n<p><strong>\u6ce8\uff1a \u5168\u5c40\u53d8\u91cf TimingDelay , \u5fc5\u987b\u5b9a\u4e49\u4e3a volatile \u7c7b\u578b , \u5ef6\u8fdf\u65f6\u95f4\u5c06\u4e0d\u968f\u7cfb\u7edf\u65f6\u949f\u9891\u7387\u6539\u53d8\u3002<\/strong><\/p>\n<p><strong><span style=\"color: red;\">(\u4e09) ST SysTick \u5e93\u6587\u4ef6\n<\/span><\/strong>\n<span style=\"font-family: Arial;\"><span style=\"font-size: x-small;\">\u4f7f\u7528ST\u7684\u51fd\u6570\u5e93\u4f7f\u7528systick\u7684\u65b9\u6cd5\n1\u3001\u8c03\u7528SysTick_CounterCmd()\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0&#8212; \u5931\u80fdSysTick\u8ba1\u6570\u5668\n<\/span><\/span>\n<span style=\"font-family: Arial;\"><span style=\"font-size: x-small;\">2\u3001\u8c03\u7528SysTick_ITConfig  ()\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0&#8212; \u5931\u80fdSysTick\u4e2d\u65ad\n3\u3001\u8c03\u7528SysTick_CLKSourceConfig()\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0&#8212; \u8bbe\u7f6eSysTick\u65f6\u949f\u6e90\u3002\n4\u3001\u8c03\u7528SysTick_SetReload()\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0&#8212; \u8bbe\u7f6eSysTick\u91cd\u88c5\u8f7d\u503c\u3002\n5\u3001\u8c03\u7528SysTick_ITConfig ()\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0&#8212; \u4f7f\u80fdSysTick\u4e2d\u65ad\n6\u3001\u8c03\u7528SysTick_CounterCmd()\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0 \u00a0&#8212; \u5f00\u542fSysTick\u8ba1\u6570\u5668<\/span><\/span>\n<span style=\"font-size: small;\">\n<\/span><\/p>\n<p><strong><span style=\"color: red;\">(\u56db) SystemTick \u5de5\u7a0b\u5b9e\u6218<\/span><\/strong><\/p>\n<p><span style=\"font-size: x-small;\">\u5916\u90e8\u6676\u632f\u4e3a 8 MHz\uff0c9 \u500d\u9891\uff0c\u7cfb\u7edf\u65f6\u949f\u4e3a 72MHz\uff0cSysTick \u7684\u6700\u9ad8\u9891\u7387\u4e3a9MHz\uff08\u6700\u5927\u4e3aHCLK \/  8\uff09\uff0c\u5728\u8fd9\u4e2a\u6761\u4ef6\u4e0b\uff0c\u628a SysTick \u6548\u9a8c\u503c\u8bbe\u7f6e\u62109000\uff0c\u5c06 SysTick \u65f6\u949f\u8bbe\u7f6e\u4e3a 9 MHz\uff0c \u5c31\u80fd\u591f\u4ea7\u751f 1ms \u7684\u65f6\u95f4\u57fa\u503c\uff0c\u5373  SysTick \u4ea7\u751f 1ms \u7684\u4e2d\u65ad\u3002<\/span><\/p>\n<p><span style=\"font-size: x-small;\"> \/<em> Configure the system clocks <\/em>\/\nRCC_Configuration();\nSysTick_Configuration();<\/span><\/p>\n<p><strong><span style=\"font-size: x-small;\"><span style=\"color: purple;\">\u7b2c\u4e00\u6b65: \u914d\u7f6e RCC \u5bc4\u5b58\u5668 \u548c SysTick  \u5bc4\u5b58\u5668<\/span><\/span>\n<\/strong>\n<span style=\"color: darkorange;\"><span style=\"font-size: x-small;\">RCC_Configuration: \u914d\u7f6e RCC \u5bc4\u5b58\u5668<\/span>\n<\/span>\n<span style=\"font-size: x-small;\">void RCC_Configuration(void)\n{\n\/<em> RCC system reset(for debug purpose) <\/em>\/\nRCC_DeInit();<\/span><\/p>\n<p>\/<em> Enable HSE <\/em>\/\nRCC_HSEConfig(RCC_HSE_ON);<\/p>\n<p>\/<em> Wait till HSE is ready <\/em>\/\nHSEStartUpStatus = RCC_WaitForHSEStartUp();<\/p>\n<p>if(HSEStartUpStatus == SUCCESS)\n{\n\/<em> HCLK = SYSCLK <\/em>\/\nRCC_HCLKConfig(RCC_SYSCLK_Div1);<\/p>\n<p>\/<em> PCLK2 = HCLK <\/em>\/\nRCC_PCLK2Config(RCC_HCLK_Div1);<\/p>\n<p>\/<em> PCLK1 = HCLK\/2 <\/em>\/\nRCC_PCLK1Config(RCC_HCLK_Div2);<\/p>\n<p>\/<em> Flash 2 wait state <\/em>\/\nFLASH_SetLatency(FLASH_Latency_2);\n\/<em> Enable Prefetch Buffer <\/em>\/\nFLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);<\/p>\n<p>\/<em> PLLCLK = 8MHz <\/em> 9 = 72 MHz *\/\nRCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);<\/p>\n<p>\/<em> Enable PLL <\/em>\/\nRCC_PLLCmd(ENABLE);<\/p>\n<p>\/<em> Wait till PLL is ready <\/em>\/\nwhile(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\n{\n}<\/p>\n<p>\/<em> Select PLL as system clock source <\/em>\/\nRCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);<\/p>\n<p>\/<em> Wait till PLL is used as system clock source <\/em>\/\nwhile(RCC_GetSYSCLKSource() != 0x08)\n{\n}\n}<\/p>\n<p>\/<em> Enable GPIOA and AFIO clocks <\/em>\/\nRCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |\nRCC_APB2Periph_AFIO, ENABLE);\n}<\/p>\n<p><span style=\"color: darkorange;\">SysTick_Configuration: \u914d\u7f6e SysTick<\/span>\n<span style=\"font-size: x-small;\">void SysTick_Configuration(void)\n{\n\/<em> Select AHB clock(HCLK) as SysTick clock source <\/em>\/\nSysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);<\/span><\/p>\n<p>\/<em> Set SysTick Priority to 3 <\/em>\/\nNVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);<\/p>\n<p>\/<em> SysTick interrupt each 1ms with HCLK equal to 72MHz <\/em>\/\nSysTick_SetReload(72000);<\/p>\n<p>\/<em> Enable the SysTick Interrupt <\/em>\/\nSysTick_ITConfig(ENABLE);\n}<\/p>\n<p><strong><span style=\"color: purple;\"><span style=\"font-size: x-small;\">\u7b2c\u4e8c\u6b65: \u914d\u7f6e SysTick \u4e2d\u65ad\u51fd\u6570<\/span>\n<\/span><\/strong>\n\u8fd9\u91cc\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a TestSig \u5168\u5c40\u53d8\u91cf, \u7528\u4e8e\u6211\u4eec\u4f7f\u7528 Keil \u8f6f\u4ef6\u81ea\u5e26\u7684\u903b\u8f91\u5206\u6790\u4eea\u6765\u5206\u6790.<\/p>\n<p><span style=\"font-size: x-small;\">volatile vu32 TimingDelay = 0;\nvu8 TestSig = 0;<\/span><\/p>\n<p><span style=\"font-size: x-small;\">void SysTickHandler(void)\n{\nTimingDelay&#8211;;\nif(TimingDelay % 2)\n{\nTestSig = 1;\n}\nelse\n{\nTestSig = 0;\n}\n}<\/span><\/p>\n<p><strong><span style=\"color: purple;\"><span style=\"font-size: x-small;\">\u7b2c\u4e09\u6b65: \u7f16\u5199 Delay \u5ef6\u65f6\u51fd\u6570<\/span>\n<\/span><\/strong>\n<span style=\"font-size: x-small;\">Delay: \u7cfb\u7edf\u5ef6\u65f6\u51fd\u6570, \u4f7f\u7528\u7cfb\u7edf\u65f6\u949f\u64cd\u4f5c.<\/span><\/p>\n<p><span style=\"font-size: x-small;\">void Delay(u32 nTime)\n{\n\/<em> Enable the SysTick Counter <\/em>\/\nSysTick_CounterCmd(SysTick_Counter_Enable);<\/span><\/p>\n<p>TimingDelay = nTime;<\/p>\n<p>while(TimingDelay != 0);<\/p>\n<p>\/<em> Disable the SysTick Counter <\/em>\/\nSysTick_CounterCmd(SysTick_Counter_Disable);\n\/<em> Clear the SysTick Counter <\/em>\/\nSysTick_CounterCmd(SysTick_Counter_Clear);\n}<\/p>\n<p><strong><span style=\"color: purple;\"><span style=\"font-size: x-small;\">\u7b2c\u56db\u6b65:\u00a0\u00a0\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528 Delay<\/span>\n<\/span><\/strong>\n<span style=\"font-size: x-small;\">\u5728 Mini-STM32 \u5f00\u53d1\u677f\u4e0a\u6709\u4e24\u4e2a LED \u706f, \u5206\u522b\u662f PA0, PA1. \u6211\u4eec\u505a\u4e2a\u6d41\u6c34\u706f\u7a0b\u5e8f,  \u8ba9\u4ed6\u4eec\u5faa\u73af\u70b9\u4eae.<\/span>\n<span style=\"font-size: x-small;\"> while(1)\n{\nGPIO_SetBits(GPIOA,GPIO_Pin_0);\nDelay(100);\nGPIO_ResetBits(GPIOA,GPIO_Pin_0);\nDelay(100);\nGPIO_SetBits(GPIOA,GPIO_Pin_1);\nDelay(100);\nGPIO_ResetBits(GPIOA,GPIO_Pin_1);\nDelay(100);\n}<\/span><\/p>\n<p><strong><span style=\"color: red;\">(\u4e94) \u4eff\u771f\u8c03\u8bd5\n<\/span><\/strong>\n\u628a\u5de5\u7a0b\u4fbf\u5b9c\u901a\u8fc7\u540e, \u8fdb\u5165\u8f6f\u4ef6\u4eff\u771f\n\u5982\u4e0b\u56fe\u6240\u793a:\u70b9\u51fb\u5de5\u7a0b\u5feb\u6377\u83dc\u5355\u7684\u903b\u8f91\u5206\u6790\u4eea<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/011927fVO.gif\" border=\"0\" alt=\"\" \/> <img decoding=\"async\" id=\"aimg_504\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/011937ggh.jpg\" alt=\"1.jpg\" width=\"554\" \/><\/p>\n<div id=\"aimg_504_menu\"><a title=\"1.jpg\" href=\"http:\/\/www.mystm32.com\/bbs\/attachment.php?aid=504&#038;k=dd34c29233467a864951b273ceef79e0&#038;t=1257839454&#038;nothumb=yes&#038;sid=fbc8qVcdyAAZURNpPJqdN4fWtjJYK7QR4SujFSoyvmCeoaE\" target=\"_blank\"><strong>\u4e0b\u8f7d<\/strong><\/a> (9.38 KB)\n<div>2009-8-16 14:56<\/div>\n<\/div>\n<p>\u5728\u903b\u8f91\u5206\u6790\u4eea\u4e2d\u6211\u4eec\u70b9\u51fb Setup \u6309\u952e\u4f1a\u5f39\u51fa\u5b89\u88c5\u5bf9\u8bdd\u6846.<\/p>\n<p>\u70b9\u53f3\u4e0a\u65b9\u7684 &#8220;\u65b0\u5efa&#8221; \u56fe\u6807, \u5728\u83dc\u5355\u4e2d\u8f93\u5165 &#8220;TestSig&#8221; \u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf.<\/p>\n<p>\u6dfb\u52a0\u5b8c\u4e4b\u540e\u5c31\u53ef\u4ee5\u70b9 Close \u4e86. \u5982\u679c\u60a8\u4eff\u771f\u5b8c\u53ef\u4ee5\u70b9\u51fb \u5de6\u4e0b\u65b9\u7684 &#8220;Kill All&#8221; \u5220\u9664\u6240\u6709\u76d1\u89c6\u53d8\u91cf.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/011927fVO.gif\" border=\"0\" alt=\"\" \/> <img decoding=\"async\" id=\"aimg_505\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/0119395zQ.jpg\" alt=\"2.jpg\" width=\"516\" \/><\/p>\n<div id=\"aimg_505_menu\"><a title=\"2.jpg\" href=\"http:\/\/www.mystm32.com\/bbs\/attachment.php?aid=505&#038;k=398aaf3dec64b8b0b6c290b98625cbf2&#038;t=1257839454&#038;nothumb=yes&#038;sid=fbc8qVcdyAAZURNpPJqdN4fWtjJYK7QR4SujFSoyvmCeoaE\" target=\"_blank\"><strong>\u4e0b\u8f7d<\/strong><\/a> (50.63 KB)\n<div>2009-8-16 14:58<\/div>\n<\/div>\n<p>\u5168\u901f\u8fd0\u884c\u540e\u5c31\u53ef\u4ee5\u770b\u5230\u4e0b\u9762\u7684\u6ce2\u5f62\u4e86\u54e6<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/011927fVO.gif\" border=\"0\" alt=\"\" \/> <img decoding=\"async\" id=\"aimg_506\" src=\"http:\/\/nick.txtcc.com\/wp-content\/uploads\/remote_image\/2010\/05\/011941hek.jpg\" alt=\"3.jpg\" width=\"585\" \/><\/p>\n<div id=\"aimg_506_menu\"><a title=\"3.jpg\" href=\"http:\/\/www.mystm32.com\/bbs\/attachment.php?aid=506&#038;k=9a9010313079c212ceaa8b0fb9118273&#038;t=1257839454&#038;nothumb=yes&#038;sid=fbc8qVcdyAAZURNpPJqdN4fWtjJYK7QR4SujFSoyvmCeoaE\" target=\"_blank\"><strong>\u4e0b\u8f7d<\/strong><\/a> (31.56 KB)\n<div>2009-8-16 15:03<\/div>\n<\/div>\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u4eff\u771f\u5668\u5728 Mini-STM32 \u4e0a\u8c03\u8bd5\u7684\u8bdd\u4f60\u8fd8\u53ef\u4ee5\u770b\u5230\u4e24\u4e2a LED \u5728\u8dd1\u8dd1\u9a6c\u706f\u7a0b\u5e8f\u4e86.\n\u5230\u6b64\u6211\u4eec\u8fd9\u7ae0\u8282\u7684\u6559\u7a0b\u5c31\u7ed3\u675f\u4e86, \u76f8\u4fe1\u5927\u5bb6\u4e5f\u638c\u63e1\u4e86 System Tick \u7684\u7528\u6cd5\u4e86.<\/p>","protected":false},"excerpt":{"rendered":"<p>(\u4e00) \u80cc\u666f\u4ecb\u7ecd \u5728\u4f20\u7edf\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u8f6f\u4ef6\u6309\u4e2d\u901a\u5e38\u5b9e\u73b0 Delay(N) \u51fd\u6570\u7684\u65b9\u6cd5\u4e3a\uff1a for(i = 0; i<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[41,52,51],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-scm","tag-stm32","tag-systick","tag-51"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":3,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":112,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/80\/revisions\/112"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}