📄 timer.c
字号:
/*
* ApBUILDER CODE FILE - Intel Corporation
*
*
* Purpose: Contains initialization functions for Multi-function timers and PCA modules.
*
*
* The Software is provided "AS IS."
*
* LIMITATION OF LIABILITY: NEITHER INTEL NOR ITS VENDORS OR AGENTS
* SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
* INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
* OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* While we have made every attempt to completely test this code, we request that
* you personally test it completely prior to actual inclusion in your own projects.
*
* Compiler: Developed using Compass251 from Production Languages corporation.
*
* Ext Packages: None
*
* Author: Brad B.
*
* Revisions:
*
*
*
*
*/
#include "global.h"
#include "timer.h"
#include "main.h" /* for ON/OFF definitions */
/* -----------------7/30/96 10:51AM------------------
Function: init_timer0()
General purpose initialization routine for initializing
timer 0. This function sets up all relevant SFR's for
timer 0, and then turns it on/off depending on the passed flag.
* --------------------------------------------------*/
void init_timer0(char tconval, char tmodval, char tl0val, char th0val, char ie0val, char on_off)
{
TCON &= 0xef; /* Turn off the timer */
TMOD &= 0xf0; /* clear Timer 0 */
TMOD |= (tmodval & 0x0f); /* OR in our mode/control bits for timer 0 */
TL0 = tl0val;
TH0 = th0val;
if (on_off == ON)
{
TCON |= 0x10; /* Turn on the timer */
}
}
/* -----------------7/30/96 10:51AM------------------
Function: init_timer1()
General purpose initialization routine for initializing
timer 1. This function sets up all relevant SFR's for
timer 1, and then turns it on/off depending on the passed flag.
* --------------------------------------------------*/
void init_timer1(char tconval, char tmodval, char tl1val, char th1val, char ie0val, char on_off)
{
TCON &= 0xbf; /* Turn off the timer */
TMOD &= 0x0f; /* clear Timer 1 control */
TMOD |= (tmodval & 0xf0); /* OR in our mode/control bits for timer 1*/
TL1 = tl1val; /* initialize the count registers */
TH1 = th1val;
if (on_off == ON)
{
TCON |= 0x40; /* Turn on the timer */
}
}
/* -----------------7/30/96 10:51AM------------------
Function: init_timer2()
General purpose initialization routine for initializing
timer 2. This function sets up all relevant SFR's for
timer 2, and then turns it on/off depending on the passed flag.
Note, that timer 2 can be used in a variety of modes.
You may want to eliminate the dead-code once you have decided
on a particular configuration.
* --------------------------------------------------*/
void init_timer2(char t2conval, char t2modval, char tl2val, char th2val,
char rcap2lval, char rcap2hval, char t2modesflag, char on_off)
{
TCON &= 0xfb; /* T2CON.2 stop the timer */
T2MOD = t2modval; /* set up the modes. */
/* Which mode are we initializing timer 2 in */
switch (t2modesflag)
{
case 0: /* no operation */
break;
case 1: /* Auto Reload NOTE: Values for reload not inserted by ApBUILDER.. */
case 2: /* Capture */
RCAP2L = 0; /* you fill this re-load value in to the desired value */
RCAP2H = 0; /* you fill this re-load value in to the desired value */
TL2 = 0; /* Initial value for counter Low byte */
TH2 = 0; /* Initial value for counter High Byte */
break;
case 3: /* Timer 2 being used as BaudRate Generator */
/*
These values are set by ApBUILDER.
SCON.6 = 1 baud rate for Serial Port selected to come from 'a' timer
T2CON.4 = 1 Timer 2 overflow pulses selected for baud rate generator
Baud-rate = Timer2 overflow Rate / 16
ie.
11.059 Mhz xtal
9600 Baud desired baud-rate.
9600 = 11.059 E6 / ( 32 * (65536 - RCAP2X) )
RCAP2X = 65500 = 0xffdc
so,
RCAP2L = 0xdc;
RCAP2H = 0xff;
*/
RCAP2L = rcap2lval;
RCAP2H = rcap2hval;
break;
case 4: /* Clock Out on a pin */
/* Output clock = Foxc / (( 4 * (65535 - RCAP2X) )
ie.
100 Hz output
12 Mhz Xtal
100 = 12 E6 / ( 4 * (65535 - RCAP2X) )
RCAP2L = 0xd0;
RCAP2H = 0x8a;
*/
RCAP2L = rcap2lval; /* Clk Out Value */
RCAP2H = rcap2hval; /* Clk Out Value */
break;
default:
/* error */
break;
}
T2CON = t2conval & 0xfb; /* Set all control values for This timer except run-bit */
if (on_off == 1)
{
TCON |= 0x04; /* T2CON.2 start the timer */
}
}
/* -----------------7/30/96 10:51AM------------------
Function: InitPCATimer()
General purpose initialization routine for initializing
the PCA Timer.
* --------------------------------------------------*/
void InitPCATimer(char clval, char chval, char cmodval, char ie0val, char cconval)
{
CCON &= 0xbf; /* Turn off PCA timer to load count*/
CL = clval; /* Set PCA count (low byte)*/
CH = chval; /* Set PCA count (high byte)*/
CMOD = cmodval & 0xBF; /* Set CMOD, but do not enable WDT output on PCA module 4 */
CCON = cconval;
}
/* -----------------7/30/96 10:51AM------------------
Function: InitPCAModule()
General purpose initialization routine for initializing
a PCA Module.
Possible values for "flag" parameter
case 0: No operation, disabled
case 1: Capture mode, trigger on Positive transition of CEXx pin
case 2: Capture mode, trigger on negative edge of CEXx pin
case 3: Capture mode, trigger on negative or positive edge of CEXx pin
case 4: Compare mode, Software timer used to trigger periodic int routines
case 5: Compare mode high-speed output, toggle output port when match
case 6: Compare mode, pulse width modulator
The PCA can generate 8-bit PWMs by comparing the
low byte of the PCA timer(CL) with the low byte of the
compare registers (CCAPnL). When CL < CCAPnL the output
is low. When CL >= CCAPnL the output is high.
To control duty cycle, load CCAPnH with the desired value. It will
be loaded automaticly into CCAPnL upon roll-over of PCA count.
case 7: Watchdog timer mode
Generates reset when count in PCA timer reaches CCAP4X.
* --------------------------------------------------*/
void InitPCAModule(char cmodval, char ccapmval, char ccaplval, char ccaphval, char flag, char module_number)
{
if (module_number == 4)
{
CMOD &= 0xBF; /* Disable watchdog mode to load compare value. Only applicable for module 4 */
}
switch(module_number)
{
case 0:
CCAPM0 = ccapmval;
CCAP0L = ccaplval;
CCAP0H = ccaphval;
break;
case 1:
CCAPM1 = ccapmval;
CCAP1L = ccaplval;
CCAP1H = ccaphval;
break;
case 2:
CCAPM2 = ccapmval;
CCAP2L = ccaplval;
CCAP2H = ccaphval;
break;
case 3:
CCAPM3 = ccapmval;
CCAP3L = ccaplval;
CCAP3H = ccaphval;
break;
case 4:
CCAPM4 = ccapmval;
CCAP4L = ccaplval;
CCAP4H = ccaphval;
break;
}
/* if being used as PWM, load up high-byte capture reg with desired low-byte comparison value. */
if (flag == 6)
{
CCAP1H = ccaplval;
}
/* Set for WDT Mode. Applicable for PCA module 4 only */
if ((module_number == 4) && (flag == 7))
{
CMOD |= 0x40; /*Enable watchdog mode*/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -