[DemoCode下载] N79E715的定时器2的使用

[复制链接]
1459|11
 楼主| 643757107 发表于 2017-1-20 19:03 | 显示全部楼层 |阅读模式
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************
  10. //  Application: Timer2 Function
  11. //  This file set up timer 2 in auto-reload mode (16 bits timer) with a hardware gate.
  12. //  The 16-bits register consist of all 8 bits of TH2 and all 8 bits of TL2.
  13. //  Timer2 flag will be set when times out. P1.4 is going to be toggled by polling.
  14. //
  15. //  Output : P1.4 & P2.1 toggle when timer0 flag is set
  16. //***********************************************************************************************************

  17. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  18. //     <o0.6> UART pin Select
  19. //          <0=> Select P1.0, P1.1 as UART pin(default)
  20. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  21. //-------------------------------- <<< end of configuration section >>> -------------------------------------

  22. #define Uart_Port_Sel   0x00

  23. #include <stdio.h>
  24. #include "N79E715.h"
  25. #include "Typedef.h"
  26. #include "Define.h"
  27. #include "Common.h"
  28. #include "Delay.h"
  29. #include "Version.h"

  30. //-----------------------------------------------------------------------------------------------------------
  31. void main(void)
  32. {
  33.     AUXR1 |= Uart_Port_Sel;             // Select P10/P11 as UART pin(default)
  34.     InitialUART0_Timer1(9600);          // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  35.     Show_Version_Number_To_PC();
  36.     printf ("\n*===================================================================");
  37.     printf ("\n*  Name: N79E715 Series Timer2 Sample Code.");
  38.     printf ("\n*===================================================================");
  39.     printf ("\nTimer2 Demo Start.");
  40.     printf ("\nP1.4 toggle by polling.");
  41.     printf ("\nP2.1 toggle by polling.\n");

  42.     T2MOD = 0x90;                       // Enabled auto-reload and timer2 clock divider is 1/8
  43.     TH2 = 0x10;                         // Initial values
  44.     TL2 = 0x00;
  45.     RCOMP2L = TL2 ;                      // Reload values initialize
  46.     RCOMP2H = TH2 ;

  47.     EA = 1;                             // Enable interrupts
  48.     TR2 = 1;                            // Timer2 run
  49.     while(1)
  50.     {
  51.         while(!TF2);
  52.         P14 = ~P14;                     // P1.4 toggle by polling
  53.         P21 = ~P21;                     // P2.1 toggle by polling
  54.         TF2 = 0;                        // Clear timer2 flag(TF2)
  55.     }
  56. }
  57. //-----------------------------------------------------------------------------------------------------------


 楼主| 643757107 发表于 2017-1-20 19:04 | 显示全部楼层
可以看到定时器2其实使用起来还是很简单的。
 楼主| 643757107 发表于 2017-1-20 19:07 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technology Corp.
  8. //  E-mail: MicroC-8bit@nuvoton.com
  9. //***********************************************************************************************************

  10. #include <stdio.h>
  11. #include "N79E715.h"
  12. #include "Typedef.h"
  13. #include "Define.h"
  14. #include "Common.h"
  15. #include "Delay.h"

  16. #ifdef FOSC_110592
  17.     #define VALUE_10us    65536-9       //27*12/11.0592 = 10 us
  18.     #define VALUE_1ms     65536-923     //2769*12/11.0592 = 1 ms
  19. #endif
  20. #ifdef FOSC_184320
  21.     #define VALUE_10us    65536-15      //15*12/18.432 = 10 us
  22.     #define VALUE_1ms     65536-1536    //1536*12/18.432 = 1 ms
  23. #endif
  24. #ifdef FOSC_221184
  25.     #define VALUE_10us    65536-18      //18*12/22.1184 = 10 us
  26.     #define VALUE_1ms     65536-1843    //1843*12/22.1184 = 1 ms
  27. #endif
  28. #ifdef FOSC_368640
  29.     #define VALUE_10us    65536-93      //93*12/36.864 = 10 us
  30.     #define VALUE_1ms     65536-3072    //3072*12/36.864 = 1 ms
  31. #endif
  32. //-------------------------------------------------------------------------
  33. void Delay10us(UINT16 u16CNT)
  34. {
  35.     TMOD &= 0xF0;
  36.     TMOD |= 0x01;
  37.     TR0 = 1;
  38.     while (u16CNT != 0)
  39.     {
  40.         TL0 = LOBYTE(VALUE_10us);
  41.         TH0 = HIBYTE(VALUE_10us);
  42.         while (TF0 != 1);
  43.         TF0 = 0;
  44.         u16CNT --;
  45.     }
  46.     TR0 = 0;
  47. }
  48. //------------------------------------------------------------------------------
  49. void Delay1ms(UINT32 u32CNT)
  50. {
  51.     TMOD &= 0xF0;
  52.     TMOD |= 0x01;
  53.     TR0 = 1;
  54.     while (u32CNT != 0)
  55.     {
  56.         TL0 = LOBYTE(VALUE_1ms);
  57.         TH0 = HIBYTE(VALUE_1ms);
  58.         while (TF0 != 1);
  59.         TF0 = 0;
  60.         u32CNT --;
  61.     }
  62.     TR0 = 0;
  63. }
delay的实现方法。
 楼主| 643757107 发表于 2017-1-20 19:08 | 显示全部楼层
从上面的实现可以看到用了不少的预编译,因此我们要在程序指定用的什么样的晶振时钟。
dongnanxibei 发表于 2017-1-23 20:26 | 显示全部楼层
跟定时器0使用方法差不多的。
wahahaheihei 发表于 2017-1-23 20:32 | 显示全部楼层
好多配置如果可以一个函数搞定就OK了,太多的函数要记住,感觉很吃力。
玛尼玛尼哄 发表于 2017-1-23 20:48 | 显示全部楼层
两个定时器可以同时工作,互相不干扰。
捉虫天师 发表于 2017-1-24 15:24 | 显示全部楼层
用两个定时器同时计时也是可以,可以用作不同的功能。
稳稳の幸福 发表于 2017-1-24 19:41 | 显示全部楼层
void Delay10us(UINT16 u16CNT)
{
    TMOD &= 0xF0;
    TMOD |= 0x01;
    TR0 = 1;
    while (u16CNT != 0)
    {
        TL0 = LOBYTE(VALUE_10us);
        TH0 = HIBYTE(VALUE_10us);
        while (TF0 != 1);
        TF0 = 0;
        u16CNT --;
    }
    TR0 = 0;
}
heisexingqisi 发表于 2017-1-24 20:11 | 显示全部楼层
只看这个是不行的,需要配合手册,要不不知道那些赋值都代表什么。
wahahaheihei 发表于 2017-1-24 20:44 | 显示全部楼层
TL0 = LOBYTE(VALUE_10us);
TH0 = HIBYTE(VALUE_10us);
这个用的比较好。
 楼主| 643757107 发表于 2017-1-26 10:21 | 显示全部楼层
void Delay1ms(UINT32 u32CNT)
{
    TMOD &= 0xF0;
    TMOD |= 0x01;
    TR0 = 1;
    while (u32CNT != 0)
    {
        TL0 = LOBYTE(VALUE_1ms);
        TH0 = HIBYTE(VALUE_1ms);
        while (TF0 != 1);
        TF0 = 0;
        u32CNT --;
    }
    TR0 = 0;
}
其实我看中的就是这些,定时器来延时,比那个只写跑空指令的循环靠谱多了。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3972

帖子

11

粉丝
快速回复 在线客服 返回列表 返回顶部