<>51 timer interrupt

<>1. timer / Counter

51 SCM has 2 individual 16 Bit timer / Counter : timer 0(T0 by P3.4) minute timer 1(T1 by P3.5)
What we're talking about here 16 When bit is specified / Inside the counter are 16 Bit count send Memory . When working in timing mode , Within each machine cycle 16 Bit count
The value of the register will be added 1, Overflow when this register is full . We can work out that the maximum single timing time is 65535*1.085us= time ( Company us)
When working in counter mode ,T0(P3.4 Pin ),T1(P3.5 Pin ) each One pulse count register plus 1

Timer function : The timing counter can be used for precise event timing ,PWM Pulse width modulation system , Waveform generation , Signal timing measurement .

<> use 51 timing / Counter steps :

(1) Start timing / Counter ( adopt TCON controller )
(2) Set timing / Counter operation mode ( adopt TMOD controller )
(3) Query timing / Whether the counter overflows ( read TCON within TF position

<>3. Code slice :

(1) Timer timing :
#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) Timer count :
#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) timer interrupt :
#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); }

Technology
©2019-2020 Toolsou All rights reserved,
elementui select obtain value inherit jpa Repository Write custom method query Thread.getContextClassLoader And Thread.getClassLoader() difference airflow Timed tasks + Time setting +cron expression @Repository The role of annotations Result Encapsulation of return result information python Short circuit logic in latex Custom commands in ———\newcommand[work] python read txt Last line of file The project followed for a year , The customer finally said no