<>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;  }  }
(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); } }
(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); }

技术
©2019-2020 Toolsou All rights reserved,
Spring注解驱动开发——bean的后置处理器cv2.resize()和imutil.resize()“平生不见陈近南(天地会总舵主),再称英雄也枉然”Qt4中的QButtonGroup类使用CentOS7 关闭防火墙Python爬虫入门教程 74-100 Python分布式夯实,夯实,还是**夯实协议学习——HTTP2帧结构总结Qt怎样禁止用户最大化窗口解决办法markdown书写数学公式java多线程之Future模式使用