[DemoCode下载] N79E715的Timer2捕捉

[复制链接]
 楼主| mintspring 发表于 2017-3-24 09:47 | 显示全部楼层 |阅读模式
  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. //-----------------------------------------------------------------------------------------------------------


 楼主| mintspring 发表于 2017-3-24 09:50 | 显示全部楼层
先進的0.35um BCD工艺,全面提升您的竞争力

新唐科技 晶圆代工 提供0.35um BCD工艺,以模块化的方式整合您所需要的所有器件,实现于单一工艺完成您所有产品的设计(如:AC / DC,DC / DC,充电器,LED等相关产品)。使用同一工艺平台,将有效缩短各产品的开发时程,加速产品Time-to-Market。配合世界一流的元件特性,提供更具竞争力产品性能,选择的0.35um BCD工艺满足您所有的需求。


home-0406.jpg


wahahaheihei 发表于 2017-3-24 15:25 | 显示全部楼层
这个原理不搞懂,只看代码你难以理解。
 楼主| mintspring 发表于 2017-3-26 00:47 | 显示全部楼层
喜欢3.3V系统,可以轻松的锂电池供电。
heisexingqisi 发表于 2017-3-26 16:46 | 显示全部楼层
这种结构的单片机性能高,驱动能力强
dongnanxibei 发表于 2017-3-27 11:25 | 显示全部楼层
这几个定时器用法还略有区别。
稳稳の幸福 发表于 2017-3-27 15:42 | 显示全部楼层
还配置的有噪声滤波器,太用心了。
598330983 发表于 2017-3-31 22:18 来自手机 | 显示全部楼层
我还是比较习惯自己编写代码
天灵灵地灵灵 发表于 2017-4-6 13:44 | 显示全部楼层
不知道怎么让3.3V的单片机驱动5V的设备。。
huangcunxiake 发表于 2017-4-6 20:06 | 显示全部楼层
以模块化的方式整合您所需要的所有器件
玛尼玛尼哄 发表于 2017-4-6 21:16 | 显示全部楼层
定时器入门的基本技能就有这个捕获的应用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

302

主题

4962

帖子

24

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