forked from MetalSeed/stc51_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.4.c
66 lines (62 loc) · 1.65 KB
/
6.4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/********************************************************************
* 文件名 : 定时器的使用.c
* 描述 : 用定时器1的方式1的程序代码。
相对前面的软件延时,定时器的延时更为精确。
晶振为11.0592,延时时间为50毫秒。
***********************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint Count = 0;
uchar code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/********************************************************************
* 名称 : Time0_Init()
* 功能 : 定时器的初始化,12MZ晶振,0.1ms
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Time0_Init()
{
TMOD = 0x10;
TH1 = 0x4c;
TL1 = 0x00;
IE = 0x88;
TR1 = 1;
}
/********************************************************************
* 名称 : Time0_Int()
* 功能 : 定时器中断,中断中实现 Count 加一
* 输入 : 无
* 输出 : 无
***********************************************************************/
void Time0_Int() interrupt 3
{
TH1 = 0x4c;
TL1 = 0x00;
Count++; //长度加1
}
/********************************************************************
* 名称 : Main()
* 功能 : 主函数
* 输入 : 无
* 输出 : 无
***********************************************************************/
void main()
{
uchar i = 0;
Time0_Init();
P2 = 7;
while(1)
{
P0 = table[i % 10]; //取 i 的个位
while(1)
{
if(Count == 20) //当Count为 10000 时,i 自加一次,20 * 50MS = 1S
{
Count = 0;
i++;
break;
}
}
}
}