📄 ext0led.c
字号:
/****************************************************************************/
/* */
/* Copyright (c) 2005, 老树工作室 */
/* All rights reserved. */
/* */
/* Email:laoshu0902@163.com */
/* www.laoshu0902.bokee.com */
/* */
/****************************************************************************/
/****************************************************************************/
/* 文件名:ext0led.c */
/* 版 本:Version 1.0 */
/* 描 述:用外部中断0方式实现定时器控制的流水灯实验代码 */
/* */
/* 作 者:spot */
/* 函 数: */
/* time0_over_int */
/* ext0_int_proc */
/* system_init */
/* */
/* 历史记录: */
/* spot 2005-06-25 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;
/* 函数声明 */
void time0_over_int(void); /* 定时器0中断服务程序 ,使用第2组寄存器 */
void ext0_int_proc(void); /* 外部中断0中断服务程序,使用第2组寄存器 */
void system_init(void); /* 系统上电初始化 */
/* 声明结束 */
u_int int_times; /* 定时计数 */
bit timer_int; /* 定时标志 */
u_int loopcount;
u_char set_ledf = 0x01;
u_char set_ledb = 0x80;
/* 定时器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;
}
}
/* 外部中断0中断服务程序,使用第2组寄存器 */
void ext0_int_proc(void) interrupt 0 using 2
{
TR0 = 1; /* 启动 T/C0 计数 */
}
/* 系统上电初始化 */
void system_init(void)
{
EA = 0;
timer_int=0;
int_times=0;
P1 = 0xFF;
IT0 = 0; /* 外部中断0低电平触发 */
TMOD = 0x02; /* T/C0 方式2 定时 */
TH0 = 20; /* 预置计数初值 */
TL0 = 20;
TR0 = 0; /* 停止T/C0计数 */
IE = 0x83; /* 外部中断0,定时器0,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;
TR0 = 0; /* 停止T/C0计数 */
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -