目标:编写定时器1中断--控制数码管程序
#include<reg52.h>
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P3^0;
sbit LSB=P3^1;
sbit LSC=P3^3;//数码管位选
sbit led = P2^0; //led灯
u8 code list[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值
u8 n = 0; //定义在全局的被程序调用会不断累加,不会使用后又变成0
void Timer1Init() //定时中断1初始
{
TMOD |= 0x10;
TH1 = 0xFC;
TL1 = 0x18;
ET1 = 1;
EA = 1;
TR1 = 1;
}
void main()
{
LSA=0;
LSB=0;
LSC=0;
Timer1Init(); //定时器1 的初始化
while(1);
}
void Timer1() interrupt 3
{
static u16 i; //静态变量
TH1 = 0xfc;
TL1 = 0x18;
i++;
if(i==1000){
i = 0;
P0 = list[n++];//显示数字
if(n==16) //到列表尽头再重新来
n=0;
}
}
//状态: