[DemoCode下载]

N79E715的Timer2捕捉

[复制链接]
704|10
手机看帖
扫描二维码
随时随地手机跟帖
mintspring|  楼主 | 2017-3-24 09:47 | 显示全部楼层 |阅读模式
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  E-mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: Timer2 Capture Function
//  P1.2 input signal by function generator.
//
//  Output : UART show capture result on hyper-terminal
//***********************************************************************************************************

//=========================== How to use Capture to calculate input signal period ===========================
//  1. If input pulse is 100Hz, the trigger level is only "falling" or only "rising", the Capture time is 10ms,
//     XTAL=11.0592MHz, divider->Fsys/64
//     then Capture count (C0H/C0L) = 1727
//
//         64
//    ----------- * 1727 = 9994.2 us = 10 ms
//     11.0592 M
//
//  2. If input pulse is 100Hz, the trigger level is "falling or rising", XTAL=11.0592MHz, the Capture time is 5ms,
//     divider->Fsys/64
//     then Capture count (C0H/C0L) = 863
//
//         64
//    ----------- * 863 = 4994.2 us = 5 ms
//     11.0592 M
//===========================================================================================================

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

#define Uart_Port_Sel   0x00

#include <stdio.h>
#include "N79E715.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Timer2_Capture.h"
#include "Version.h"

//-----------------------------------------------------------------------------------------------------------
void Capture_Select(E_CAPTURE_SEL cap, E_EDGE_SEL edge)
{
    if (cap == E_CAPTURE0)
    {
        set_CAPEN0;                             // Enable input capture channel0
        switch (edge)
        {
            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;
                break;
        }
    }
    if (cap == E_CAPTURE1)
    {
        set_CAPEN1;                             // Enable input capture channel1
        switch (edge)
        {
            case E_FALLING_S:
                CAPCON1 &= CLR_BIT3;            // Falling edge
                CAPCON1 &= CLR_BIT2;
                break;
            case E_RISING_S:
                CAPCON1 &= CLR_BIT3;            // Rising edge
                CAPCON1 |= SET_BIT2;
                break;
            case E_FALLING_RISING_S:
                CAPCON1 |= SET_BIT3;            // Either falling or rising edge
                CAPCON1 &= CLR_BIT2;
                break;
        }
    }
    if (cap == E_CAPTURE2)
    {
        set_CAPEN2      ;                       // Enable input capture channel2
        switch (edge)
        {
            case E_FALLING_S:
                CAPCON1 &= CLR_BIT5;            // Falling edge
                CAPCON1 &= CLR_BIT4;
                break;
            case E_RISING_S:
                CAPCON1 &= CLR_BIT5;            // Rising edge
                CAPCON1 |= SET_BIT4;
                break;
            case E_FALLING_RISING_S:
                CAPCON1 |= SET_BIT5;            // Either falling or rising edge
                CAPCON1 &= CLR_BIT4;
                break;
        }
    }
}
//-----------------------------------------------------------------------------------------------------------
void Timer2_Clock_Divider_Sel(E_CLKDIV_SEL clkd)
{
    switch (clkd)
    {
        case E_DIV4:                            // Timer2 clock divider is 1/4
            clr_T2DIV2;
            clr_T2DIV1;
            clr_T2DIV0;
            break;
        case E_DIV8:                            // Timer2 clock divider is 1/8
            clr_T2DIV2;
            clr_T2DIV1;
            set_T2DIV0;
            break;
        case E_DIV16:                           // Timer2 clock divider is 1/16
            clr_T2DIV2;
            set_T2DIV1;
            clr_T2DIV0;
            break;
        case E_DIV32:                           // Timer2 clock divider is 1/32
            clr_T2DIV2;
            set_T2DIV1;
            set_T2DIV0;
            break;
        case E_DIV64:                           // Timer2 clock divider is 1/64
            set_T2DIV2;
            clr_T2DIV1;
            clr_T2DIV0;
            break;
        case E_DIV128:                          // Timer2 clock divider is 1/128
            set_T2DIV2;
            clr_T2DIV1;
            set_T2DIV0;
            break;
        case E_DIV256:                          // Timer2 clock divider is 1/256
            set_T2DIV2;
            set_T2DIV1;
            clr_T2DIV0;
            break;
        case E_DIV512:                          // Timer2 clock divider is 1/512
            set_T2DIV2;
            set_T2DIV1;
            set_T2DIV0;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Auto_Rel_Sel(E_AUTOREL_SEL rel_sel)
{
    switch (rel_sel)
    {
        case E_TF0:                             // Reload when timer2 overflows
            clr_LDTS1;
            clr_LDTS0;
            break;
        case E_CAPF0:                           // Reload when input capture0 event causes
            clr_LDTS1;
            set_LDTS0;
            break;
        case E_CAPF1:                           // Reload when input capture1 event causes
            set_LDTS1;
            clr_LDTS0;
            break;
        case E_CAPF2:                           // Reload when input capture2 event causes
            set_LDTS1;
            set_LDTS0;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Noise_Filter_Sel(E_CAPTURE_SEL cap)
{
    switch (cap)
    {
        case E_CAPTURE0:
            set_ENF0;                           // Enable noise filer on input capture 0
            break;
        case E_CAPTURE1:
            set_ENF1;                           // Enable noise filer on input capture 1
            break;
        case E_CAPTURE2:
            set_ENF2;                           // Enable noise filer on input capture 2
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Set_IO_Input_Mode(E_CAPTURE_SEL cap)
{
    switch (cap)
    {
        case E_CAPTURE0:                        //T0(P1.2) is input-only mode
            P1M1 = SET_BIT2;
            P1M2 = 0x00;
            break;
        case E_CAPTURE1:                        //T1(P0.7) is input-only mode
            P0M1 = SET_BIT7;
            P0M2 = 0x00;
            break;
        case E_CAPTURE2:                        //T2(P2.0) is input-only mode
            P2M1 = SET_BIT0;
            P2M2 = 0x00;
            break;
    }
}
//-----------------------------------------------------------------------------------------------------------
void Capture_Init(void)
{
    TH2 = RCOMP2H = 0x00;
    TL2 = RCOMP2L = 0x00;

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

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

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

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

    set_LDEN;                                   // Enable Auto-reload.

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

    while(1)
    {
        C0H_Value = C0H;
        C0L_Value = C0L;
        C0_Value = MAKEWORD(C0H_Value,C0L_Value);
        printf ("\nCapture Value_H = %x",(UINT16)C0H_Value);
        printf ("\nCapture Value_L = %x",(UINT16)C0L_Value);
        printf ("\nCapture Value   = %x",(UINT16)C0_Value);
        printf ("\n");
        C0H = 0;
        C0H = 0;
        Delay1ms(2000);
    }
}
//-----------------------------------------------------------------------------------------------------------


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 | 显示全部楼层
我还是比较习惯自己编写代码

使用特权

评论回复
不知道怎么让3.3V的单片机驱动5V的设备。。

使用特权

评论回复
huangcunxiake| | 2017-4-6 20:06 | 显示全部楼层
以模块化的方式整合您所需要的所有器件

使用特权

评论回复
玛尼玛尼哄| | 2017-4-6 21:16 | 显示全部楼层
定时器入门的基本技能就有这个捕获的应用。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

281

主题

4801

帖子

23

粉丝