/*---------------------------------------------------------------------------------------------------------*/
/* */
/* 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);
}
}
//-----------------------------------------------------------------------------------------------------------
|