STC32G单片机与STC15系列单片机一样有T0~T4共5个通用定时器。其功能大致相同,与STC15系列单片机定时器不同的是STC32G单片机的定时器每个都多了一个8位预分频器,如下:
这样定时器可作为一个24位定时器使用,做计数器使用与分频器就没有多大意义。
说到SCT32G单片机与STC5单片机的区别,这里就简单介绍一下。STC32G单片机的寄存器分SFR与XFR。XFR的逻辑地址位于XDATA区域,访问前需要将P_SW2寄存器的最高位(EAXFR)置1。如果STC15系列单片机的SFR的逻辑地址能够被8整除,那么该寄存器可位操作。STC32G单片机SFR都可位操作,XFR不可进行位操作,只能整体赋值。SCT32G单片机的寄存器依然是8位的,但SCT32G单片机包含32位加减和16位乘除法指令。SCT32G单片机还硬件扩充了32位乘除法单元MDU32。SCT32G系列单片机内部还集成了增强型的双数据指针。这些都是STC15系列单片机不具备的,另外STC32G单片机还较STC5单片机多出了CAN总线、LIN总线、USB串行总线、RTC实时时钟、LCM(I8080/M6800)接口、DMA等,功能较STC15系列单片机强大很多。可以说SCT32G是一款32位单片机。注意要使用MDU32需将STC官方的库文件加入到项目中。如下:
STC32G使用Keil C251编程,其操作与Kei C51没有明显区别。
继续定时器话题,定时器核心器件是一个加法计数器,其本质是对脉冲计数。计数脉冲来自系统时钟,定时器工作在定时方式(因为系统时钟是一定的,因而计数器寄存器溢出的时间是可预期的)。定时器的计数脉冲来自外部引脚,则工作在计数器方式。定时器的工作模式可以通过对相应寄存器的C/T位置1或置0来进行设置,如:T0是对TMOD寄存器的B2(T0_CT)位进行设置,置0就工作在定时方式,置1就工作在计数器方式。T1是对TMOD寄存器的B6(T1_CT)位进行设置,置0就工作在定时方式,置1就工作在计数器方式。T2是对AUXR寄存器的B3(T2_CT)位进行设置,置0就工作在定时方式,置1就工作在计数器方式。T3是对T4T3M寄存器的B2(T3_CT)位进行设置,置0就工作在定时方式,置1就工作在计数器方式。T4是对T4T3M寄存器的B2(T3_CT)位进行设置,置0就工作在定时方式,置1就工作在计数器方式。
定时器工作在定时模式时,可选对系统时钟不分频计数,或对系统时钟12分频计数。两种不同的计数是通过对计数器相应寄存器的Tnx12位来设置的。置1,不分频;置0,12分频。如:T0设置AUXR寄存器的B7位(T0x12),T1设置AUXR寄存器的B6位(T1x12),T2设置AUXR寄存器的B2位(T2x12),T3设置T4T3M寄存器的B1位(T3x12),T4设置T4T3M寄存器的B5位(T4x12)。
定时器0有4种工作模式:模式0(16位自动重装),模式1(16位不可重装),模式2(8位自动重装),模式3(不可屏蔽中断的16位自动重装)。定时器1有3种工作模式,机定时器0的前3重模式。定时器2、定时器3、定时器4只有16位自动重装一种工作模式。
定时器可用来定时、计数、可编程系统时钟输出及波特率发生器。定时器0不可做波特率发生器。我们还可以利用定时中断实现PWM脉冲输出、外部中断扩展、任意引脚可编程脉冲输出等功能。
定时器编程与GPIO编程一样离不开对相关寄存器的操作,STC32G系列单片机定时器的相关寄存器如下:
做单片机编程时,我们可以用宏来实现寄存器操作这类简单的操作。下面用宏函数来实现定时器相关寄存器的操作与设置。为便于与普通函数的区别,宏函数全部使用大写,具体代码如下:
///T0 Macro Function//
#define T0_TIMER() {T0_CT = 0;} //timer0 work as timer
#define T0_COUNTER() {T0_CT = 1;} //timer0 work as counter
#define T0_TCLK12T() {T0x12 = 0;} //set timer clk cycle time to 12 tmes system clk cycle time
#define T0_TCLK1T() {T0x12 = 1;} //set timer clk cycle time to system clk cycle time
#define T0_INTENABLE() {ET0 = 1;} //timer0 INT enable
#define T0_INTDISABLE() {ET0 = 0;} //timer0 INT disable
#define T0_START() {TR0 = 1;} //timer0 start
#define T0_SOP() {TR0 = 0;} //timer0 stop
#define T0_WAITOVERFLOW(){while(!TF0); TR0 = 0; TF0 = 0; } //wait timer0 conter register overflow
#define T0_CLKOUTENABLE() {T0CLKO = 1;} //timer0 clk output enable
#define T0_CLKOUTDISABLE() {T0CLKO = 0;} //timer0 clk output disable
#define T0MODE_AL16() {T0_M0 = 0; T0_M1 = 0;} //auto load 16bit
#define T0MODE_NAL16() {T0_M0 = 1; T0_M1 = 0;} // no auto load 16bit
#define T0MODE_AL8() {T0_M0 = 0; T0_M1 = 1;} //auto load 8bit
#define T0MODE_USAL16() {T0_M0 = 1; T0_M1 = 1;} //can not sheield auto load 16bit
///T1 Macro Function//
#define T1_TIMER() {T1_CT = 0;} //timer1 work as timer
#define T1_COUNTER() {T1_CT = 1;} //timer1 work as counter
#define T1_TCLK12T() {T1x12 = 0;} //set timer clk cycle time to 12 tmes system clk cycle time
#define T1_TCLK1T() {T1x12 = 1;} //set timer clk cycle timer to system clk cycle time
#define T1_INTENABLE() {ET1 = 1;} //timer1 INT enable
#define T1_INTDISABLE() {ET1 = 0;} //timer1 INT disable
#define T1_START() {TR1 = 1;} //timer1 start
#define T1_SOP() {TR1 = 0;} //timer1 stop
#define T1_WAITOVERFLOW(){while(!TF1); TR1 = 0; TF1 = 0; } //wait timer1 conter register overflow
#define T1_CLKOUTENABLE() {T1CLKO = 1;} //timer1 clk output enable
#define T1_CLKOUTDISABLE() {T1CLKO = 0;} //timer1 clk output disable
#define T1MODE_AL16() {T1_M0 = 0; T1_M1 = 0;} //auto load 16bit
#define T1MODE_NAL16() {T1_M0 = 1; T1_M1 = 0;}// no auto load 16bit
#define T1MODE_AL8() {T1_M0 = 0; T1_M1 = 1;} //auto load 8bit
///T2 Macro Function/
#define T2_TIMER() {T2_CT = 0;} //timer2 work as timer
#define T2_COUNTER() {T2_CT = 1;} //timer2 work as counter
#define T2_TCLK12T() {T2x12 = 0;} //set timer clk cycle time to 12 tmes system clk cycle time
#define T2_TCLK1T() {T2x12 = 1;} //set timer clk cycle time to system clk cycle time
#define T2_INTENABLE() {ET2 = 1;} //timer2 INT enable
#define T2_INTDISABLE() {ET2 = 0;} //timer2 INT disable
#define T2_START() {T2R = 1;} //timer2 start
#define T2_STOP() {T2R = 0;} //timer2 stop
#define T2_WAITOVERFLOW(){while(!T2IF); T2R = 0; T2IF = 0; } //wait timer2 conter register overflow
#define T2_CLKOUTENABLE() {T2CLKO = 1;} //timer2 clk output enable
#define T2_CLKOUTDISABLE() {T2CLKO = 0;} //timer2 clk output disable
///T4 Macro Fuanction/
#define T3_TIMER() {T3_CT = 0;} //timer3 work as timer
#define T3_COUNTER() {T3_CT = 1;} //timer3 work as counter
#define T3_TCLK12T() {T3x12 = 0;} //set timer clk cycle time to 12 tmes system clk cycle time
#define T3_TCLK1T() {T3x12 = 1;} //set timer clk cycle time to system clk cycle time
#define T3_INTENABLE() {ET3 = 1;} //timer3 INT enable
#define T3_INTDISABLE() {ET3 = 0;} //timer3 INT disable
#define T3_START() {T3R = 1;} //timer3 start
#define T3_STOP() {T3R = 0;} //timer3 stop
#define T3_WAITOVERFLOW(){while(!T3IF); T3R = 0; T3IF = 0; } //wait timer3 conter register overflow
#define T3_CLKOUTENABLE() {T3CLKO = 1;} //timer3 clk output enable
#define T3_CLKOUTDISABLE() {T3CLKO = 0;} //timer3 clk output disable
#define T3_CLKOUTSELP05() {T3T4PS = 0;} //select P05 as clk output pin
#define T3_CLKOUTSELP01() {T3T4PS = 1;} //select P01 as clk output pin
对比较复杂的设置就用函数实现,对延时、时钟输出、波特率发生器等相关函数我做了如下声明:
///T0//
/**********************************************************************
Function: T0CounterEnable(BOOL able);
Return value: void
able: T0 work as counter enable/disable, 1: enable, 0: disable(as timer)
Discription: configure T0 work as counter/timer
Example:
T0CounterEnable(1); //T0 work as conter
T0CounterEnable(0); //T0 work timer
***********************************************************************/
void T0CounterEnable(BOOL able);
/**********************************************************************
Function: T0ModeConfigure(TIMER_MODE mode);
Return value: void
mode: T0 work mode AL_16: auto_load 16bit, NAL_16: 16bit no auto load, AL_8: 8bit auto_load,
USAL_16: 16bit auto_load unsheilded
Discription: configure T0 work mode
Example:
T0ModeConfigure(AL16); //T0 work at auto_load 16bit
***********************************************************************/
void T0ModeConfigure(TIMER_MODE mode);
/**********************************************************************
Function: T0PreLoad(ui16 loadValue);
Return value: void
loadValue: load value
Discription: preload T0 counter register
Example:
T0PreLoad(1156);
***********************************************************************/
void T0PreLoad(ui16 loadValue);
/**********************************************************************
Function: T0Delay1uSInit();
Return value: void
Discription: T0 delay 1uS init
Example:
T0Delay1uSInit();
***********************************************************************/
void T0Delay1uSInit();
/**********************************************************************
Function: T0Delay10uSInit();
Return value: void
Discription: T0 delay 10uS init
Example:
T0Delay10uSInit();
***********************************************************************/
void T0Delay10uSInit();
/**********************************************************************
Function: T0Delay1mSInit();
Return value: void
Discription: T0 delay 1mS init
Example:
T0Delay1mSInit();
***********************************************************************/
void T0Delay1mSInit();
/**********************************************************************
Function: T0Delay10mSInit();
Return value: void
Discription: T0 delay 10mS init
Example:
T0Delay10mSInit();
***********************************************************************/
void T0Delay10mSInit();
/**********************************************************************
Function: T0Delay100mSInit();
Return value: void
Discription: T0 delay 100mS init
Example:
T0Delay10mSInit(F30MHz);
***********************************************************************/
void T0Delay100mSInit();
/**********************************************************************
Function: T0Delay1SInit();
Return value: void
Discription: T0 delay 1S init
Example:
T0Delay1SInit();
***********************************************************************/
void T0Delay1SInit();
/**********************************************************************
Function: T0PusleOutInit(ui32 outFr);
Return value: void
outFr: output clk frequency
Discription: T0 control pusle out init
Example:
T0PusleOutInit(1000);
***********************************************************************/
void T0PusleOutInit(ui32 outFr);
/**********************************************************************
Function: T0ClkOutInit(ui32 outFr);
Return value: void
outFr: output clk frequency
Discription: T0 divide sclk fruency out init
Example:
T0PusleOutInit(1000);
***********************************************************************/
void T0ClkOutInit(ui32 outFr); //T0 CLKO at P35
///T1//
/**********************************************************************
Function: T1CounterEnable(BOOL able);
Return value: void
able: T1 work as counter enable/disable, 1: enable, 0: work as timer
Discription: configure T1 work as counter/timer
Example:
T1CounterEnable(1); //T1 work as conter
T1CounterEnable(0); //T1 work timer
***********************************************************************/
void T1CounterEnable(BOOL able);
/**********************************************************************
Function: T1ModeConfigure(TIMER_MODE mode);
Return value: void
mode: timer mode of T0 work at
Discription: configure T1 work mode
Example:
T1ModeConfigure(AL_16); //T1 work at auto_load 16bi
***********************************************************************/
void T1ModeConfigure(TIMER_MODE mode);
/**********************************************************************
Function: T1PreLoad(ui16 loadValue);
Return value: void
loadValue: load value
Discription: preload T1 counter register
Example:
T1PreLoad(1156);
***********************************************************************/
void T1PreLoad(ui16 loadValue);
/**********************************************************************
Function: T1Delay1uSInit();
Return value: void
Discription: T1 delay 1uS init
Example:
T1Delay1uSInit();
***********************************************************************/
void T1Delay1uSInit();
/**********************************************************************
Function: T1Delay10uSInit();
Return value: void
Discription: T1 delay 10uS init
Example:
T1Delay10uSInit();
***********************************************************************/
void T1Delay10uSInit();
/**********************************************************************
Function: T1Delay1mSInit();
Return value: void
Discription: T1 delay 1mS init
Example:
T1Delay1mSInit();
***********************************************************************/
void T1Delay1mSInit();
/**********************************************************************
Function: T1Delay10mSInit();
Return value: void
Discription: T1 delay 10mS init
Example:
T1Delay10mSInit();
***********************************************************************/
void T1Delay10mSInit();
/**********************************************************************
Function: T1Delay100mSInit();
Return value: void
Discription: T1 delay 100mS init
Example:
T1Delay100mSInit();
***********************************************************************/
void T1Delay100mSInit();
/**********************************************************************
Function: T1Delay1SInit();
Return value: void
Discription: T1 delay 1S init
Example:
T1Delay1SInit();
***********************************************************************/
void T1Delay1SInit();
/**********************************************************************
Function: T1PusleOutInit(ui32 outFr);
Return value: void
outFr: ouput pusle frequence
Discription: T1 control pusle out init
Example:
T1PusleOutInit(1000);
***********************************************************************/
void T1PusleOutInit(ui32 outFr);
/**********************************************************************
Function: T1ClkOutInit(ui32 outFr);
Return value: void
outFr: frequency of output pusle
Discription: T1 divide sclk fruency out init
Example:
T1PusleOutInit(1000);
***********************************************************************/
void T1ClkOutInit(ui32 outFr); //T1 CLKO at P34
/**********************************************************************
Function: T1BrOutInit(ui32 outFr);
Return value: void
outFr: baudrate
Discription: T1 as baudrate generator init
Example:
T1BrOutInit(9600);
***********************************************************************/
void T1BrOutInit(ui32 outFr);
///T2/
/**********************************************************************
Function: T2CounterEnable(BOOL able);
Return value: void
able: T2 work as counter enable/disable, 1: work as counter, 0: work as timer
Discription: configure T2 work as counter/timer
Example:
T2CounterEnable(1); //T2 work as conter
T2CounterEnable(0); //T2 work timer
***********************************************************************/
void T2CounterEnable(BOOL able);
/**********************************************************************
Function: T2PreLoad(ui16 loadValue);
Return value: void
loadValue: load value
Discription: preload T2 counter register
Example:
T2PreLoad(1156);
***********************************************************************/
void T2PreLoad(ui16 loadValue);
/**********************************************************************
Function: T2Delay1uSInit();
Return value: void
Discription: T2 delay 1uS init
Example:
T2Delay1uSInit();
***********************************************************************/
void T2Delay1uSInit();
/**********************************************************************
Function: T2Delay10uSInit();
Return value: void
Discription: T2 delay 10uS init
Example:
T0Delay10uSInit();
***********************************************************************/
void T2Delay10uSInit();
/**********************************************************************
Function: T2Delay1mSInit();
Return value: void
fsclk: system clk frequency
Discription: T2 delay 1mS init
Example:
T2Delay1mSInit();
***********************************************************************/
void T2Delay1mSInit();
/**********************************************************************
Function: T2Delay10mSInit();
Return value: void
fsclk: system clk frequency
Discription: T2 delay 10mS init
Example:
T2Delay10mSInit();
***********************************************************************/
void T2Delay10mSInit();
/**********************************************************************
Function: T2Delay100mSInit();
Return value: void
fsclk: system clk frequency
Discription: T2 delay 100mS init
Example:
T2Delay100mSInit();
***********************************************************************/
void T2Delay100mSInit();
/**********************************************************************
Function: T2Delay1SInit();
Return value: void
fsclk: system clk frequency
Discription: T2 delay 1S init
Example:
T2Delay1SInit();
***********************************************************************/
void T2Delay1SInit();
/**********************************************************************
Function: T2PusleOutInit(ui32 outFr);
Return value: void
outFr: frequency of output pusle
Discription: T2 control pusle out init
Example:
T2PusleOutInit(1000);
***********************************************************************/
void T2PusleOutInit(ui32 outFr);
/**********************************************************************
Function: T2ClkOutInit(ui32 outFr);
Return value: void
outFr: fequency of output clk
Discription: T2 divide sclk fruency out init
Example:
T2PusleOutInit(1000);
***********************************************************************/
void T2ClkOutInit(ui32 outFr); //T2 CLKO at P30
/**********************************************************************
Function: T2BrOutInit(ui32 outFr);
Return value: void
fsclk: system clk frequency
outFr: baudrate
Discription: T2 as baudrate generator init
Example:
T2BrOutInit(9600);
***********************************************************************/
void T2BrOutInit(ui32 outFr);
///T3/
/**********************************************************************
Function: T3CounterEnable(BOOL able);
Return value: void
able: T3 work as counter enable/disable, 1: work as counter, 0: work as timer
Discription: configure T3 work as counter/timer
Example:
T3CounterEnable(1); //T3 work as conter
T3CounterEnable(0); //T3 work timer
***********************************************************************/
void T3CounterEnable(BOOL able);
/**********************************************************************
Function: T3PreLoad(ui16 loadValue);
Return value: void
loadValue: load value
Discription: preload T3 counter register
Example:
T3PreLoad(1156);
***********************************************************************/
void T3PreLoad(ui16 loadValue);
/**********************************************************************
Function: T3Delay1uSInit(fsclk);
Return value: void
Discription: T3 delay 1uS init
Example:
T3Delay1uSInit();
***********************************************************************/
void T3Delay1uSInit();
/**********************************************************************
Function: T3Delay10uSInit(fsclk);
Return value: void
Discription: T3 delay 10uS init
Example:
T3Delay10uSInit();
***********************************************************************/
void T3Delay10uSInit();
/**********************************************************************
Function: T3Delay1mSInit();
Return value: void
Discription: T3 delay 1mS init
Example:
T2Delay1mSInit();
***********************************************************************/
void T3Delay1mSInit();
/**********************************************************************
Function: T3Delay10mSInit();
Return value: void
Discription: T3 delay 10mS init
Example:
T3Delay10mSInit();
***********************************************************************/
void T3Delay10mSInit();
/**********************************************************************
Function: T3Delay100mSInit();
Return value: void
Discription: T3 delay 100mS init
Example:
T3Delay100mSInit();
***********************************************************************/
void T3Delay100mSInit();
/**********************************************************************
Function: T3Delay1SInit();
Return value: void
Discription: T3 delay 1S init
Example:
T3Delay1SInit();
***********************************************************************/
void T3Delay1SInit();
/**********************************************************************
Function: T3PusleOutInit(ui32 outFr);
Return value: void
outFr: frequency of output pusle
Discription: T3 control pusle out init
Example:
T3PusleOutInit(1000);
***********************************************************************/
void T3PusleOutInit(ui32 outFr);
/**********************************************************************
Function: T3ClkOutInit(ui32 outFr);
Return value: void
outFr: frequency of output clk
Discription: T3 divide sclk fruency out init
Example:
T3PusleOutInit(1000);
***********************************************************************/
void T3ClkOutInit(ui32 outFr); //T3 CLKO at P04
/**********************************************************************
Function: T3BrOutInit(ui32 outFr);
Return value: void
outFr: baudrate
Discription: T3 as baudrate generator init
Example:
T3BrOutInit(9600);
***********************************************************************/
void T3BrOutInit(ui32 outFr);
///T4/
#define T4_TIMER() {T4_CT = 0;}
#define T4_COUNTER() {T4_CT = 1;}
#define T4_TCLK12T() {T4x12 = 0;}
#define T4_TCLK1T() {T4x12 = 1;}
#define T4_INTENABLE() {ET4 = 1;}
#define T4_INTDISABLE() {ET4 = 0;}
#define T4_START() {T4R = 1;}
#define T4_STOP() {T4R = 0;}
#define T4_WAITOVERFLOW(){while(!T4IF); T4R = 0; T4IF = 0; }
#define T4_CLKOUTENABLE() {T4CLKO = 1;}
#define T4_CLKOUTDISABLE() {T4CLKO = 0;}
#define T4_CLKOUTSELP07() {T3T4PIN = 0;}
#define T4_CLKOUTSELP03() {T3T4PIN = 1;}
/**********************************************************************
Function: T4CounterEnable(BOOL able);
Return value: void
able: 1-T4 work as counter, 0-T4 work as timer
Discription: configure T4 work as counter/timer
Example:
T4CounterEnable(1); //T4 work as conter
T4CounterEnable(0); //T4 work timer
***********************************************************************/
void T4CounterEnable(BOOL able);
/**********************************************************************
Function: T4PreLoad(ui16 loadValue);
Return value: void
loadValue: load value
Discription: preload T4 counter register
Example:
T4PreLoad(1156);
***********************************************************************/
void T4PreLoad(ui16 loadValue);
/**********************************************************************
Function: T4Delay1uSInit();
Return value: void
Discription: T4 delay 1uS init
Example:
T4Delay1uSInit();
***********************************************************************/
void T4Delay1uSInit();
/**********************************************************************
Function: T4Delay10uSInit();
Return value: void
Discription: T4 delay 10uS init
Example:
T4Delay10uSInit();
***********************************************************************/
void T4Delay10uSInit();
/**********************************************************************
Function: T4Delay1mSInit();
Return value: void
Discription: T4 delay 1mS init
Example:
T4Delay1mSInit();
***********************************************************************/
void T4Delay1mSInit();
/**********************************************************************
Function: T4Delay10mSInit();
Return value: void
Discription: T4 delay 10mS init
Example:
T4Delay10mSInit();
***********************************************************************/
void T4Delay10mSInit();
/**********************************************************************
Function: T4Delay100mSInit();
Return value: void
Discription: T4 delay 10mS init
Example:
T4Delay100mSInit();
***********************************************************************/
void T4Delay100mSInit();
/**********************************************************************
Function: T4Delay1SInit();
Return value: void
Discription: T4 delay 1S init
Example:
T4Delay1SInit();
***********************************************************************/
void T4Delay1SInit();
/**********************************************************************
Function: T4PusleOutInit(ui32 outFr);
Return value: void
outFr: frequency of output pusle
Discription: T4 control pusle out init
Example:
T4PusleOutInit(1000);
***********************************************************************/
void T4PusleOutInit(ui32 outFr);
/**********************************************************************
Function: T4ClkOutInit(ui16 outFr);
Return value: void
outFr: frequency of output clk
Discription: T4 divide sclk fruency out init
Example:
T4PusleOutInit(1000);
***********************************************************************/
void T4ClkOutInit(ui32 outFr); //T4 CLKO at P06
/**********************************************************************
Function: T4BrOutInit(ui32 outFr);
Return value: void
outFr: baudrate
Discription: T4 as baudrate generator init
Example:
T4BrOutInit(9600);
***********************************************************************/
void T4BrOutInit(ui32 outFr);
函数的源代码如下:
/*STC32G_Timer.c
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu on 07/11/2022
*/
#include "STC32G_Timer.h"
#include "STC32G_INT.h"
///T0//
//**********************************************************************
void T0CounterEnable(BOOL able)
{
TF0 = 0;
if(able)
T0_CT = 1;
else
T0_CT = 0;
}
//End of T0CounterEnable(BOOL able)
//**********************************************************************
void T0ModeConfigure(TIMER_MODE mode)
{
ui8 tem = TMOD;
TF0 = 0;
ReplaceBits(&tem,1,0,mode);
TMOD = tem;
}
//End of T0ModeConfigure(TIMER_MODE mode)
//**********************************************************************
void T0PreLoad(ui16 loadValue)
{
TL0 = loadValue;
TH0 = loadValue >> 8;
}
//End of T0PreLoad(ui16 loadValue)
//**********************************************************************
void T0Delay1uSInit()
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK1T(); //T0 fclk at FOSC
tem = FOSC / 1000000;
tem = 65536 - tem;
T0PreLoad(tem);
}
//End of T0Delay1uSInit()
//**********************************************************************
void T0Delay10uSInit()
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK1T(); //T0 fclk at FOSC
tem = FOSC / 100000;
tem = 65536- tem;
T0PreLoad(tem);
}
//End of T0Delay10uSInit()
//**********************************************************************
void T0Delay1mSInit()
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK12T(); //T0 fclk is FOSC/12
tem = FOSC / 12 / 1000;
tem = 65536- tem;
T0PreLoad(tem);
}
//End of T0Delay1mSInit()
//**********************************************************************
void T0Delay10mSInit()
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK12T(); //T0 fclk is FOSC/12
tem = FOSC / 12 /100;
tem = 65536- tem;
T0PreLoad(tem);
}
//End of T0Delay10mSInit()
//***********************************************************************/
void T0Delay100mSInit()
{
ui16 tem;
//SysInit();
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK12T(); //T0 fclk is FOSC/12
TM0PS = 0xFF; //pe-divide frequency 256
tem = FOSC / 12 /10 /256;
tem = 65536- tem;
T0PreLoad(tem);
}
//End of T0Delay100mSInit()
//***********************************************************************/
void T0Delay1SInit()
{
ui16 tem;
//SysInit();
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_NAL16();
T0_TCLK12T(); //T0 fclk is FOSC/12
TM0PS = 0xFF; //pe-divide frequency 256
tem = FOSC / 12 / 256;
tem = 65536- tem;
T0PreLoad(tem);
}
//End of T0Delay1SInit()
//**********************************************************************
void T0PusleOutInit(ui32 outFr)
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_AL16();
if(outFr <= FOSC / 65535)
{
AUXR &= ~0x80; //T0 fclk is FOSC/12
tem = FOSC / outFr / 24;
}
else
{
AUXR |= 0x80;
tem = FOSC/ outFr / 2;
}
tem = 65536-tem;
T0PreLoad(tem);
}
//End of T0PusleOutInit(ui32 outFr)
//**********************************************************************
void T0ClkOutInit(ui32 outFr) //T0 CLKO at P35
{
ui16 tem;
T0_TIMER();
ET0 = 0; //INT disable
TF0 = 0;
T0MODE_AL16();
T0_CLKOUTENABLE(); //T0 clk out enable
T0_INTDISABLE(); //T0 INT disable
if(outFr <= FOSC / 65535)
{
T0_TCLK12T(); //T0 fclk is FOSC/12
tem = FOSC / outFr / 24;
}
else
{
T0_TCLK1T(); //T0 fclk is FOSC
tem = FOSC / outFr / 2;
}
tem = 65536-tem;
T0PreLoad(tem);
}
//End of T0ClkOutInit(ui32 outFr)
///T1//
//**********************************************************************
void T1CounterEnable(BOOL able)
{
TF1 = 0;
if(able)
T1_CT = 1;
else
T1_CT = 0;
}
//End of T1CounterEnable(BOOL able)
//**********************************************************************
void T1ModeConfigure(TIMER_MODE mode)
{
ui8 tem = TMOD;
TF1 = 0;
ReplaceBits(&tem,5,4,mode);
TMOD = tem;
}
//End of T1ModeConfigure(TIMER_MODE mode)
//**********************************************************************
void T1PreLoad(ui16 loadValue)
{
TL1 = loadValue;
TH1 = loadValue >> 8;
}
//End of T1PreLoad(ui16 loadValue)
//**********************************************************************
void T1Delay1uSInit()
{
ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK1T(); //T1 clk is FOSC
tem = FOSC / 1000000;
tem = 65536 - tem;
T1PreLoad(tem);
}
//End of T1Delay1uSInit()
//**********************************************************************
void T1Delay10uSInit()
{
ui16 tem = 0;
//ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK1T(); //T1 fclk is FOSC
tem = FOSC / 100000;
tem = 65536 - tem;
T1PreLoad(tem);
}
//End of T1Delay10uSInit()
//**********************************************************************
void T1Delay1mSInit()
{
ui16 tem = 0;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC / 1000 / 12;
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1Delay1mSInit()
//**********************************************************************
void T1Delay10mSInit()
{
ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC/100/12;
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1Delay10mSInit()
//**********************************************************************
void T1Delay100mSInit()
{
ui16 tem;
//SysInit();
TM1PS = 0xFF;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC/10 / 12 /256;
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1Delay100mSInit()
//**********************************************************************
void T1Delay1SInit()
{
ui16 tem;
//SysInit();
TM1PS = 0xFF;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_NAL16();
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC / 12 /256;
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1Delay1SInit()
//**********************************************************************
void T1PusleOutInit(ui32 outFr)
{
ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_AL16();
if(outFr <= FOSC/65535)
{
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T1_TCLK1T(); //T1 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1PusleOutInit(ui32 outFr)
//**********************************************************************
void T1ClkOutInit(ui32 outFr) //T1 CLKO at P34
{
ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_AL16();
T1_CLKOUTENABLE(); //T1 clk out enable
T1_INTDISABLE(); //T1 INT disable
if(outFr <= FOSC / 65535)
{
T1_TCLK12T(); //T1 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T1_TCLK1T(); //T1 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T1PreLoad(tem);
}
//End of T1ClkOutInit(ui32 outFr)
//**********************************************************************
void T1BrOutInit(ui32 outFr)
{
ui16 tem;
T1_TIMER();
ET1 = 0; //INT disable
TF1 = 0;
T1MODE_AL16();
STC_IntT1Init(0, 0); //T1 INT disable
T1_TCLK1T(); //T1 fclk is FOSC
tem = FOSC/outFr/4;
tem = 65536 - tem;
T1PreLoad(tem);
SMOD = 0; //baudrate undouble
TR1 = 1; //T1 start
}
//End of T1BrOutInit(ui32 outFr)
///T2/
//**********************************************************************
void T2CounterEnable(BOOL able)
{
if(able)
T2_CT = 1; //T2 as counter
else
T2_CT = 0;//T2 as timer
}
//End of T2CounterEnable(BOOL able)
//**********************************************************************
void T2PreLoad(ui16 loadValue)
{
T2L = loadValue;
T2H = loadValue >> 8;
}
//End of T2PreLoad(ui16 loadValue)
//**********************************************************************
void T2Delay1uSInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
T2_TCLK1T(); //T2 clk is FOSC
tem = FOSC / 1000000;
tem = 65536 - tem;
T2PreLoad(tem);
}
//End of T2Delay1uSInit()
//**********************************************************************
void T2Delay10uSInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
T2_TCLK1T(); //T2 clk is FOSC
tem = FOSC / 100000;
tem = 65536 - tem;
T2PreLoad(tem);
}
//End of T2Delay10uSInit()
//**********************************************************************
void T2Delay1mSInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / 1000 /12;
tem = 65536 -tem;
T2PreLoad(tem);
}
//End of T2Delay1mSInit(ui32 FOSC)
//**********************************************************************
void T2Delay10mSInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / 100 /12;
tem = 65536 -tem;
T2PreLoad(tem);
}
//End of T2Delay10mSInit()
//**********************************************************************
void T2Delay100mSInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
//SysInit();
TM2PS = 0xFF;
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / 10 / 12 /256;
tem = 65536 -tem;
T2PreLoad(tem);
}
//End of T2Delay100mSInit()
//**********************************************************************
void T2Delay1SInit()
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
//SysInit();
TM2PS = 0xFF;
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / 12 /256;
tem = 65536 -tem;
T2PreLoad(tem);
}
//End of T2Delay1SInit()
//**********************************************************************
void T2PusleOutInit(ui32 outFr)
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
if(outFr <= FOSC/65535)
{
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / outFr / 24;
}
else
{
T2_TCLK1T(); //T2 clk is FOSC
tem = FOSC / outFr / 2;
}
tem = 65536-tem;
T2PreLoad(tem);
}
//End of T2PusleOutInit(ui32 outFr)
//**********************************************************************
void T2ClkOutInit(ui32 outFr) //T2 CLKO at P30
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0;
INTCLKO |= 0x01 < 2; //T2 clk out enable
STC_IntT2Init(0); //T2 INT disable
if(outFr <= FOSC/65535)
{
T2_TCLK12T(); //T2 clk is FOSC/12
tem = FOSC / outFr / 24;
}
else
{
T2_TCLK1T(); //T2 clk is FOSC
tem = FOSC / outFr / 2;
}
tem = 65536-tem;
T2PreLoad(tem);
}
//End of T2ClkOutInit(ui32 outFr)
//**********************************************************************
void T2BrOutInit(ui32 outFr)
{
ui16 tem;
T2_TIMER();
ET2 = 0; //INT disable
T2IF = 0; //T2 INT disable
T2_TCLK1T(); //T2 clk at FOSC
tem = FOSC / outFr / 4;
tem = 65536 -tem;
T2PreLoad(tem);
SMOD = 0; //baudrate undouble
T2R = 1; //T2 start
}
//End of T2BrOutInit(ui32 outFr)
///T3/
//**********************************************************************
void T3CounterEnable(BOOL able)
{
if(able)
T3_CT = 1;
else
T3_CT = 0;
}
//End of T3CounterEnable(BOOL able)
//**********************************************************************
void T3PreLoad(ui16 loadValue)
{
T3L = loadValue;
T3H = loadValue >> 8;
}
//End of T3PreLoad(ui16 loadValue)
//**********************************************************************
void T3Delay1uSInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_TCLK1T(); //T3 fclk is FOSC
tem = FOSC / 1000000;
tem = 65536 - tem;
T3PreLoad(tem);
}
//End of T3Delay1uSInit()
//**********************************************************************
void T3Delay10uSInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_TCLK1T(); //T3 fclk is FOSC
tem = FOSC / 100000;
tem = 65536 - tem;
T3PreLoad(tem);
}
//End of T3Delay10uSInit()
//**********************************************************************
void T3Delay1mSInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC / 1000 /12;
tem = 65536 -tem;
T3PreLoad(tem);
}
//End of T3Delay1mSInit()
//**********************************************************************
void T3Delay10mSInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC / 100 /12;
tem = 65536 -tem;
T3PreLoad(tem);
}
//End of T3Delay10mSInit()
//**********************************************************************
void T3Delay100mSInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
//SysInit();
TM3PS = 0xFF;
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC / 10 / 12 / 256;
tem = 65536 -tem;
T3PreLoad(tem);
}
//End of T3Delay100mSInit()
//**********************************************************************
void T3Delay1SInit()
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
//SysInit();
TM3PS = 0xFF;
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC / 12 / 256;
tem = 65536 -tem;
T3PreLoad(tem);
}
//End of T3Delay1SInit()
//**********************************************************************
void T3PusleOutInit(ui32 outFr)
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
if(outFr <= FOSC/65535)
{
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T3_TCLK1T(); //T3 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T3PreLoad(tem);
}
//End of T3PusleOutInit(ui16 outFr)
//**********************************************************************
void T3ClkOutInit(ui32 outFr) //T3 CLKO at P04
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_CLKOUTENABLE(); //T3 clk out enable
T3_INTDISABLE(); //T3 INT disable
if(outFr <= FOSC/65535)
{
T3_TCLK12T(); //T3 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T3_TCLK1T(); //T3 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T3PreLoad(tem);
}
//End of T3ClkOutInit(ui16 outFr)
//**********************************************************************
void T3BrOutInit(ui32 outFr)
{
ui16 tem;
T3_TIMER();
ET3 = 0; //INT disable
T3IF = 0;
T3_INTDISABLE(); //T3 INT disable
T3_TCLK1T(); //T3 clk at FOSC
tem = FOSC / outFr / 4;
tem = 65536 -tem;
T3PreLoad(tem);
SMOD = 0; //baudrate undouble
T3_START(); //T3 start
}
//End of T3BrOutInit(ui16 outFr)
///T4/
//**********************************************************************
void T4CounterEnable(BOOL able)
{
if(able)
T4_CT = 1;
else
T4_CT = 0;
}
//End of T4CounterEnable(BOOL able)
//**********************************************************************
void T4PreLoad(ui16 loadValue)
{
T4L = loadValue;
T4H = loadValue >> 8;
}
//End of T4PreLoad(ui16 loadValue)
//**********************************************************************
void T4Delay1uSInit()
{
ui16 tem;
ET4 = 0; //INT disable
T4IF = 0;
T4_TIMER();
T4_TCLK1T(); //T4 fclk is FOSC
tem = FOSC/1000000;
T4PreLoad(tem);
}
//End of T4Delay1uSInit()
//**********************************************************************
void T4Delay10uSInit()
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
T4_TCLK1T(); //T4 fclk is FOSC
tem = FOSC/100000;
tem = 65536-tem;
T4PreLoad(tem);
}
//End of T4Delay10uSInit(ui32 FOSC)
//**********************************************************************
void T4Delay1mSInit()
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC / 1000 /12;
tem = 65536 -tem;
T4PreLoad(tem);
}
//End of T4Delay1mSInit()
//**********************************************************************
void T4Delay10mSInit()
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC / 100 /12;
tem = 65536 -tem;
T4PreLoad(tem);
}
//End of T4Delay10mSInit()
//**********************************************************************
void T4Delay100mSInit()
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
//SysInit();
TM4PS = 0xFF;
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC / 10 / 12 / 256;
tem = 65536 -tem;
PCON &= ~0x80; //baudrate undouble
T4PreLoad(tem);
}
//End of T4Delay100mSInit()
//**********************************************************************
void T4Delay1SInit()
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
//SysInit();
TM4PS = 0xFF;
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC / 12 / 256;
tem = 65536 -tem;
T4PreLoad(tem);
}
//End of T4Delay1SInit()
//**********************************************************************
void T4PusleOutInit(ui32 outFr)
{
ui16 tem;
T4_TIMER();
ET4 = 0; //INT disable
T4IF = 0;
T4_INTDISABLE();
if(outFr <= FOSC/65535)
{
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T4_TCLK1T(); //T4 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T4PreLoad(tem);
}
//End of T4PusleOutInit(ui32 FOSC, ui32 outFr)
//**********************************************************************
void T4ClkOutInit(ui32 outFr) //T4 CLKO at P06
{
ui16 tem;
T4_TIMER();
T4IF = 0;
ET4 = 0; //INT disable
T4_CLKOUTENABLE() //T4 clk out enable
if(outFr <= FOSC/65535)
{
T4_TCLK12T(); //T4 fclk is FOSC/12
tem = FOSC/outFr/24;
}
else
{
T4_TCLK1T(); //T4 fclk is FOSC
tem = FOSC/outFr/2;
}
tem = 65536-tem;
T4PreLoad(tem);
}
//End of T4ClkOutInit(ui32 outFr)
//**********************************************************************
void T4BrOutInit(ui32 outFr)
{
ui16 tem;
T4_TIMER();
T4IF = 0;
ET4 = 0; //INT disable
T4T3M |= 0x02; //T4 fclk is FOSC
tem = FOSC / outFr / 4;
tem = 65536 -tem;
T4PreLoad(tem);
T4_INTDISABLE();
T4_START(); //T4 start
}
//End of T4BrOutInit(ui32 outFr)