[DemoCode下载] N79E715定时器2的捕捉功能

[复制链接]
 楼主| yiyigirl2014 发表于 2017-1-17 20:34 | 显示全部楼层 |阅读模式
  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 Capture Function
  11. //  P1.2 input signal by function generator.
  12. //
  13. //  Output : UART show capture result on hyper-terminal
  14. //***********************************************************************************************************

  15. //=========================== How to use Capture to calculate input signal period ===========================
  16. //  1. If input pulse is 100Hz, the trigger level is only "falling" or only "rising", the Capture time is 10ms,
  17. //     XTAL=11.0592MHz, divider->Fsys/64
  18. //     then Capture count (C0H/C0L) = 1727
  19. //
  20. //         64
  21. //    ----------- * 1727 = 9994.2 us = 10 ms
  22. //     11.0592 M
  23. //
  24. //  2. If input pulse is 100Hz, the trigger level is "falling or rising", XTAL=11.0592MHz, the Capture time is 5ms,
  25. //     divider->Fsys/64
  26. //     then Capture count (C0H/C0L) = 863
  27. //
  28. //         64
  29. //    ----------- * 863 = 4994.2 us = 5 ms
  30. //     11.0592 M
  31. //===========================================================================================================

  32. //------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
  33. //     <o0.6> UART pin Select
  34. //          <0=> Select P1.0, P1.1 as UART pin(default)
  35. //          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
  36. //-------------------------------- <<< end of configuration section >>> -------------------------------------

  37. #define Uart_Port_Sel   0x00

  38. #include <stdio.h>
  39. #include "N79E715.h"
  40. #include "Typedef.h"
  41. #include "Define.h"
  42. #include "Common.h"
  43. #include "Delay.h"
  44. #include "Timer2_Capture.h"
  45. #include "Version.h"

  46. //-----------------------------------------------------------------------------------------------------------
  47. void Capture_Select(E_CAPTURE_SEL cap, E_EDGE_SEL edge)
  48. {
  49.     if (cap == E_CAPTURE0)
  50.     {
  51.         set_CAPEN0;                             // Enable input capture channel0
  52.         switch (edge)
  53.         {
  54.             case E_FALLING_S:
  55.                 CAPCON1 &= CLR_BIT1;            // Falling edge
  56.                 CAPCON1 &= CLR_BIT0;
  57.                 break;
  58.             case E_RISING_S:
  59.                 CAPCON1 &= CLR_BIT1;            // Rising edge
  60.                 CAPCON1 |= SET_BIT0;
  61.                 break;
  62.             case E_FALLING_RISING_S:
  63.                 CAPCON1 |= SET_BIT1;            // Either falling or rising edge
  64.                 CAPCON1 &= CLR_BIT0;
  65.                 break;
  66.         }
  67.     }
  68.     if (cap == E_CAPTURE1)
  69.     {
  70.         set_CAPEN1;                             // Enable input capture channel1
  71.         switch (edge)
  72.         {
  73.             case E_FALLING_S:
  74.                 CAPCON1 &= CLR_BIT3;            // Falling edge
  75.                 CAPCON1 &= CLR_BIT2;
  76.                 break;
  77.             case E_RISING_S:
  78.                 CAPCON1 &= CLR_BIT3;            // Rising edge
  79.                 CAPCON1 |= SET_BIT2;
  80.                 break;
  81.             case E_FALLING_RISING_S:
  82.                 CAPCON1 |= SET_BIT3;            // Either falling or rising edge
  83.                 CAPCON1 &= CLR_BIT2;
  84.                 break;
  85.         }
  86.     }
  87.     if (cap == E_CAPTURE2)
  88.     {
  89.         set_CAPEN2      ;                       // Enable input capture channel2
  90.         switch (edge)
  91.         {
  92.             case E_FALLING_S:
  93.                 CAPCON1 &= CLR_BIT5;            // Falling edge
  94.                 CAPCON1 &= CLR_BIT4;
  95.                 break;
  96.             case E_RISING_S:
  97.                 CAPCON1 &= CLR_BIT5;            // Rising edge
  98.                 CAPCON1 |= SET_BIT4;
  99.                 break;
  100.             case E_FALLING_RISING_S:
  101.                 CAPCON1 |= SET_BIT5;            // Either falling or rising edge
  102.                 CAPCON1 &= CLR_BIT4;
  103.                 break;
  104.         }
  105.     }
  106. }
  107. //-----------------------------------------------------------------------------------------------------------
  108. void Timer2_Clock_Divider_Sel(E_CLKDIV_SEL clkd)
  109. {
  110.     switch (clkd)
  111.     {
  112.         case E_DIV4:                            // Timer2 clock divider is 1/4
  113.             clr_T2DIV2;
  114.             clr_T2DIV1;
  115.             clr_T2DIV0;
  116.             break;
  117.         case E_DIV8:                            // Timer2 clock divider is 1/8
  118.             clr_T2DIV2;
  119.             clr_T2DIV1;
  120.             set_T2DIV0;
  121.             break;
  122.         case E_DIV16:                           // Timer2 clock divider is 1/16
  123.             clr_T2DIV2;
  124.             set_T2DIV1;
  125.             clr_T2DIV0;
  126.             break;
  127.         case E_DIV32:                           // Timer2 clock divider is 1/32
  128.             clr_T2DIV2;
  129.             set_T2DIV1;
  130.             set_T2DIV0;
  131.             break;
  132.         case E_DIV64:                           // Timer2 clock divider is 1/64
  133.             set_T2DIV2;
  134.             clr_T2DIV1;
  135.             clr_T2DIV0;
  136.             break;
  137.         case E_DIV128:                          // Timer2 clock divider is 1/128
  138.             set_T2DIV2;
  139.             clr_T2DIV1;
  140.             set_T2DIV0;
  141.             break;
  142.         case E_DIV256:                          // Timer2 clock divider is 1/256
  143.             set_T2DIV2;
  144.             set_T2DIV1;
  145.             clr_T2DIV0;
  146.             break;
  147.         case E_DIV512:                          // Timer2 clock divider is 1/512
  148.             set_T2DIV2;
  149.             set_T2DIV1;
  150.             set_T2DIV0;
  151.             break;
  152.     }
  153. }
  154. //-----------------------------------------------------------------------------------------------------------
  155. void Auto_Rel_Sel(E_AUTOREL_SEL rel_sel)
  156. {
  157.     switch (rel_sel)
  158.     {
  159.         case E_TF0:                             // Reload when timer2 overflows
  160.             clr_LDTS1;
  161.             clr_LDTS0;
  162.             break;
  163.         case E_CAPF0:                           // Reload when input capture0 event causes
  164.             clr_LDTS1;
  165.             set_LDTS0;
  166.             break;
  167.         case E_CAPF1:                           // Reload when input capture1 event causes
  168.             set_LDTS1;
  169.             clr_LDTS0;
  170.             break;
  171.         case E_CAPF2:                           // Reload when input capture2 event causes
  172.             set_LDTS1;
  173.             set_LDTS0;
  174.             break;
  175.     }
  176. }
  177. //-----------------------------------------------------------------------------------------------------------
  178. void Noise_Filter_Sel(E_CAPTURE_SEL cap)
  179. {
  180.     switch (cap)
  181.     {
  182.         case E_CAPTURE0:
  183.             set_ENF0;                           // Enable noise filer on input capture 0
  184.             break;
  185.         case E_CAPTURE1:
  186.             set_ENF1;                           // Enable noise filer on input capture 1
  187.             break;
  188.         case E_CAPTURE2:
  189.             set_ENF2;                           // Enable noise filer on input capture 2
  190.             break;
  191.     }
  192. }
  193. //-----------------------------------------------------------------------------------------------------------
  194. void Set_IO_Input_Mode(E_CAPTURE_SEL cap)
  195. {
  196.     switch (cap)
  197.     {
  198.         case E_CAPTURE0:                        //T0(P1.2) is input-only mode
  199.             P1M1 = SET_BIT2;
  200.             P1M2 = 0x00;
  201.             break;
  202.         case E_CAPTURE1:                        //T1(P0.7) is input-only mode
  203.             P0M1 = SET_BIT7;
  204.             P0M2 = 0x00;
  205.             break;
  206.         case E_CAPTURE2:                        //T2(P2.0) is input-only mode
  207.             P2M1 = SET_BIT0;
  208.             P2M2 = 0x00;
  209.             break;
  210.     }
  211. }
  212. //-----------------------------------------------------------------------------------------------------------
  213. void Capture_Init(void)
  214. {
  215.     TH2 = RCOMP2H = 0x00;
  216.     TL2 = RCOMP2L = 0x00;

  217.     Set_IO_Input_Mode(E_CAPTURE0);              // Set T0(P1.2) is input mode

  218.     Noise_Filter_Sel(E_CAPTURE0);               // Noise filer select --> capture 0.

  219.     Timer2_Clock_Divider_Sel(E_DIV64);          // Timer 2 clock divider --> Fsys/64

  220.     Auto_Rel_Sel(E_CAPF0);                      // Auto-reload trigger select --> CAPF0

  221.     set_LDEN;                                   // Enable Auto-reload.

  222.     Capture_Select(E_CAPTURE0,E_FALLING_S);     // Input capture 0 level select --> Falling
  223. }
  224. //-----------------------------------------------------------------------------------------------------------
  225. void main(void)
  226. {
  227.     UINT16  C0H_Value,C0L_Value;
  228.     UINT16  C0_Value;
  229.     AUXR1 |= Uart_Port_Sel;                     // Select P10/P11 as UART pin(default)
  230.     InitialUART0_Timer1(9600);                  // 9600 Baud Rate [url=home.php?mod=space&uid=72445]@[/url] 11.0592MHz
  231.     Show_Version_Number_To_PC();
  232.     printf ("\n*===================================================================");
  233.     printf ("\n*  Name: N79E715 Series Timer2 Capture Sample Code.");
  234.     printf ("\n*===================================================================");
  235.     printf ("\nTimer2 Capture Demo Start.\n");
  236.     Capture_Init();
  237.     TR2 = 1;                                    // Trigger Timer2

  238.     while(1)
  239.     {
  240.         C0H_Value = C0H;
  241.         C0L_Value = C0L;
  242.         C0_Value = MAKEWORD(C0H_Value,C0L_Value);
  243.         printf ("\nCapture Value_H = %x",(UINT16)C0H_Value);
  244.         printf ("\nCapture Value_L = %x",(UINT16)C0L_Value);
  245.         printf ("\nCapture Value   = %x",(UINT16)C0_Value);
  246.         printf ("\n");
  247.         C0H = 0;
  248.         C0H = 0;
  249.         Delay1ms(2000);
  250.     }
  251. }
  252. //-----------------------------------------------------------------------------------------------------------


 楼主| yiyigirl2014 发表于 2017-1-17 20:42 | 显示全部楼层
