📄 timer.c
字号:
/***************************************************************************
timer.c - description
-------------------
begin : Fri May 17 2002
copyright : (C) 2002 by Raphael Zulliger
email : zulli@hsr.ch
***************************************************************************/
/***************************************************************************
* *
* This library is Copyright (c) Raphael Zulliger <zulli@gmx.net>. *
* It is licensed under the GNU Library General Public License (LGPL). *
* *
***************************************************************************/
#include <def.h>
#include <p18c658.h>
#include <app.h>
#include <timer.h>
/* initialized the timer module. the timer-interrupt should occure after every
* one millisecond (1ms).
*/
static BYTE timerLowRegister;
static BYTE timerHighRegister;
void initTimer( void )
{
DWORD wCountOfOverflows;
//Timer1 Settings:
PIE1bits.TMR1IE = (BYTE)0x01; // Enables the Timer0 overflow interrupt
IPR1bits.TMR1IP = (BYTE)0x01; // Setze Timer0 interrupt auf hohe Priorit鋞
RCONbits.IPEN = (BYTE)0x01; // Enable priority levels on interrupts
INTCONbits.GIEL = (BYTE)0x00; // Disables all peripheral interrupts
INTCONbits.GIEH = (BYTE)0x01; // Enable all high priority interrupts
T1CONbits.TMR1CS = (BYTE)0x00; // sets the timer clock, so it increments every fourth (FOsc/4) (external)clock cycle
T1CONbits.TMR1ON = (BYTE)0x01;
// Setting the timer1, so every 1ms an interrupt (overflow) occures
// therefore we have to calculate the correct start-value of the counter (timer)
// register
// theorie: timer increments every FOSC/4. -> count of increments until first
// overflow: (1ms/4)*FOSC (e.g. with 4MHz: 1000)
wCountOfOverflows = ((WORD)FOSC)/((BYTE)0x04); // remeber: FOSC is in khz
timerHighRegister = (BYTE)0xFF - (BYTE)( (wCountOfOverflows&(WORD)0xFF00 ) >> (BYTE)0x08 );
timerLowRegister = (BYTE)0xFF - (BYTE)( (wCountOfOverflows&(WORD)0x00FF ) );
// sets the prescaler to 1:1 (wouldn't be neccessary because the value is 1:1 after reset
T1CONbits.T1CKPS0 = (BYTE)0x00;
T1CONbits.T1CKPS1 = (BYTE)0x00;
// shut off timer1 oscillator
T1CONbits.T1OSCEN = (BYTE)0x00;
// acces of high- and low-byte by 8bit... (instead of 16bit)
T1CONbits.RD16 = (BYTE)0x00;
resetTimer( );
}
void resetTimer( void )
{
TMR1H = timerHighRegister;
TMR1L = timerLowRegister;
PIR1bits.TMR1IF = (BYTE)0x00; // clear interrupt flag for timer 1
}
WORD getTime( WORD* time )
{
WORD wReturnValue;
WORD wTestTime;
wReturnValue = *time;
wTestTime = *time;
if( wTestTime != wReturnValue )
{ // maybe an overflow occured. call this function again (recursive)
wReturnValue = getTime( time );
}
return wReturnValue;
}
void setTime( WORD* time, WORD value )
{
*time = value;
if( *time != value )
{ // it seams that the irq-serviceroutine was called during setting the value, so try again
setTime( time, value );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -