timer.c

#include <reg52.h>
#include "utilities.h"
#define TMR0
#define TMR0_MOD1
//#define TMR0_MOD2
//#define TMR1
//#define TMR1_MOD1
//#define TMR1_MOD2

#if defined (TMR0)
#if defined (TMR0_MOD1)
uchar T0RH = 0;
uchar T0RL = 0;

void setTmr0Mod1(uint ms) {
    ulong tmp;

    TMOD &= 0xF0;
    TMOD |= 0x01;
    tmp = 11059200 / 12;
    tmp = tmp * ms / 1000;
    tmp = 65536 - tmp;
    tmp += 28;
    T0RL = tmp;
    T0RH = tmp >> 8;
}
#elif defined (TMR0_MOD2)
void setTmr0Mod2(uint us) {    //微秒,最多定时277微秒
    ulong tmp;

    TMOD &= 0xF0;
    TMOD |= 0x02;
    tmp = us * 11059200 / 12000000;     //不会超过unsigned int
    tmp = 256 - tmp;
    TL0 = tmp;
    TH0 = tmp;
}
#endif // defined
#endif // defined



#if defined (TMR1)
#if defined (TMR1_MOD1)
uchar T1RH = 0;
uchar T1RL = 0;

void setTmr1Mod1(uint ms) {
    ulong tmp;

    tmp = 11059200 / 12;
    tmp = tmp * ms / 1000;
    tmp = 65536 - tmp;
    tmp += 28;
    T1RL = tmp;
    T1RH = tmp >> 8;
}
#elif defined (TMR1_MOD2)
void setTmr1Mod2(uint us) {     //微秒,最多定时277微秒
    ulong tmp;

    TMOD &= 0x0F;
    TMOD |= 0x20;
    tmp = us * 11059200 / 12000000;     //不会超过unsigned int
    tmp = 256 - tmp;
    TL1 = tmp;
    TH1 = tmp;
}
#endif // defined
#endif // defined




你可能感兴趣的:(timer.c)