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