include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar num=100
uchar time=0
//共阴数码管七段码
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,0x7f,0x6f,0x77}
void Delay1ms(uint i) //1ms延时程序
{
uint j
for(i>0i--)
{
for(j=0j<164j++)
{}
}
}
void main(void )//主程序
{
TMOD=0X01
ET0=1
EA=1
TH0=(65536-50000)/256//定时50ms
TL0=(65536-50000)%256
TR0=1
while(1)
{
P0=table[num/10]
Delay1ms(2)
P2=table[num%10]
Delay1ms(2)
}
}
void TIMER0()interrupt 1//中断程序
{
TH0=(65536-50000)/256
TL0=(65536-50000)%256
time++
if(time==20)
{
time=0
num--
}
if(num==0)
{
num=100
}
}
//功能:0~99秒的简易秒表设计,两个静态数码管,定时器采用中断方式
#include"reg51.h"
#define uchar unsigned char
#define uint unsigned int
uchar count=0 //对50ms定时时间进行计数
uchar miao=0 //秒计数器
//函数名:timer_1()
//函数功能:定时器T1的中断函数,T1在工作方式1下每50秒产生中断,执行该中断函数
//形式参数:无
//返回值:无
void timer_1() interrupt 3 //T1的中断类型号为3
{
TH1=(65536-50000)/256 //重新设置T1计数初值高8位
TL1=(65536-50000)%256 //重新设置T1计数初值低8位
count++ //50ms计数器加1
if(count==20) //1s时间到
{
count=0 //50ms计数器清0
miao++ //秒计数器加1
if(miao==100)miao=0 //miao计数到100,则从0开始计数
}
}
bit b=0
void int_0() interrupt 0
{
if(b == 0){TR1 = 0b = 1}
else
{
b = 0
TR1 = 1
}
}
void int_1() interrupt 2
{
miao=0
count = 0
TH1=(65536-50000)/256
TL1=(65536-50000)%256
TR1=1
}
//函数名:disp
//函数功能:将i的值显示在两个静态连接的数码管上
//形式参数:i,取值范围0~99
//返回值:无
void disp(uchar i)
{
uchar led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
//定义0~9显示码,共阳极数码管
P1=led[i/10] //显示i高位
P2=led[i%10] //显示i地位
}
void main()
{
TMOD=0x10 //设置T1在工作方式1
TH1=(65536-50000)/256 //设置T1计数初值高8位,定时时间50ms
TL1=(65536-50000)%256 //设置T1计数初值低8位
ET1=1 //开放T1中断允许
EX0 = 1
IT0 = 1
EX1 = 1
IT1=1
EA=1 //开放总中断允许
TR1=1 //启动T1开始计时
while(1)
{
disp(miao) //显示秒计数器值
}
}
#include<reg52.h>unsigned int s
unsigned char key
sbit d1=P3^7
void delay() //动态显示延时
{
int k
for(k=0k<100k++)
}
void delay1()//去抖动延时
{
unsigned char code table[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
P2=table[s%10]P3=0xf8delay()P3=0xf0
P2=table[s/10%10] P3=0xf4delay()P3=0xf0
P2=table[s/100%10]P3=0xf2delay()P3=0xf0
P2=table[s/1000] P3=0xf1delay()P3=0xf0
}
void main()
{
TMOD = 0x01
TH0 = (65536 - 10000) / 256//初值,每10ms中断一次
TL0 = (65536 - 10000) % 256
ET0=1
EA=1
while(1) {
delay1()
if(!d1) {
delay1()
if(!d1) { //去抖动
while(!d1) delay1()//等待释放
key++
if(key == 4) key = 1
}
}
if(key == 1) TR0 = 1//开始计时
else if(key == 2) TR0 = 0//停止
else s = 0
}
}
void time0() interrupt 1 //内部中断
{
TH0 = (65536 - 10000) / 256
TL0 = (65536 - 10000) % 256
s++
if(s == 10000) s = 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)