#include "timer.h"
unsigned int g_timer_tick = 0;
#ifdef IS_INTERRUPT
#pragma vector = T1_VECTOR
__interrupt void T1_ISR(void)
{
g_timer_tick++;
IRCON &= ~(1<<1);
}
#endif
void clear_timer_tick(void)
{
g_timer_tick = 0;
}
unsigned int get_timer_tick(void)
{
return g_timer_tick;
}
void Timer1_Init(void)
{
#ifdef IS_INTERRUPT
//定时器方式
T1CTL &= ~( (3<<0) |(3<<2) );
T1CTL |= ((1<<0) | (1<<2) );
IEN1 |= 1<<1;
IEN0 |= 1<<7;
#else
/* 系统时钟是16MHZ
** 8分频
** 当达到计数0xffff,IRCON.T1IF置1 时间1S
*/
//查询方式
T1CTL &= ~( (3<<0) | (3<<2) );
T1CTL |= ((1<<0) | (1<<2) );
#endif
}
#ifndef IS_INTERRUPT
bool ircon_state(void)
{
if (IRCON&(1<<1))
{
IRCON &= ~(1<<1);
return true;
}
return false;
}
#endif
//头文件===================================
#ifndef __TIMER_H__
#define __TIMER_H__
#include "all.h"
#define IS_INTERRUPT
void Timer1_Init(void);
#ifndef IS_INTERRUPT
bool ircon_state(void);
#else
unsigned int get_timer_tick(void);
void clear_timer_tick(void);
#endif
#endif
//例子=============================
void main(void)
{
#ifndef IS_INTERRUPT
unsigned char i =0;
#endif
InitGPIO(); //设置LED灯相应的GPIO口
Timer1_Init();
while(1) //死循环
{
#ifndef IS_INTERRUPT
if (ircon_state())
{
i++;
if (i>32)
{
i=0;
LED1 = ~LED1;
}
}
#else
if (get_timer_tick() > 32)
{
clear_timer_tick();
LED1 = ~LED1;
}
#endif
}
}
网友评论