其实所有的定时器都是用计数器做的,PLC也是由单片机构成的。看看单片机的定时程序就明白了# P/ o" l6 B5 k/ t9 a" Y
/****************************中断子程序_定时***************************************/
" p H% e5 q+ x6 _: Vvoid Timer0() interrupt 1
% H' e6 f% s/ {" ?% H1 H {
/ H" O3 J( }! W static Count1=0,Count2=0; //静态变量计数器1" q7 T$ m& c9 x# `* ^( f
Count1++; //每次中断计数器加1
( L3 l$ ? A5 c1 ^2 V2 G Count2++; //每次中断计数器加1
* U8 E6 u* }* B. j if(Count1>=12) //如果计数器1超过12
/ J! P5 b! B, O$ M4 M {
$ C8 J) B+ r' h' A time_led_sansuo=!time_led_sansuo;# e2 v2 _2 \( g
Count1=0;
6 P( Z0 D) }9 U: y! M5 E }
& C- L& L! S' s" @# `" y I* T if(time_led_sansuo)
3 b2 G% l9 e, {, `' P5 k led_dis &= 0xbf;) X9 Z& I, Q( z! N: d$ x# v
else
" h. \! V3 D. ~( p& P, c3 a led_dis |= 0x40; & d! M' j* e. k) ?( U1 l5 t
if(Count2>=100) //如果计数器2超过100
4 a" r( }+ i* Y- K' `+ W$ S0 N { . A" d" v {' e3 o- F% }
time_run--;
- F; h" n0 I5 b6 `( c if(time_run==0)4 ]+ E4 h8 p0 h) j7 r- C2 w
{
5 |$ q& P# C3 j6 |$ r; f2 W& f ET0=0; //关定时器0& R, d- z+ ?; _+ ~* y8 T& O' R7 W/ m
Time_on=2;
) @5 |9 c# M: F+ f- j EX0=1; //开外部0中断;6 p/ H" K" P! v3 v& b
}( G- [( m' k6 P& B9 t
Count2=0; //计数器清零" F+ C) h$ O5 H% h3 P
}! b& J1 `& U% z: m K, ?
* w5 J1 O9 c8 m) ^. C TH0=0x28; //重置定时初值
. _2 R. e0 Y8 b* V9 d$ c; C0 _! v TL0=0x4b;
6 D, w. i5 F, p } |