数码管计时器程序
#include
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsignedlonguint32;
code uint8 number[]={0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83, 0xa7,0xa1,0x86,0x8e}; //数字0到F
uint16 counter = 0; //定义计数器初始化
uint8 a[6]; //六个数码管
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
void timer1_led()
{
TMOD |= 0x10; //寄存器状态M0位置1,或运算
TMOD &= 0xDF;
TH1 = 0xFC; //12*(65535-x)/11059200 =1s
TL1 = 0x67;
TR1 = 1; //定时器置1打开,计数器开始计时
}
void int_init()
{
ET1 = 1; // 打开定时器1溢出中断使能置1
EA = 1; //总阀门打开
}
refresh_led()
{
static uint8 j = 0; //静态变量刷新
switch(j)
{
case 0:ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[0]];break;
case 1:ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[1]];break;
case 2:ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[2]];break;
case 3:ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[3]];break;
case 4:ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;j++;P0 = number[a[4]];break;
case 5:ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;j = 0;P0 = number[a[5]];break;
default :break;
}
}
main()
{
ENLED = 0;ADDR3 = 1; //138译码器使能
timer1_led(); //定时初始化
int_init();
while(1);
}
void interrupt_timer1() interrupt 3 //中断函数
{
static uint32 sec=0; //定义变量
TH1 = 0xFC;
TL1 = 0x67;
counter++;
if(1000==counter)
{
sec++;
counter=0;
a[0] = sec%10; //个位数取余
a[1] = sec/10%10; //十位取余
a[2] = sec/100%10; //百位取余
a[3] = sec/1000%10; //...............
a[4] = sec/10000%10;
a[5] = sec/100000%10;
}
refresh_led();
}
数码管计时 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)