📄 systimer.c
字号:
//*****************************************************************
//
// File Name : 'systimer.c'
// Title : Procyon MP3 player system timer code
// Author : Pascal Stang
// Date : 11/22/2000
// Version : 0.1
// Target MCU : ATmega103
// Editor Tabs : 3
//
//*****************************************************************
#include <io.h>
#include <signal.h>
#include <interrupt.h>
#include "global.h"
#include "systimer.h"
// Clock prescaling
// 0 = STOP
// 1 = CLOCK tics= 4MHz overflow= 15625Hz
// 2 = CLOCK/8 tics= 500kHz overflow= 1953.125Hz
// 3 = CLOCK/64 tics= 62500Hz overflow= 244.141Hz
// 4 = CLOCK/256 tics= 15625Hz overflow= 61.035Hz
// 5 = CLOCK/1024 tics= 3906.25Hz overflow= 15.259Hz
#define TIMER0CLOCK 0x04 // timer 0 prescaler
volatile unsigned int TimeReg0;
volatile unsigned int TimeReg1;
SIGNAL(SIG_OVERFLOW0) // interrupt handler for tcnt0 overflow interrupt
{
TimeReg0++; // increment low byte counter
if(!TimeReg0) // if low byte counter rollover
{
TimeReg1++; // increment high byte counter
}
}
void timerPause(unsigned int pausetime)
{
register unsigned int endtime = TimeReg0+(pausetime>>5);
while(TimeReg0 != endtime);
}
void timerInit0(void)
{
outp((1<<TOIE0), TIMSK); // enable TCNT0 overflow
outp(TIMER0CLOCK, TCCR0); // count with cpu clock/1024
outp(0, TCNT0); // reset TCNT0
TimeReg0 = 0; // initialize time registers
TimeReg1 = 0; // initialize time registers
sei(); // enable interrupts
}
// delay for a minimum of <us> microseconds
// the time resolution is dependent on the time the loop takes
// e.g. with 4Mhz and 5 cycles per loop, the resolution is 1.25 us
void delay(unsigned short us)
{
unsigned short delay_loops;
register unsigned short i;
delay_loops = (us+3)/5*CYCLES_PER_US; // +3 for rounding up (dirty)
// one loop takes 5 cpu cycles
for (i=0; i < delay_loops; i++) {};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -