CH552/project/examples/Timer/Timer.C

154 lines
6.7 KiB
C++
Raw Normal View History

2018-10-17 05:37:36 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name : Timer.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 Time <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>T2<EFBFBD><EFBFBD>׽<EFBFBD><EFBFBD><EFBFBD>ܿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>жϺ<EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
2018-10-17 10:44:45 +02:00
#include "..\Public\CH554.H"
#include "..\Public\Debug.H"
#include "Timer.H"
2018-10-17 05:37:36 +02:00
#include "stdio.h"
#pragma NOAREGS
/*******************************************************************************
* Function Name : mTimer_x_ModInit(UINT8 x ,UINT8 mode)
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UINT8 mode,Timerģʽѡ<EFBFBD><EFBFBD>
0<EFBFBD><EFBFBD>ģʽ0<EFBFBD><EFBFBD>13λ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>TLn<EFBFBD>ĸ<EFBFBD>3λ<EFBFBD><EFBFBD>Ч
1<EFBFBD><EFBFBD>ģʽ1<EFBFBD><EFBFBD>16λ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
2<EFBFBD><EFBFBD>ģʽ2<EFBFBD><EFBFBD>8λ<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
3<EFBFBD><EFBFBD>ģʽ3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8λ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD> Timer0
3<EFBFBD><EFBFBD>ģʽ3<EFBFBD><EFBFBD>Timer1ֹͣ
* Output : None
* Return : <EFBFBD>ɹ<EFBFBD> SUCCESS
ʧ<EFBFBD><EFBFBD> FAIL
*******************************************************************************/
UINT8 mTimer_x_ModInit(UINT8 x ,UINT8 mode)
{
if(x == 0){TMOD = TMOD & 0xf0 | mode;}
else if(x == 1){TMOD = TMOD & 0x0f | (mode<<4);}
else if(x == 2){RCLK = 0;TCLK = 0;CP_RL2 = 0;} //16λ<36>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ض<EFBFBD>ʱ<EFBFBD><CAB1>
else return FAIL;
return SUCCESS;
}
/*******************************************************************************
* Function Name : mTimer_x_SetData(UINT8 x,UINT16 dat)
* Description : CH554Timer0 TH0<EFBFBD><EFBFBD>TL0<EFBFBD><EFBFBD>ֵ
* Input : UINT16 dat;<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
* Output : None
* Return : None
*******************************************************************************/
void mTimer_x_SetData(UINT8 x,UINT16 dat)
{
UINT16 tmp;
tmp = 65536 - dat;
2018-10-17 10:44:45 +02:00
if(x == 0){TL0 = tmp & 0xff;TH0 = (tmp>>8) & 0xff;}
else if(x == 1){TL1 = tmp & 0xff;TH1 = (tmp>>8) & 0xff;}
else if(x == 2){
RCAP2L = TL2 = tmp & 0xff; //16λ<36>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ض<EFBFBD>ʱ<EFBFBD><CAB1>
RCAP2H = TH2 = (tmp>>8) & 0xff;
2018-10-17 05:37:36 +02:00
}
}
/*******************************************************************************
* Function Name : CAP2Init(UINT8 mode)
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2 T2EX<EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD>׽<EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD>ʼ<EFBFBD><EFBFBD>
UINT8 mode,<EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD>׽ģʽѡ<EFBFBD><EFBFBD>
0:T2ex<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD>
1:T2ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD>
3:T2ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAP2Init(UINT8 mode)
{
RCLK = 0;
TCLK = 0;
C_T2 = 0;
EXEN2 = 1;
CP_RL2 = 1; //<2F><><EFBFBD><EFBFBD>T2ex<65>IJ<EFBFBD>׽<EFBFBD><D7BD><EFBFBD><EFBFBD>
T2MOD |= mode << 2; //<2F><><EFBFBD>ز<EFBFBD>׽ģʽѡ<CABD><D1A1>
}
/*******************************************************************************
* Function Name : CAP1Init(UINT8 mode)
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2 T2<EFBFBD><EFBFBD><EFBFBD>Ų<EFBFBD>׽<EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD>ʼ<EFBFBD><EFBFBD>T2
UINT8 mode,<EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD>׽ģʽѡ<EFBFBD><EFBFBD>
0:T2ex<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD>
1:T2ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD>
3:T2ex<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAP1Init(UINT8 mode)
{
RCLK = 0;
TCLK = 0;
CP_RL2 = 1;
C_T2 = 0;
T2MOD = T2MOD & ~T2OE | (mode << 2) | bT2_CAP1_EN; //ʹ<><CAB9>T2<54><32><EFBFBD>Ų<EFBFBD>׽<EFBFBD><D7BD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ز<EFBFBD>׽ģʽѡ<CABD><D1A1>
}
#ifdef T0_INT
/*******************************************************************************
* Function Name : mTimer0Interrupt()
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
void mTimer0Interrupt( void ) interrupt INT_NO_TMR0 using 1 //timer0<72>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>1
{ //<2F><>ʽ<33><CAB1>TH0ʹ<30><CAB9>Timer1<72><31><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>Դ
SCK = ~SCK;
// mTimer_x_SetData(0,0x0000); //<2F><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ط<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>¸<EFBFBD>TH0<48><30>TL0<4C><30>ֵ
}
#endif
#ifdef T1_INT
/*******************************************************************************
* Function Name : mTimer1Interrupt()
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
void mTimer1Interrupt( void ) interrupt INT_NO_TMR1 using 2 //timer1<72>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>2
{ //<2F><>ʽ<33><CAB1>Timer1ֹͣ
SCK = ~SCK;
// mTimer_x_SetData(1,0x0000); //<2F><><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ط<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>¸<EFBFBD>TH1<48><31>TL1<4C><31>ֵ
}
#endif
2018-10-17 10:44:45 +02:00
#ifdef T2_INT
2018-10-17 05:37:36 +02:00
/*******************************************************************************
* Function Name : mTimer2Interrupt()
* Description : CH554<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
2018-10-17 10:44:45 +02:00
void mTimer2Interrupt( void ) interrupt INT_NO_TMR2 using 3 //timer2<72>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>3
2018-10-17 05:37:36 +02:00
{
mTimer2RunCTL( 0 ); //<2F>ض<EFBFBD>ʱ<EFBFBD><CAB1>
#ifdef T2_CAP
if(EXF2) //T2ex<65><78>ƽ<EFBFBD><EFBFBD>ж<EFBFBD><D0B6>жϱ<D0B6>־
{
SCK = ~SCK; //P17<31><37>ƽָʾ<D6B8><CABE><EFBFBD><EFBFBD>
Cap[FLAG++] = RCAP2; //T2EX
printf("RCAP2 %04x \n",RCAP2);
EXF2 = 0; //<2F><><EFBFBD><EFBFBD>T2ex<65><78>׽<EFBFBD>жϱ<D0B6>־
}
if(CAP1F) //T2<54><32>ƽ<EFBFBD><C6BD>׽<EFBFBD>жϱ<D0B6>־
{
Cap[FLAG++] = T2CAP1; //T2;
printf("T2CAP1 %04x \n",T2CAP1);
CAP1F = 0; //<2F><><EFBFBD><EFBFBD>T2<54><32>׽<EFBFBD>жϱ<D0B6>־
}
#endif
if(TF2)
2018-10-17 10:44:45 +02:00
{
TF2 = 0; //<2F><><EFBFBD>ն<EFBFBD>ʱ<EFBFBD><CAB1>2<EFBFBD><32><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
SCK = ~SCK; //P17<31><37>ƽָʾ<D6B8><CABE><EFBFBD><EFBFBD>
2018-10-17 05:37:36 +02:00
}
2018-10-17 10:44:45 +02:00
mTimer2RunCTL( 1 ); //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
2018-10-17 05:37:36 +02:00
}
#endif