📄 timer.c
字号:
#include "reg51.h"
#define uint unsigned int
#define uchar unsigned char
sbit P0_0=P0^0;
sbit P0_1=P0^1;
sbit P0_2=P0^2;
sbit P0_3=P0^3;
sbit P0_4=P0^4;
sbit P0_5=P0^5;
sbit P0_6=P0^6;
sbit P0_7=P0^7;
//P2口
sbit P2_0=P2^0;
sbit P2_1=P2^1;
sbit P2_2=P2^2;
sbit P2_3=P2^3;
sbit P2_4=P2^4;
sbit P2_5=P2^5;
sbit P2_6=P2^6;
sbit P2_7=P2^7;
//定义全局变量、
uchar ucn=0;
uchar count_s=0;
uchar count_m=54;
uchar count_h=15;
//延时程序
void delay(uchar m)
{
uint i,j;
for(i=0;i<m;i++)
for(j=0;j<100;j++);
}
//显示
void display(uchar n)
{
if(n==0)
{
P2_0=0;
P2_1=0;
P2_2=0;
P2_3=0;
P2_4=0;
P2_5=0;
P2_6=1;
// P2_7=1;
}
if(n==1)
{
P2_0=1;
P2_1=0;
P2_2=0;
P2_3=1;
P2_4=1;
P2_5=1;
P2_6=1;
// P2_7=1;
}
if(n==2)
{
P2_0=0;
P2_1=0;
P2_2=1;
P2_3=0;
P2_4=0;
P2_5=1;
P2_6=0;
// P2_7=1;
}
if(n==3)
{
P2_0=0;
P2_1=0;
P2_2=0;
P2_3=0;
P2_4=1;
P2_5=1;
P2_6=0;
// P2_7=1;
}
if(n==4)
{
P2_0=1;
P2_1=0;
P2_2=0;
P2_3=1;
P2_4=1;
P2_5=0;
P2_6=0;
// P2_7=1;
}
if(n==5)
{
P2_0=0;
P2_1=1;
P2_2=0;
P2_3=0;
P2_4=1;
P2_5=0;
P2_6=0;
// P2_7=1;
}
if(n==6)
{
P2_0=0;
P2_1=1;
P2_2=0;
P2_3=0;
P2_4=0;
P2_5=0;
P2_6=0;
// P2_7=1;
}
if(n==7)
{
P2_0=0;
P2_1=0;
P2_2=0;
P2_3=1;
P2_4=1;
P2_5=1;
P2_6=1;
// P2_7=1;
}
if(n==8)
{
P2_0=0;
P2_1=0;
P2_2=0;
P2_3=0;
P2_4=0;
P2_5=0;
P2_6=0;
// P2_7=1;
}
if(n==9)
{
P2_0=0;
P2_1=0;
P2_2=0;
P2_3=0;
P2_4=1;
P2_5=0;
P2_6=0;
// P2_7=1;
}
}
//显示横杠
void heng(void)
{
P2_0=1;
P2_1=1;
P2_2=1;
P2_3=1;
P2_4=1;
P2_5=1;
P2_6=0;
}
//初始化
void Init(void)
{
EA=1; //CPU中断允许
ET0=1; //允许T0溢出中断
TMOD=0x01; //工作方式1,16位定时器
TL0=0xaf;
TH0=0x3c;
}
//中断程序
void timer0(void) interrupt 1 using 1
{
TL0=0x77;
TH0=0xec;
if(ucn==200)
{
count_s++;
if(count_s==60)
{
count_s=0;
count_m++;
if(count_m==60)
{
count_m=0;
count_h++;
if(count_h==24)
count_h=0;
}
}
ucn=0;
}
ucn++;
}
void main(void)
{
Init();
TR0=1; //允许计数
while(1)
{
P0_0=0;
display(count_s%10);
delay(1);
P0_0=1;
P0_1=0;
display(count_s/10);
delay(1);
P0_1=1;
P0_2=0;
display(count_m%10);
delay(1);
P0_2=1;
P0_3=0;
display(count_m/10);
delay(1);
P0_3=1;
P0_4=0;
display(count_h%10);
delay(1);
P0_4=1;
P0_5=0;
display(count_h/10);
delay(1);
P0_5=1;
P0_6=0;
heng();
delay(1);
P0_6=1;
P0_7=0;
heng();
delay(1);
P0_7=1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -