10/17/2008

AT89S5X Time

使用keil C寫一準確Time
因C無法像組語精準
所以使用autoreload and interrupt
使用視波器量測後,準度尚可接受
最小可達1ms



/*
First ver 1.0
sys_clock = 24 MHz
24000000/12=2MHz
1/2MHz=0.0000005s
0.0000005*200=0.0001s
Brian @Taiwan
*/
#include "AT89X52.H"

void sys_init(void);

int time_ms;
int time_1s;

void main(void)
{
sys_init();
time_ms=0;
time_1s=0;
P1_2=1; //Led Output
TR0=1;

while(1)
{
if(time_1s==1)
{
P1_2=~P1_2;
time_1s=0;
}
}
}

void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
}

void Time0_int(void)interrupt 1
{
// TIME 0 MODE2 AUTORELOAD USER UNTERUPT
// 1000 = 0.1S, 10000 = 1S, 100=0.01S ,10=0.001S
time_1ms++;
if(time_ms==10000)
{
time_ms=0;
time_1s=1;
}
}

沒有留言: