铁血丹心LLLL 发表于 2025-1-19 23:52

使用定时器生成精确延时

本帖最后由 铁血丹心LLLL 于 2025-1-19 23:54 编辑

定时器/计数器应用实例通过 HC15P013A0 的 8 位定时器/计数器,可以实现定时任务、外部事件计数等功能。

以下提供几个常见应用场景的代码示例。

3.使用定时器生成精确延时3.1 定时器配置在定时模式下,定时器从一个初始值开始计数,直到溢出为止。通过设置合适的预分频器和计数初值,可以控制定时器的溢出时间,从而实现精确的延时。
假设系统时钟频率为 8 MHz,我们希望定时器产生 1 毫秒的时间延迟。由于定时器是 8 位的,其最大计数值为 255,因此需要设置合适的预分频器。
3.2 硬件配置
[*]系统时钟频率:假设为 8 MHz。
[*]定时器预分频器:设定为 64。
[*]定时器溢出时间:1 毫秒。
3.3 计算定时器溢出的频率系统时钟为 8 MHz,预分频器为 64,则定时器的时钟频率为:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUIAAAA7CAYAAAAO94MhAAAQpklEQVR4Ae2d68sV1RfH+wf6D3rli8B3QRBCIhIikWQkhWFRYhZqaqJFZQmWll2kixdKUSs1M9NuVFRipaWlUqmVd0MrzUtYlpnhZf/47B/fYT1zZo7nHM85zzxn1obzzJw5s2/fvfZ31l5r7XkuCZ4cAUfAESg5ApeUvP/efUfAEXAEghOhC4Ej4AiUHgEnwtKLgAPgCDgCToQuA46AI1B6BJwISy8CDoAj4Ag4EboMOAKOQOkRcCIsvQg0B4DTp0+HtWvXhrlz54ZXXnkl/Pzzz+H8+fOZhX/00UfhmmuuCZdeemn8TJgwIfzzzz+Z93Lxgw8+SO694oorwlNPPRX+/fffsHDhwtCnT5/kt8suuywMHz487N+/P2zZsiVcd911yW/Uxb3k8eQIpBFwIkwj4t/rRgASe/zxx8Onn34ajh8/Hn766adw//33h3feeSeXDKlk1apV4bbbbgtXXXVV+PHHHzPrpezp06eHq6++Otxyyy2xfHsjZDtz5sxIeJs2bbI/xfOvvvoq/vbcc89V/OYXHAEh4EQoJPzYMAJobCtWrOiS/8CBA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BEwAkagBwRMoj2A56xGwAgYAZOoZcAIGAEj0AMCJtEewHNWI2AEjIBJ1DJgBIyAEegBAZNoD+A5qxEwAkbAJGoZMAJGwAj0gIBJtAfwnNUIGAEjYBK1DBgBI2AEekDAJNoDeM66eQj88ccfadWqVenqq69ON910U/rss8/Sv//+W1rYY489lnbddde05ZZb5s+JJ56Yfv3119J7OfnII4+07t1uu+3ShRdemH7//fe0bNmytMMOO7SubbXVVunQQw9Nn3zySXr99dfTHnvs0bpGXdxLHicjMBUCJtGpEPL1viIAAZ5//vnp6aefTt9//336+OOP06mnnpqWL19eSaQ04P77708HHXRQ2n777dO6detK20TZ5513Xtpxxx3T/vvvn8uPN0LUl1xySSbLNWvWxEv5+4svvpivXXbZZZOu+YQRqELAJFqFjM8PBAE0xXvuuWdC2Z9++mk65phj0tdffz3hfPwBiZJv9913T9dcc00p4b733nv5Hgi0jEQpD4LcZZdd0kcffRSLz98hVrRQk+gkaHyiDQIm0Tbg+FL/EYCgbr755gkkiEYKiaKVViVI9Lnnnktnn312OuCAA9J333036da77rorvfbaa5lATaKT4PGJASFgEh0QsC62HIFbb701bb311um6665Lv/32W77p2WefzeSo32U5IVE0xdWrV6dtttkmH+N9P/zwQ7rzzjvTt99+23cS/eWXX7JpALKPn59//nnCyyC2x9/HBwGT6PiMdS16+s0332SHDtPmgw8+OEGqp5xySuJ8uyQSRQNFE0Uj/fPPP1tZIGI+kNxU0/mdd945a6yREPmOnbZsOo/2PHPmzDzNxxl27rnnZiLHthvb0GqMv4wVAibRsRruenQWb/ycOXMyYeElh6TaedxptUgU5xA2UWyj2FJJf//9d9ZC0UY7IdFtt902O7cgxPhZsGDBJBJFO4awsbeSaOf8+fPTPvvsk7766qt8zn/GGwGT6HiP/9B7jyaJ5vn4449nzQ8nD9rfySefnJgeVyWRKNfxzuOlZ/pOgkyxh5I6IdFuHEuQ6BVXXJEgaAj8gQceyHW/8MILuT7/MQImUcvA0BAgPpSpMISotHHjxrRw4cJMpHjuq1IkUb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F0R4yAERgFAibRUaDuOo2AEWgMAibRxgylO2IEjMAoEDCJjgJ112kEjEBjEDCJNmYo3REjYARGgcB/3RnjuAx6CSMAAAAASUVORK5CYII=
由于定时器是 8 位的,最大计数值为 255,所以计数器的溢出时间为:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWIAAAA/CAYAAADaO8LnAAAStklEQVR4Ae2d16skRRTG/Qf8D3zyQfDNJ1EUERERFxVFUVRUTBjQFQNmUdec85pzxoyKOWKOGHbNmDBjjhhKfgVfc6a2u6evM3t7b9+vYOiZ7uoKX1V9derUqTMrJQcjYASMgBHoFYGVes3dmRsBI2AEjEAyEbsTGAEjYAR6RsBE3HMDOHsjYASMgInYfcAIGAEj0DMCJuKeG8DZGwEjYARMxO4DRsAIGIGeETAR99wAzt4IGAEjYCJ2HzACRsAI9IyAibjnBnD2RsAIGAETsfuAETACRqBnBEzEPTeAszcCRsAImIjdB4yAETACPSNgIu65AZy9ETACRsBE7D5gBIyAEegZARNxzw3g7GcXge+++y7dfPPN6YILLkg33HBD+vzzz5cpwNNPP52WLl2afvvtt/Tvv/+mH374IT322GPpww8/rOL++eef6dFHH00ff/xx4vs///yTvvnmm/TAAw+kL7/8sornL0agCwIm4i4oOc4gEHj33XfTEUcckd58881Mqsccc0xaddVV05133pkJl0r+/fff6eSTT04rr7zyyOekk05Kv/76a4XDL7/8khYuXDgSZ5VVVklXXnll+uuvv6p4/mIEuiBgIu6CkuPMeQT++OOPdPzxx6eHH344S69U6Keffkr77bdfWn/99dP7779f1fG8885Lu+++e9pmm23SiSeemF588cXqHUVCWobIibfDDjuks88+O73zzjsVoSuer0agCwIm4i4oOc6cRwCVxLbbbps23HDDERXDLbfckqXau+66q6rjxRdfPELM1YPwBSI+66yzEuk6GIFJETART4qg358TCKAuuPTSS9ORRx45Qp633XZbJmKuCiZiIeHrbCFgIp4tpJ3PCocA+mBUD2ussUZ6/fXXq/JddNFF6bLLLkv77LNP2nrrrdMBBxyQPvjgg+o5X5CITznllHT55Zdn9cRWW22VFi1a5I26EZT8oysCJuKuSDne4BBAp4t+GAuKuMF2zjnnpFtvvbXSC2MxsckmmyQ2+xQgYqTrp556KuuFsa64/vrr084775y++uorRfPVCHRCwETcCSZHGhoCWEAcfvjh6YwzzsjSbawfzzBHU/j222+zZHzaaadlqwruQ7zE46oAUa+33nrpxhtv1C1fjUAnBEzEnWBypCEhgPR74YUXZrUC1hTjgjb6sI74/vvvG6NjeYGEDcH//vvvjfH8wAiUCJiIS0T8e9AIiISj6uGTTz5Jr776aq73I488klZfffV0//33VziIiLG6kJXE1VdfndZcc83qPSKLiA855JBlpOwqMX8xAjUImIhrQPGtYSKAGoHDG5iqRZUCp+GeffbZXGmsJziYEYlYqolIsJiuscknAudlqSbQMcf0h4mmazVNBEzE00TTaa2wCECMSLvocLGE4LCGPmzEvfXWW7nsb7/9dsJqIm7esSG37rrrpmeeeaaq35NPPpk39ES4XJGyN95445FNveoFfzECLQhMlYjpvCzzOKuPpBCPhLaUYdYeUb733nsvl41db3wE4EcgbszMWmGc0awiIPVCeXSZ31tssUX64osvcnkgVKTmQw89ND300EPpkksuSQsWLEj33XffiJRLX4KwTzjhhEzwZ555Zo738ssvz2q9nNkwEJgKEUNkTzzxRJY0MAVCgrjnnnvy0U/O7Uuv1idkOGzZY4898o725ptvntZee+1syB/1fn2UjyWwJLNxV8r/0Ucf5WIyyXHw4KabbhqR3vqowxDz/Prrr9Nzzz3XKlBA2p999lkWPLBDZmIfUmBcc7ybCYcPAlaXzU1h8Omnn6arrroqmwfCD+PeBc+77757RC2ktIZ+nZiIkQxwdMLyDo9VMQA85/b7tq2EtPbff/9MwvgX2HPPPbMkgzF+30SMTvLggw/OXsDoiAT0j0hq0luCMQMCjCVxaWOo7/LH9vb34SBAn9NEj4e6Bx98MFuE4Fuji500apxdd901Hydn9YmNNQ6X2lbJsuuOpxyHg2h7TSYmYiS61VZbLbsJrMsKc59ddtklm/S0NULdu9O6J9J64YUXRpKkwfsmMiaq1157baRcI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K5CYII=

为了实现 1 毫秒的延时,我们可以设置定时器的初值为:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaMAAABMCAYAAADA8NgXAAAUbUlEQVR4Ae2d18skRRfG/Qf8D7zyzjuvRFBEREQUFRdFUVExYVzFgDngmnPOOeeAAXPEHDHrmnXNa86pPn7F94znre2e6dl9552e6adg6J6equqqp07VU+fUqZrlkoMRMAJGwAgYgTEjsNyY3+/XGwEjYASMgBFIJiMLgREwAkbACIwdAZPR2JvABTACRsAIGAGTkWXACBgBI2AExo6AyWjsTeACGAEjYASMgMnIMmAEjIARMAJjR8BkNPYmcAGMgBEwAkbAZGQZMAJGwAgYgbEjYDIaexO4AEbACBgBI2AysgwYASNgBIzA2BEwGY29CVwAI2AEjIARMBlZBoyAETACRmDsCJiMxt4ELoARMAJGwAiYjCwDRmAECHzxxRfpscceG0HOztIITCcCJqPpbFfXagwIfPzxx+nuu+9OBx54YFp55ZXTqaeeOoZS+JVGYDIRMBlNZru51C1EADJ6+umn05tvvpk23XRTk1EL28hFai8CJqP2to1LNqEIfPvtt2nzzTc3GU1o+7nY40HAZDQe3P3WKUagKRn9+uuv6ZtvvkkvvfRSQqv666+/0sKFC7N29dVXX2WE/v333/Tpp5+mJ554Isf5559/ZiCn3++999704IMP5jhoZg5GYNIQMBlNWou5vK1HoCkZYdLbZJNN0vLLL5/OPvvsdPLJJ6dHHnkk3XDDDWnNNddM999/f35+++23p3vuuSfHPe+889Lff/+dMYCILrnkknTVVVdlUoPQFixYYI2s9RLiAlYhYDKqQsXPjMAyINCUjHgFmhHrS9ttt1367rvv8lt/+eWXtO+++6bVVlstvfbaa72SXHnlldn8R/4E4u+2227pnXfe6cWBkCAoByMwaQiYjCatxVze1iMwDBkp7gUXXNCrF+a7/fffPxMSxKRw8803zyCjH374Ie2www5pxx13TA8//HD6+uuvs6kvplFaX41A2xEwGbW9hVy+iUNABNPEtVtxIRoFkRGExL1CSUY8f+6557JJD1Mfn6233jq9//77SuKrEZgYB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2SX+PwtCe/QoZxc+c5zDuckLzbpko79TAsWLMj3XNnbBGGWB9aqDr4agblEwGQ0l2j7XZ1CgE2qkBqnK3Ad96GvaGw69R0SRStCY3MwAm1AwGTUhlZwGYyAETACHUfAZNRxAXD1jYARMAJtQMBk1IZWcBmMgBEwAh1HwGTUcQFw9Y2AETACbUDAZNSGVnAZjIARMAIdR8Bk1HEBcPWNgBEwAm1AwGTUhlZwGYyAETACHUfAZNRxAXD1jYARMAJtQMBk1IZWcBmMgBEwAh1HwGTUcQFw9Y2AETACbUDAZNSGVnAZjIARMAIdR8Bk1HEBcPWNgBEwAm1AwGTUhlZwGYyAETACHUfAZNRxAXD1jYARMAJtQMBk1IZWcBmMgBEwAh1HwGTUcQFw9Y2AETACbUDAZNSGVnAZjIARMAIdR8Bk1HEBcPWNgBEwAm1AwGTUhlZwGYyAETACHUfAZNRxAXD1jYARMAJtQMBk1IZWcBmMgBEwAh1HwGTUcQFw9Y2AETACbUDgf83jFcSR9g4TAAAAAElFTkSuQmCC

3.4 代码实现
#define TIMER0(*(volatile unsigned char*)0x10)// 定时器寄存器地址
#define TMSK    (*(volatile unsigned char*)0x11)// 定时器中断掩码寄存器地址
#define TCTL    (*(volatile unsigned char*)0x12)// 定时器控制寄存器地址

void delay_ms(unsigned int ms) {
    // 配置定时器为定时模式,使用预分频器
    TCTL = 0x04;// 设置定时器工作模式,并启用定时器

    // 设置定时器预分频器为 64
    TCTL |= 0x40; // 选择分频器为 64

    while(ms--) {
      // 设置定时器初始值
      TIMER0 = 127;// 设置定时器初值

      // 等待定时器溢出
      while (TMSK & 0x01) {
            // 等待定时器溢出中断标志清零
      }
    }
}







键盘手没手 发表于 2025-2-27 11:41

定时器是 8 位的,它的最大计数值是 255。为了产生 1 毫秒的延时,我们首先需要计算出定时器的溢出时间。

yangjiaxu 发表于 2025-2-27 13:17

定时器延时确实好用,而且很多时候也是必选的一种延时方法

呐咯密密 发表于 2025-3-28 16:39

通用定时器做延时啊

shenxiaolin 发表于 2025-3-28 20:52

学习一下

地瓜patch 发表于 2025-3-28 23:04

不错的工具,试用一下
页: [1]
查看完整版本: 使用定时器生成精确延时