📄 ledtimer.c
字号:
/****************************************************************************/
/* */
/* Copyright (c) 2005, 老树工作室 */
/* All rights reserved. */
/* */
/* Email:laoshu0902@163.com */
/* www.laoshu0902.bokee.com */
/* */
/****************************************************************************/
/****************************************************************************/
/* 文件名:ledtimer.c */
/* 版 本:Version 1.0 */
/* 描 述:定时器控制流水灯演示代码 */
/* 作 者:spot */
/* 函 数: */
/* time0_over_int */
/* system_init */
/* */
/* 历史记录: */
/* spot 2005-06-20 Creat Inital version. (Version 1.0) */
/****************************************************************************/
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
typedef unsigned char u_char;
typedef unsigned int u_int;
u_char timer_int; /* 定时到标志 */
u_int int_times; /* 定时计数 */
u_int loopcount;
u_char set_ledf = 0x01;
u_char set_ledb = 0x80;
/* 系统上电初始化 */
void system_init(void);
/* 定时器0中断服务程序 ,使用第2组寄存器 */
void time0_over_int(void) interrupt 1 using 2
{
TF0=0;
int_times++;
if (int_times == 2000)
{
timer_int = 1;
int_times = 0;
}
}
/* 系统上电初始化 */
void system_init()
{
EA=0;
timer_int=0;
int_times=0;
TMOD = 0x02; /* T/C0 方式2 定时 */
TH0 = 20; /* 预置计数初值 */
TL0 = 20;
ET0 = 1; /* 开 T/C0 中断 */
TR0 = 1; /* 启动 T/C0 */
EA = 1; /* 开CPU中断 */
}
main()
{
system_init();
while(1)
{
if (timer_int == 1) /* 500ms 定时到 */
{
timer_int = 0;
loopcount++;
if (loopcount <= 8 )
{
P1 = set_ledf;
set_ledf <<= 1;
if (set_ledf == 0x00)
set_ledf = 0x01;
}
if (loopcount > 8)
{
P1 = set_ledb;
set_ledb >>= 1;
if (set_ledb == 0x00)
{
set_ledb = 0x80;
loopcount = 0;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -