⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stimer.c

📁 mifarea卡程序mifarea卡程序mifarea卡程序
💻 C
字号:
//////////////////////////////////////////////
//  timer.c                                 //
//  1毫秒系统时钟                           //
//  自定义时间长度中断调用                  //
//  预定义时间长度中断调用                  //
//  design by: 刘俐训                       //
//////////////////////////////////////////////

#include <reg52.h>
#include "stimer.h"
#include <v51rx2.h>
#include "t_event.h"
#include "intrsc.h"
//#include "wdt.h"

#ifndef enable
#define enable()     EA=1
#define disable()    EA=0
#endif

#ifdef  BUZZEROUT
#ifndef BZFRQ
#define BZFRQ
#define BZFRQ0      0
#define BZFRQ2K		2
#define BZFRQ4K		4
#endif
#endif

#ifndef NULL
#define NULL    (void *)0
#endif

#define USER_TIMER  TL0
#define SYS_TIMER   TH0
#define TF_USER     TF0
#define TF_SYS      TF1


typedef struct _INTERVAL
{
    unsigned int mscycles;
    unsigned char tenmsloops;
}INTERVAL;

typedef struct _TSTATUS
{
    union _TICK
    {
        unsigned int cycles;
        unsigned char loops;
    }Tick;
    unsigned int ms;
}TSTATUS;

static INTERVAL data Interval = {921, 36};
static volatile TSTATUS data TStatus = {0};

static unsigned char data LedTick = 0;
static unsigned char data LedCnt = 0;
static bit LedFlash = 0;

static unsigned char data UserReload;
static unsigned long SysClk;

/*
static unsigned char data Led1Tick = 0;
static unsigned char data Led1Cnt = 0;
static bit Led1Flash = 0;
*/

static unsigned char data BuzzerTick = 0;
static unsigned char data BuzzerCnt = 0;
static unsigned char data BuzzerTimes = 0;
static bit fSetting = 0;

void IntUserTimer(void) interrupt TF0_VECTOR    using 2
{
    TL0 += UserReload;
    TimerEvent();
}

void IntSysTimer( void ) interrupt TF1_VECTOR   using 1
{
    TStatus.Tick.loops ++;
    if(TStatus.Tick.loops > Interval.tenmsloops)
    {
        TStatus.Tick.loops = 0;
        TStatus.ms += 10;
        if(LedFlash)
        {
            LedCnt ++;
            if(LedCnt >= LedTick)
            {
                LedCnt = 0;
                LED = ~LED;
            }
        }
        
        /*
        if(Led1Flash)
        {
            Led1Cnt ++;
            if(Led1Cnt >= Led1Tick)
            {
                Led1Cnt = 0;
                LED1 = ~LED1;
            }
        }
        */
        
        if((!fSetting) && BuzzerTimes)
        {
            BuzzerCnt ++;
            if(BuzzerCnt >= BuzzerTick)
            {
                BuzzerCnt = 0;
                BUZZER = ~BUZZER;
                if(BUZZER == BUZZEROFF)
                {
                    BuzzerTimes --;
                }
            }
        }
    }
}

void beep(unsigned char times, unsigned char tick)
{
    fSetting = 1;
    BuzzerTick = tick;
    BuzzerTimes = times;
    if(times)
    {
        BUZZER = BUZZERON;
    }
    else
    {
        BUZZER = BUZZEROFF;
    }
    fSetting = 0;
}

void delay( unsigned int time )
{
    unsigned int data startms;
    
    startms = GetTickCount();
    
    while((GetTickCount() - startms) < time)
	{
	}
}

unsigned int GetTickCount(void)
{
    unsigned int data cyl, ms;
    
	#ifdef  WATCHDOG
	//ResetWDT();
	#endif

    ET1 = 0;
    ms = TStatus.ms;
    cyl = TStatus.Tick.cycles;
    ET1 = 1;
    
    return cyl / Interval.mscycles + ms;
}

unsigned int GetTenTickCount(void)
{
	unsigned int data tmp;
	
	#ifdef  WATCHDOG
	//ResetWDT();
	#endif
	
	ET1 = 0;
	tmp = TStatus.ms;
	ET1 = 1;
	
    return tmp;
}

void InitTimer( unsigned long sysclk )
{
    unsigned long data ltmp;
    
    if((TMOD & 0xf0) != 0x03)
    {
        TMOD &= 0xf0;
        TMOD |= 0x03;
    }

    SysClk = sysclk;
    ltmp = SysClk / 100;
    Interval.tenmsloops = (unsigned char)(ltmp >> 8);
    Interval.mscycles = ltmp / 10;
    
    SetIntPri(TF1_VECTOR, 0);
    SetIntPri(TF0_VECTOR, 3);
    
    TH0=0;
    TR1=1;
    ET1=1;
    enable();
}

void InitUserTimer(unsigned char rldval, unsigned char startval)
{
    UserReload = rldval;
    TL0 = startval;
    TF0 = 0;
    TR0 = 1;
    ET0 = 1;
}

void led(unsigned char ltick)
{
    if(ltick == 0)
    {
        LedFlash = 0;
        LED = LEDOFF;
    }
    else if(ltick == 0xff)
    {
        LedFlash = 0;
        LED = LEDON;
    }
    else
    {
        LedTick = ltick;
        LedFlash = 1;
    }
}

/*
void led1(unsigned char ltick)
{
    if(ltick == 0)
    {
        Led1Flash = 0;
        LED1 = LEDOFF;
    }
    else if(ltick == 0xff)
    {
        Led1Flash = 0;
        LED1 = LEDON;
    }
    else
    {
        Led1Tick = ltick;
        Led1Flash = 1;
    }
}
*/

void StartCLKO(unsigned int div)
{
    unsigned int data rld;
    
    P1_0 = 1;
    
    ET2 = 0;

    T2CON = 0;
    T2MOD = 0x02;
    
    rld = 65536 - div;
    RCAP2H = rld >> 8;
    RCAP2L = rld & 0xff;
    
    TH2 = 0xff;
    TL2 = 0xff;
    
    TR2 = 1;
}

void dummy(void)
{
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -