Hands-on teaching of 51 single-chip microcomputer | Lesson 3: Timer interrupt

Publisher:张延强Latest update time:2022-04-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Interruption

insert image description here

(1) External interruption

Open the general interrupt EA, select external interrupt EX0 or EX1, because IE is addressable, so directly operate on EX0 or EX1, then set IT0 (in TCOD) to level trigger or jump edge trigger (power-on default register 0, level trigger mode), then write the interrupt processing function to determine the number after interrupt according to the priority in the table below

insert image description here

(2) Timer interrupt

TMOD is the working mode register of the timer/counter.

TCON is the control register, which controls the start and stop of T0T1 and sets the overflow flag

Set the timing mode first
insert image description hereinsert image description here
insert image description here

If the working mode 01 is selected, TH TL is 16 bits

2^16=65536

(65536-N)/256 is equivalent to a bucket that can hold 65536, scooping out N units of water, and then adding 1 unit of water every 1us, and finally taking 5000us=50ms


Next is the detailed explanation of TCON

insert image description here
insert image description here

//Use the timer to replace the delay subfunction in the previous lesson

#include

#define uchar unsigned char 

#define uint unsigned int 

sbit dula=P2^6;

sbit temperature=P2^7;

flying number;

uchar code table[]={

0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};

void Time0_delay();

void main()

{

temperature=1;

P0=0xc0;

temperature=0;

while(1)

{

for(num=0;num<16;num++)

{

game=1;

P0=table[num];

game=0;

Time0_delay();

}

}

}

void Time0_delay()

{

unsigned int i;

TMOD&=0xF0;//有效避免打乱高四位

TMOD|=0X01; //Final TMOD= ABCD 0001

for(i=0;i<20;i++)

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TR0=1; //Start the timer

while(!TF0); //delay 50ms

TF0=0; // Overflow bit cleared

TR0=0; //turn off the timer

//Repeat 20 times for 1s

}

}


3. Code

//Comprehensive use of interrupts and timers

#include

#define uint unsigned int

#define uchar unsigned char

sbit dula=P2^6;

sbit temperature=P2^7;

sbit d1=P1^0;

fly num,tt;

uchar code table[]={

0x3f,0x06,0x5b,0x4f,

0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,

0x39,0x5e,0x79,0x71};


void main()

{

num=0;

tt=0;

TMOD=0x01; //Set timer 0 to working mode 1

//Assign initial value

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

EA=1; //Open the general interrupt

ET0=1; // Enable the interrupt of timer 0

TR0=1; //Start timer 0, it will keep running as long as it is turned on

temperature=1;

P0=0xfe; //First digital tube

temperature=0;

dula=1; P0=0x3f; //To avoid garbled code, assign the initial value 0

game=0;

while(1)

{

//20 times is 1s, perform 1 if

if(tt==20)

{

tt=0;

  num++;

if(num==16)

num=0;//clear!!!

game=1;

P0=table[num];

game=0;

}

}

}


/*void delay(uint z)

{

fly x,y;

for(x=z;x>0;x--)

for(y=110;y>0;y--);

}*/


void exter0()interrupt 1

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

tt++;

}


4. Summary

insert image description here

Reference address:Hands-on teaching of 51 single-chip microcomputer | Lesson 3: Timer interrupt

Previous article:Hands-on teaching of 51 single-chip microcomputer | Section 4 Dynamic digital tube, use 6-bit digital tube to make a clock
Next article:Step-by-step teaching of 51 single-chip microcomputer lesson 3 | reset circuit, digital tube static display

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号