我们可以看出来捕获的功能确实用起来有点难度,只要懂原理再看代码就简单了。
huangcunxiake 发表于 2017-1-17 22:11 | 显示全部楼层
时钟的配置有意思,用了分支语句。
huangcunxiake 发表于 2017-1-19 17:26 | 显示全部楼层
如果看头文件还会发现很多宏。
643757107 发表于 2017-1-20 18:55 | 显示全部楼层
宏是预编译的一种,预编译用好了可以让程序更好用。
huangcunxiake 发表于 2017-1-21 12:34 | 显示全部楼层
很多实现在那些头文件关联的.c里。
598330983 发表于 2017-1-22 17:01 | 显示全部楼层
发现使用的switch case里面怎么好多重复的。
 楼主| yiyigirl2014 发表于 2017-1-25 09:56 | 显示全部楼层
确实例程写的非常标准,很有参考价值
heisexingqisi 发表于 2017-1-25 15:48 | 显示全部楼层
          case E_FALLING_S:
                CAPCON1 &= CLR_BIT1;            // Falling edge
                CAPCON1 &= CLR_BIT0;
                break;
            case E_RISING_S:
                CAPCON1 &= CLR_BIT1;            // Rising edge
                CAPCON1 |= SET_BIT0;
                break;
            case E_FALLING_RISING_S:
                CAPCON1 |= SET_BIT1;            // Either falling or rising edge
                CAPCON1 &= CLR_BIT0;
这三个都是运算符不同而已。
 楼主| yiyigirl2014 发表于 2017-1-27 14:42 | 显示全部楼层
运算符不同,赋值不同。
dongnanxibei 发表于 2017-1-27 15:08 | 显示全部楼层
捕捉有专用的
dongnanxibei 发表于 2017-1-27 15:10 | 显示全部楼层
寄存器,不小心没有打完就发出去了。
dongnanxibei 发表于 2017-1-31 09:43 | 显示全部楼层
不知道在这个捕捉的中间操作的时间会不会算进去。
 楼主| yiyigirl2014 发表于 2017-2-10 00:08 | 显示全部楼层
中间的操作时间相对于定时那就可以忽略了
wahahaheihei 发表于 2017-2-10 19:06 | 显示全部楼层
定时器2的使用不多用几次,弄不清。
zhuotuzi 发表于 2017-2-11 11:27 | 显示全部楼层
51由于寄存器不多因此一般都是用寄存器操作,如果为了方便还可以自己编写基本的库函数。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

229

主题

3675

帖子

10

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