51单片机定时器中断
【摘要】
51定时器中断
1.定时器/计数器
51单片机有2个16位定时器/计数器:定时器0(T0为P3.4) 和定时器1(T1为P3.5) 这里所说的16位是指定时/计数器内部分别有16位的计数寄 存器。当工...
51定时器中断
1.定时器/计数器
51单片机有2个16位定时器/计数器:定时器0(T0为P3.4) 和定时器1(T1为P3.5)
这里所说的16位是指定时/计数器内部分别有16位的计数寄 存器。当工作在定时模式时,每经过一个机器周期内部的16位计数 寄存器的值就会加1,当这个寄存器装满时溢出。 我们可以算出工作在定时模式时最高单次定时时间为 65535*1.085us=时间(单位us)
当工作在计数器模式时,T0(P3.4引脚),T1(P3.5引脚)每 来一个脉冲计数寄存器加1
定时器作用:定时计数器可以用于精确事件定时,PWM脉宽调 制,波形发生,信号时序测量的方面。
使用51定时/计数器步骤:
(1)启动定时/计数器(通过TCON控制器)
(2)设置定时/计数器工作模式(通过TMOD控制器)
(3)查询定时/计数器是否溢出(读TCON内TF位
3.代码片:
(1)定时器定时:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DU = P2^6;
sbit WE = P2^7;
uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void display(uchar i)
{
uchar bai, shi, ge;
bai = i / 100; //236 / 100 = 2
shi = i % 100 / 10; //236 % 100 / 10 = 3
ge = i % 10;//236 % 10 =6
P0 = 0XFF;
WE = 1;
P0 = 0XFE; //1111 1110
WE = 0;
DU = 1;
P0 = tabel[bai];//
DU = 0;
delay(5);
P0 = 0XFF;
WE = 1;
P0 = 0XFD; //1111 1101
WE = 0;
DU = 1;
P0 = tabel[shi];//
DU = 0;
delay(5);
P0 = 0XFF;
WE = 1;
P0 = 0XFB; //1111 1011
WE = 0;
DU = 1;
P0 = tabel[ge];//
DU = 0;
delay(5);
}
void timer0Init()
{
TR0 = 1;
TMOD = 0X01;
TH0 = 0x4b;
TL0 = 0xfd;
}
void main()
{
uchar mSec, Sec;
timer0Init();
while(1)
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0x4b;
TL0 = 0xfd;
mSec++;
if(mSec == 20)
{
mSec = 0;
Sec++;
}
}
display(Sec);
if(Sec > 10)
Sec = 0;
}
}
- 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
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
(2)定时器计数:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DU = P2^6;
sbit WE = P2^7;
sbit LED1 = P1^0;
uchar code tabel[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void display(uchar i)
{
uchar bai, shi, ge;
bai = i / 100;
shi = i % 100 / 10;
ge = i % 10;
P0 = 0XFF;
WE = 1;
P0 = 0XFE; //1111 1110
WE = 0;
DU = 1;
P0 = tabel[bai];//
DU = 0;
delay(5);
P0 = 0XFF;
WE = 1;
P0 = 0XFD;
WE = 0;
DU = 1;
P0 = tabel[shi];
DU = 0;
delay(5);
P0 = 0XFF;
WE = 1;
P0 = 0XFB; //1111 1011
WE = 0;
DU = 1;
P0 = tabel[ge];//
DU = 0;
delay(5);
}
void timer0Init()
{
TR0 = 1;
TMOD |= 0X05;
TH0 = 0;
TL0 = 0;
}
void timer1Init()
{
TR1 = 1;
TMOD |= 0X10;
TH1 = 0x4b;
TL1 = 0xfd;
}
void main()
{
uchar mSec, Sec;
timer0Init();
timer1Init();
while(1)
{
if(TF1 == 1)
{
TF1 = 0;
TH1 = 0x4b;
TL1 = 0xfd;
mSec++;
if(mSec == 10)
{
mSec = 0;
LED1 = ~LED1;
}
}
display(TL0);
}
}
- 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
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
(3)定时器中断:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DU = P2^6;
sbit WE = P2^7;
sbit key_s2 = P3^0;
sbit key_s3 = P3^1;
uchar num;
uchar mSec, Sec;
uchar code SMGduan[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,};
uchar code SMGwei[] = {0xfe, 0xfd, 0xfb};
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void display(uchar i)
{
static uchar wei;
P0 = 0XFF;
WE = 1;
P0 = SMGwei[wei];
WE = 0;
switch(wei)
{
case 0: DU = 1; P0 = SMGduan[i / 100]; DU = 0; break;
case 1: DU = 1; P0 = SMGduan[i % 100 / 10]; DU = 0; break;
case 2: DU = 1; P0 = SMGduan[i % 10]; DU = 0; break;
}
wei++;
if(wei == 3)
wei = 0;
}
void timer0Init()
{
EA = 1;
ET0 = 1;
TR0 = 1;
TMOD = 0X01;
TH0 = 0xED;
TL0 = 0xFF;
}
void main()
{
timer0Init();
while(1)
{
if(key_s2 == 0)
{
delay(20);
if(key_s2 == 0)
{
if(num != 120)
num++;
while(!key_s2);
}
}
if(key_s3 == 0)
{
delay(20);
if(key_s3 == 0)
{
if(num > 0)
num--;
while(!key_s3);
}
}
}
}
void timer0() interrupt 1
{
TH0 = 0xED;
TL0 = 0xFF;
display(num);
}
- 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
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
文章来源: wlybsy.blog.csdn.net,作者:万里羊,版权归原作者所有,如需转载,请联系作者。
原文链接:wlybsy.blog.csdn.net/article/details/90757901
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)